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?.
我发现很多讨论都接近于我的需要,而这个问题是最接近的——我如何设置postData。_search在jqGrid中的请求中为true ?
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!
当我在几乎同样的问题,就不能得到它的工作,我想设置“搜索”和“过滤器”的初始加载jqGrid——比如说,在加载页面,和我有过滤器存储在会话中,我尝试一切我发现奥列格的例子,它只是不工作!
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'));
}
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仍然是假的,即使没有过滤器,实际的Post也会被发送:
_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 - into some button's onClickButton function, and later click the button - everything works; but I need to make it work on "page reload"!
行-进入一些按钮的onClickButton函数,然后点击按钮-一切正常;但是我需要让它在“页面重载”上工作!
Any ideas? It's driving me crazy...
什么好主意吗?我快疯了…
2 个解决方案
#1
8
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.
在我看来,你不是第一个问同样问题的人。最近我问了一个很接近的问题(读了很多关于答案的评论)。另一个旧的答案,包括演示可能会回答一些你的开放式问题。
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.
使用beforeRequest的代码不能正常工作,因为beforeRequest函数将在ajax调用之前立即被调用,并且更改搜索参数已经太晚了。此外,每次都过度使用过滤器可能不是最好的办法。在这种情况下,用户将无法设置任何其他网格过滤器。
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
.
我可以重复一遍,你所需要的解决方案的重要性是非常简单的。您应该将jqGrid的postData参数的过滤器属性设置为您需要的过滤器,并设置另一个jqGrid参数搜索:true。这是所有!稍后用户将能够打开预先搜索对话框并覆盖过滤器。用户还可以点击“重置”按钮,将过滤器设置为空字符串,搜索:false。
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:
为了更好地理解,我必须弄清楚如何在URL中使用search参数或其他jqGrid。jqGrid的参数prmNames定义了作为URL的一部分发送的参数的名称,或者作为发送到服务器的数据的一部分。prmNames的默认值包含搜索:“_search”,jqGrid使用的内部填充函数代码有以下简化代码片段:
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);
where
在哪里
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"}
So to set _search
parameter of URL one should set search
parameter of jqGrid.
因此,要设置URL的_search参数,应该设置jqGrid的搜索参数。
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:
请看下面的演示。使用Firebug的Fiddler,您可以很容易地验证来自页面的jqGrid使用以下url发送HTTP GET:
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,
// ...
});
#2
0
@Oleg Oleg's answer works like a charm but just for the first time.
@Oleg Oleg的回答很有魅力,但这还是第一次。
For me when I reload the grid, filters and search flag are not set up. With the following code each time I reload the grid it also sends the filters and search flag. I use server side sort and pagination.
当我重新加载网格的时候,过滤器和搜索标志没有设置。使用以下代码,每次重载网格时,它也发送过滤器和搜索标志。我使用服务器端排序和分页。
I'm using:
我用的是:
jQuery("#myGrid").navGrid("#myPager", {search: true, refresh: true, edit: false,
add:false, del:false}, {}, {}, {}, {});
On the grid definition:
在网格上定义:
beforeRequest: function() {
// filter to be added on each request
var filterObj1 = {"field":"myField","op":"eq","data":"myValue"};
var grid = jQuery("#myGrid");
var postdata = grid.jqGrid('getGridParam', 'postData');
if(postdata != undefined && postdata.filters != undefined ) {
postdata.filters = jQuery.jgrid.parse(postdata.filters);
//Remove if current field exists
postdata.filters.rules = jQuery.grep(postdata.filters.rules, function(value) {
if(value.field != 'myField')
return value;
});
// Add new filters
postdata.filters.rules.push(filterObj1);
} else {
jQuery.extend(postdata, {
filters: {
"groupOp":"AND",
"rules":[filterObj1]
}
});
// more filters in the way: postdata.filters.rules.push(filterObj1);
}
postdata.filters = JSON.stringify(postdata.filters);
postdata._search = true;
return [true,''];
}
#1
8
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.
在我看来,你不是第一个问同样问题的人。最近我问了一个很接近的问题(读了很多关于答案的评论)。另一个旧的答案,包括演示可能会回答一些你的开放式问题。
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.
使用beforeRequest的代码不能正常工作,因为beforeRequest函数将在ajax调用之前立即被调用,并且更改搜索参数已经太晚了。此外,每次都过度使用过滤器可能不是最好的办法。在这种情况下,用户将无法设置任何其他网格过滤器。
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
.
我可以重复一遍,你所需要的解决方案的重要性是非常简单的。您应该将jqGrid的postData参数的过滤器属性设置为您需要的过滤器,并设置另一个jqGrid参数搜索:true。这是所有!稍后用户将能够打开预先搜索对话框并覆盖过滤器。用户还可以点击“重置”按钮,将过滤器设置为空字符串,搜索:false。
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:
为了更好地理解,我必须弄清楚如何在URL中使用search参数或其他jqGrid。jqGrid的参数prmNames定义了作为URL的一部分发送的参数的名称,或者作为发送到服务器的数据的一部分。prmNames的默认值包含搜索:“_search”,jqGrid使用的内部填充函数代码有以下简化代码片段:
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);
where
在哪里
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"}
So to set _search
parameter of URL one should set search
parameter of jqGrid.
因此,要设置URL的_search参数,应该设置jqGrid的搜索参数。
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:
请看下面的演示。使用Firebug的Fiddler,您可以很容易地验证来自页面的jqGrid使用以下url发送HTTP GET:
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,
// ...
});
#2
0
@Oleg Oleg's answer works like a charm but just for the first time.
@Oleg Oleg的回答很有魅力,但这还是第一次。
For me when I reload the grid, filters and search flag are not set up. With the following code each time I reload the grid it also sends the filters and search flag. I use server side sort and pagination.
当我重新加载网格的时候,过滤器和搜索标志没有设置。使用以下代码,每次重载网格时,它也发送过滤器和搜索标志。我使用服务器端排序和分页。
I'm using:
我用的是:
jQuery("#myGrid").navGrid("#myPager", {search: true, refresh: true, edit: false,
add:false, del:false}, {}, {}, {}, {});
On the grid definition:
在网格上定义:
beforeRequest: function() {
// filter to be added on each request
var filterObj1 = {"field":"myField","op":"eq","data":"myValue"};
var grid = jQuery("#myGrid");
var postdata = grid.jqGrid('getGridParam', 'postData');
if(postdata != undefined && postdata.filters != undefined ) {
postdata.filters = jQuery.jgrid.parse(postdata.filters);
//Remove if current field exists
postdata.filters.rules = jQuery.grep(postdata.filters.rules, function(value) {
if(value.field != 'myField')
return value;
});
// Add new filters
postdata.filters.rules.push(filterObj1);
} else {
jQuery.extend(postdata, {
filters: {
"groupOp":"AND",
"rules":[filterObj1]
}
});
// more filters in the way: postdata.filters.rules.push(filterObj1);
}
postdata.filters = JSON.stringify(postdata.filters);
postdata._search = true;
return [true,''];
}