如何在页面重新加载时保留jqGrid中的搜索过滤器? [英] How can I preserve the search filters in jqGrid on page reload?

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

问题描述

我发现许多讨论与我需要的内容很接近,这个问题是最近的
- 如何设置postData。在jqGrid的请求中_search为true?

I found many discussions that were close to what I need, and this question is the closest - How can I set postData._search to true in the request in jqGrid?.

因为我正在努力解决几乎相同的问题,而且无法让它正常工作 - 我想要在jqGrid的初始加载期间设置搜索和过滤器 - 比如说,在页面重新加载,我将我的过滤器存储在会话中 - 我尝试了在Oleg的示例中找到的所有内容 - 它只是没有工作!

As I'm struggling with almost the same problem, and just can't get it working - I want to setup "search" and "filters" during the initial loading of the jqGrid - say, on the page reload, and I have my filters stored in the session - and I tried everything I found in Oleg's examples - it just doesn't work!

这就是我要做的事情 -

That's what I'm trying to do -

loadBeforeSend: function (xhr) {
    var grid = jQuery('#' + block_id);
    var postData = grid.jqGrid('getGridParam','postData');
    jQuery.extend(postData,{filters:MyFilters});
    grid.jqGrid('setGridParam', {search: true, postData: postData});
    console.log(grid.jqGrid('getGridParam','postData'));
}

控制台打印输出显示过滤器已就位,但_search仍然是false,即使没有过滤器也会发送实际的帖子:

The console printout shows that the filters are in place, but the _search is still false, and the actual Post gets sent even with no filters:

_search   false
block_id  report_block_1table
nd        1297451574526
page      1
rows      25
sidx      id
sord      desc

但是,如果我输入完全相同的代码 - 添加

However, if I put exactly the same code - with the addition of

grid.trigger("reloadGrid");

line - 进入某个按钮的onClickButton函数,然后单击按钮 - 一切正常;但我需要让它在页面重新加载上工作!

line - into some button's onClickButton function, and later click the button - everything works; but I need to make it work on "page reload"!

任何想法?这让我发疯了......

Any ideas? It's driving me crazy...

推荐答案

在我看来,你不是第一个提出同样问题的人。最近我询问了关闭问题(阅读许多评论的答案)。另一个旧答案,包括演示可能会回答你打开的一些问题。

It seems to me that you are not the first person who ask the same question. Recently I asked on the close question (read many comments to the answer). Another old answer including the demo could probably answer on some your opened questions.

使用 beforeRequest 的代码不能正常工作只是因为函数 beforeRequest 将在ajax之前立即出现问题通话和更改搜索参数太晚。此外,每次覆盖过滤器可能不是最好的主意。在这种情况下,用户将无法设置任何其他网格过滤器。

Your code using beforeRequest don't work just because the function beforeRequest will be caled immediately before the ajax call and the changing of the search parameter is too late. Moreover overwiting of filters everytime is probably not the best idea. In the case the user will not able to set any other grid filter.

所以我可以重申,您需要的解决方案的实现非常简单。您只需将jqGrid的 postData 参数的过滤器属性设置为您需要的过滤器,并设置另一个jqGrid参数搜索:true 另外。这就是全部!稍后,用户将能够打开高级搜索对话框并覆盖过滤器。用户还可以单击高级搜索对话框的重置按钮,并设置过滤器以清空字符串和搜索:false

So I can repeat, that the imlementation of the solution which you need is very simple. You should just set filters property of the postData parameter of jqGrid to the filter which you need and set another jqGrid parameter search:true additionally. It's all! Later the user will be able to open advance searching dialog and overwrite the filters. The user can also click on "Reset" button of the advance searching dialog and set filters to empty string and search:false.

为了更好地理解,我必须清楚如何在URL中使用搜索参数或其他一些jqGrid 。 jqGrid的参数 prmNames 定义参数名称作为URL的一部分或作为POST到服务器的数据的一部分发送。 prmNames 的默认值包含搜索:_ search和内部代码 populate 函数具有以下简化代码片段:

For better understanding I have to clear how search parameter or some other jqGrid will be used in the URL. There are parameter prmNames of jqGrid which defines the names of parameters send as a part of URL or as a part of data POSTed to the server. The default value of prmNames contain search:"_search" and the code of internal populate function used by jqGrid has the following simplified code fragment:

var prm = {}, pN=ts.p.prmNames;
if(pN.search !== null) {prm[pN.search] = ts.p.search;}
if(pN.nd !== null) {prm[pN.nd] = new Date().getTime();}
if(pN.rows !== null) {prm[pN.rows]= ts.p.rowNum;}
if(pN.page !== null) {prm[pN.page]= ts.p.page;}
if(pN.sort !== null) {prm[pN.sort]= ts.p.sortname;}
if(pN.order !== null) {prm[pN.order]= ts.p.sortorder;}
...
$.extend(ts.p.postData,prm);

其中

prmNames: {page:"page",rows:"rows", sort: "sidx",order: "sord", search:"_search",
           nd:"nd", id:"id",oper:"oper",editoper:"edit",addoper:"add",
           deloper:"del", subgridid:"id", npage: null, totalrows:"totalrows"}

所以设置 _search URL的参数应该设置搜索 jqGrid的参数。

So to set _search parameter of URL one should set search parameter of jqGrid.

查看以下演示。您可以使用 Fiddler 轻松验证rel =nofollow noreferrer> Firebug 页面中的jqGrid使用以下网址发送HTTP GET:

Look at the following demo. You can easy to verify using Fiddler of Firebug that the jqGrid from the page send HTTP GET with the following url:

http://www.ok-soft-gmbh.com/jqGrid/MultisearchFilterAtStart1.json?filters=%7B%22groupOp%22%3A%22AND%22%2C%22rules%22%3A%5B%7B%22field%22%3A%22invdate%22%2C%22op%22%3A%22gt%22%2C%22data%22%3A%222007-09-06%22%7D%2C%7B%22field%22%3A%22invdate%22%2C%22op%22%3A%22lt%22%2C%22data%22%3A%222007-10-04%22%7D%2C%7B%22field%22%3A%22name%22%2C%22op%22%3A%22bw%22%2C%22data%22%3A%22test%22%7D%5D%7D&_search=true&nd=1297508504770&rows=10&page=1&sidx=id&sord=asc

所以它完全符合你的需要。演示代码包含以下代码片段:

So it do exactly what you need. The code of the demo contain the following code fragment:

$("#list").jqGrid({
    url: 'MultisearchFilterAtStart1.json',
    datatype: "json",
    postData: {
        filters:'{"groupOp":"AND","rules":['+
                '{"field":"invdate","op":"gt","data":"2007-09-06"}'+
                ',{"field":"invdate","op":"lt","data":"2007-10-04"}'+
                ',{"field":"name","op":"bw","data":"test"}]}'
    },
    search:true,
    // ...
});

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

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