jqgrid在重新加载时保留过滤器 [英] jqgrid retain filter on reload

查看:27
本文介绍了jqgrid在重新加载时保留过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经很多了,所以请原谅我再问一次.

This question has been as a lot so please forgive me asking again.

我有一个从 url 加载的网格.

I have a grid loaded from a url.

我使用 loadonce: true

I use loadonce: true

我使用过滤器工具栏:

$("#status").jqGrid('filterToolbar', { stringResult:true, searchOnEnter:false, autosearch: true, defaultSearch: "cn" });

我有一个下拉搜索列表,可以从中选择我要查找的内容.效果很好.

I have a dropdown search list from which I can select what I'm looking for. Works great.

我有一个导航按钮,每 30 秒单击一次以启动重新加载.效果很好.

I have a navbutton I click to initiate a reload every 30 seconds. Works great.

$("#status").jqGrid('navButtonAdd','#statuspager', { caption: '', buttonicon: 'ui-icon-play', onClickButton: function ()
{
stint = setInterval (function() {
      postfilt = $("#status").jqGrid('getGridParam', 'postData').filter;

      $("#status").jqGrid('setGridParam',{
             url: './ar_status.cgi?systemtype=' + systype,
             datatype: "json",
             postData: postfilt,
             search: true,
             loadtext: "Refreshing grid...",
             loadonce: true,
             loadui: "block"
      });
      $("#status").jqGrid().trigger("reloadGrid");
}, 30000);
},
title: 'Start Auto Refresh (every 30 sec.)'
});

使用谷歌浏览器我可以看到指定的过滤器被发布到服务器:

Using google chrome I can see the filters that were specified being posted to the server:

systemtype:production
_search:true
nd:1358887757603
rows:1000
page:1
sidx:system
sord:asc
totalrows:700
filters:{"groupOp":"AND","rules":[{"field":"system","op":"eq","data":"ATTDA02"}]}

我可以在重新加载之间更改过滤器并查看新的甚至多个过滤器:

I can change the filter between reloads and see the new, even multiple filters:

systemtype:production
_search:true
nd:1358887847592
rows:1000
page:1
sidx:system
sord:asc
totalrows:700
filters:{"groupOp":"AND","rules":[{"field":"system","op":"eq","data":"ATTDA02"},{"field":"dow","op":"cn","data":"MO"}]}

我在初始加载时使用 multipleSearch: true.我很确定它会在重新加载时保留

I am using multipleSearch: true on the initial load. I'm pretty sure that's retained on reload

在网格上,过滤器工具栏保留了我的过滤条件,包括文本和选择,但是当重新加载网格时,过滤器被忽略并且整个数据集都显示在网格中.

On the grid, the filtertoolbar retains my filter criteria, both text and selects, but when the grid reloads, the filters are ignored and the entire dataset is diplayed in the grid.

我已经尝试了许多在 SO 上发布的示例.我尝试将 [{current:true}] 添加到 reloadGrid 调用 - 没有区别.我似乎无法在重新加载时保留和应用过滤器.

I've tried many examples posted here on SO. I've tried adding [{current:true}] to the reloadGrid call - no difference. I can't seem to retain and apply the filters on a reload.

我希望重新加载始终从服务器返回完整的数据集(这很好),并允许当前的过滤器设置来控制重新加载后显示的内容.我不想使用 postdata 来构建一个新的 SQL 选择语句 - 服务器端过滤,因为在任何时候用户都可能想要单击暂停按钮,选择一个新的过滤器并从整个数据集中查看其他内容,而无需再次重新加载.

I would like the reload to always return the full dataset from the server (which happens fine), and allow the current filter settings to control what is shown after the reload. I do not want to use the postdata to build a new SQL select statement - server side filtering, because at any time the user may want to click the pause button, select a new filter and view something else from the entire dataset without another reload.

$("#status").jqGrid('navButtonAdd','#statuspager', { caption: '', buttonicon: 'ui-icon-pause', onClickButton: function ()
{
   clearInterval(stint);
},
title: 'End Auto Refresh'
});

正如我在一篇文章中看到的那样,如何在不使用 cookie 的情况下做到这一点?

How can this be done without using cookies, as I saw in one post?

我可以尝试使用 jqGridExport'ing postfilt var,然后 jqGridImport'ing 它,但希望有更直接的方法.我什至不确定这是否会有所帮助,因为我已经通过 postData 在网格中拥有了我需要的一切.

I could try using jqGridExport'ing the postfilt var, and then jqGridImport'ing it but was hoping for a more direct approach. I'm not even sure this would help since I already have everything I need right here in the grid via postData.

附带说明,在我上面的 setGridParam 中,我指定的 loadtext 永远不会显示.而是显示默认的正在加载...".所有其他参数都正常工作.

As a side note, in my setGridParam above, the loadtext I specify is never displayed. Instead, the default "Loading..." is displayed. All of the other parameters are working.

提前非常感谢,迈克

解决方案.从服务器重新加载 [json] 后保留过滤器、排序索引和排序顺序的完整 loadComplete:

Solution. The complete loadComplete to retain filters, sort index and sort order after a [json] reload from the server:

                    loadComplete: function() {
                            var $this = $(this);
                            postfilt = $this.jqGrid('getGridParam','postData').filters;
                            postsord = $this.jqGrid('getGridParam','postData').sord;
                            postsort = $this.jqGrid('getGridParam','postData').sidx;

                            if ($this.jqGrid("getGridParam", "datatype") === "json") {
                                    setTimeout(function () {
                                            $this.jqGrid("setGridParam", {
                                                    datatype: "local",
                                                    postData: {filters: postfilt, sord: postsord, sidx: postsort},
                                                    search: true
                                            });

                                            $this.trigger("reloadGrid");
                                    }, 50);
                            }
                    }

推荐答案

我不确定,但我想你的第一个问题的根源可能是混合 postData.filterspostData 和使用 filter 属性而不是过滤器``.你用

I am not sure, but I suppose that the origin of your first problem could be mixing postData.filters and postData and usage filter property instead of filters``. You use

postfilt = $("#status").jqGrid('getGridParam', 'postData').filter;

获取 filter 属性而不是 filters.你得到 undefined 值.所以将 postData 设置为 postfilt 没有任何意义.

to get filter property instead of filters. You get undefined value. So the setting postData to postfilt means nothing.

下一个问题是服务器响应包含未过滤的数据.要强制过滤数据本地,您必须在从服务器加载完成后重新加载网格一次.您可以在 loadComplete 中执行此操作.正是在这里,您可以根据需要设置 postData.filter,设置 search: true 并触发 reloadGrid 事件.重要的是只执行此操作 once 以使没有递归,并且在这种情况下,您不得将 datatype 设置回 "json".datatype 将更改为 "local" 并在使用 loadonce: true 选项的情况下从服务器加载结束.如果你想应用过滤 locally 你必须重新加载网格一次,选项 datatype: "local", search: truepostDatafilters 指定应用需要的过滤器.请参阅 答案另一个它做另一件事,但你需要的代码将非常接近.

The next problem is that the server response contains non-filtered data. To force filtering the data locally you have to reload the grid once after the loading from the server have finished. You can do this inside of loadComplete. Exactly here you can set postData.filter if required, set search: true and trigger reloadGrid event. It's important only to do this once to have no recursion and you must don't set datatype back to "json" in the case. The datatype will be changed to "local" and the end of loading from the server in case of usage loadonce: true option. If you want apply filtering locally you have to reload grid once with options datatype: "local", search: true and postData having filters specifying the filter which need by applied. See the code from the answer or another one which do another things, but the code which you need will be very close.

这篇关于jqgrid在重新加载时保留过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆