【问题标题】:alternative to jqgrid triggerToolbar on a local dataset替代本地数据集上的 jqgrid triggerToolbar
【发布时间】:2012-10-10 14:20:04
【问题描述】:

我有一个显示大量数据的 jqgrid。通过 jquery ajax 调用(在 jqgrid 逻辑之外)定期从服务器检索数据。将检索到的数据与先前重试的数据进行比较(并在 js 中存储为 var。它用作 jqgrid 的数据)。如果它们不同,则刷新本地数据,然后触发 jqgrid 重新加载。 jqgrid 数据类型是 jsonstring。

此解决方案运行良好,除非用户在过滤器工具栏中有过滤器值。因为我设置了一个 0.1 秒的计时器来触发 loadcomplete 事件中的过滤器,所以当有过滤器字符串时,整个网格刷新如下所示:

  • 原来jqgrid中显示了20条记录(因为用户正在过滤列中的某个值)
  • jqgrid 被刷新,因为单独的 ajax 调用从服务器轮询的数据与浏览器中存储的数据不同
  • jqgrid 将在很短的时间内显示所有新数据
  • jqgrid 过滤器在 loadcomplete 内触发。屏幕再次显示 20 条记录。

它在技术上仍然有效。但是有没有办法在可视化网格之前在 jsonstring 上本地重新应用过滤器?换一种说法,jqgrid能否只可视化一次,既会加载新的jsonsting,又会同时应用之前放置在过滤器框中的过滤器?

谢谢 卡比

更新:

我尝试了 Oleg 的一种解决方案,在重新加载网格时应用过滤器。这是demo。只要数据类型是本地的,它就可以完美地工作。我的页面实际上使用数据类型 jsonstring 来重新加载网格。代码中的这个函数似乎确实适用于 jsonstring。我希望在外部 jquery ajax 成功从服务器检索数据后调用这样的函数。

function filter() {
            var searchFiler = $("#filter").val(), f;

            if (searchFiler.length === 0) {
                grid[0].p.search = false;
                $.extend(grid[0].p.postData,{filters:""});
            }
            f = {groupOp:"OR",rules:[]};
            f.rules.push({field:"name",op:"cn",data:searchFiler});
            f.rules.push({field:"note",op:"cn",data:searchFiler});
            grid[0].p.search = true;
            $.extend(grid[0].p.postData,{filters:JSON.stringify(f)});
            grid.trigger("reloadGrid",[{page:1,current:true}]);
        }

有人可以帮我吗?非常感谢。

【问题讨论】:

    标签: jqgrid filter local reload


    【解决方案1】:

    datatype: "local" 的用法相比,datatype: "jsonstring" 的用法存在细微差别。大家可以对比一下代码here的对应部分。 datatype: "local" 的代码差异之一是 addLocalDatapopulateVisible 函数的使用。最后一个函数(populateVisible)仅在虚拟滚动(scroll: 1scroll: true)的情况下使用。在您的情况下,datatype: "jsonstring"datatype: "local" 之间的重要区别是addLocalDatadatatype: "local" 的情况下的调用。

    函数addLocalData 对本地数据进行分组和过滤(参见here)。此外,它将显示的行列表剪切到当前页面(参见here)。

    因此,如果服务器返回未过滤的数据并且您需要显示过滤后的数据,那么您应该使用datatype: "local" 而不是datatype: "jsonstring"。你应该使用data 而不是datastr。您可能需要使用localReader 而不是jsonReader(请参阅the documentation),或者只是手动将从服务器返回的数据转换为默认可以读取的格式localReader

    更新:在another answer 中,我描述并包含了演示如何使用localReader

    您可以选择将您从服务器返回的输入数据转换为标准格式(或以该格式从服务器返回数据)。 data 参数应该是命名对象的数组,其属性类似于colMode 中的列名。所以你可以做的只是一个简单的循环通过rows数组并以jqGrid所需的格式创建另一个数组。对应的代码大概如下:

    // let us you have myImput with myImput.rows
    // and you have cm which you use as the value of colModel parameter
    var mydata = [], input = myImput.rows, l = input.length,
        cmLength = cm.length, i, j, inputItem, item;
    for (i = 0; i < l; i++) {
        inputItem = input[i];
        item = {id: inputItem.id};
        inputItem = inputItem.cell;
        for (j = 0; j < cmLength; j++) {
            item[cm[j].name] = inputItem[j];
        }
        mydata.push(item);
    }
    

    经过这样的转换,您可以使用mydata数组作为data参数的值。

    【讨论】:

    • 感谢您的详细解释。我必须承认。 Tony 在创建 jqgrid 方面做得很好,但我通过阅读您的帖子了解了更多关于 jqgrid 的信息。
    • 我一直在尝试使用本地数据类型来处理从服务器轮询的数据,但网格总是呈现空白。我想不出文档中的默认本地阅读器如何工作。在所有数据类型本地示例中,我只能看到一个包含列字段值数组的数组。但默认的 localReader 需要格式 {root: "rows", page: "page",total: "total", records: "records", repeatitems: false, cell: "cell", id: "id"} 。除了重复次数外,它与 jsonreader 相同。我错过了什么吗?
    • @casbby:发一下服务器返回的数据的例子是不是很方便?包括定义 jqGrid 的代码可以清除其他打开的问题。如果我猜你可以尝试使用datatype: "local", data: mydata, localReader: {repeatitems: false} 而不是datatype: "jsonstring", datastr: mydata
    • 我从源码看了一下addlocadata函数,看起来像
    • 是的,当然。我从服务器轮询的数据大致如下 '{ "page":"1", "total":1, "records":"1", "rows":[{"id":"1","cell" :["v1","v2","v3","v4"]}, {"id":"2","cell":["v1","v2","v3","v4"] }],}'。我从源代码中查看了 addlocaldata 函数。它所做的第一件事是' if(!$.isArray(ts.p.data)) { return; }' 所以 mydata 不能是 json 对象,它必须是一个数组。如果我将从服务器返回的 json 分配给 mydata,我必须使用 data:mydata.rows 才能使其工作? (我没有机会证明)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 2011-03-11
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多