jqGrid advanced search 'beforeSearch' option?
I am using the most excellent jqGrid plugin, and have found lots of help on this site by searching, but I have found a problem I cannot solve, or find a solution to. This will be my first post here.
I am using a filterToolbar to do some searching of my grid. Because of the nature of the back end I need to interact with, I am unanble to use the filters that jqGrid provides, and instead need to intercept the search and modify the postdata before submitting. I do this with the filterToolbar option "beforeSearch" like this:
$("#SC_grid").jqGrid('filterToolbar', {stringResult: true, searchOnEnter: true, defaultSearch : "cn", beforeSearch: function() {
var postData = $("#SC_grid").jqGrid('getGridParam','postData');
var newPostData = '1=1';
var searchData = jQuery.parseJSON(postData.filters);
开发者_如何学运维 for (var iRule=0; iRule<searchData.rules.length; iRule++) {
newPostData = newPostData + " AND " + searchData.rules[iRule].field + " LIKE '%" + searchData.rules[iRule].data + "%' ";
}
$("#SC_grid").jqGrid('setGridParam',{postData: { filter: newPostData, filters: ''} } );
return false;
}});
This works great for me to build a portion of my select before submission. I would also like to use the advanced search in the same way, but Cannot figure out how to intercept the POST before submission. There does not appear to be a beforeSearch() option abailable, and the afterShowSearch or onClose options dont have the right timings. ANy suggestions on how to proceed?
Mark
You are right, the current implementation of advanced searching of the jqGrid don't have any events like beforeSearch
. The new version of advanced searching which are full new written will have onSearch
method which could you use. The method like beforeSearch
will be fired before trigger("reloadGrid",[{page:1}])
, but after the postData
will be filled.
The beforeSearch
of the filterToolbar
has one more interesting feature so you can stop the searching by returning the true
value from the beforeSearch
. So the beforeSearch
can play validation role.
In one my old answer I described an universal method which can be used for any kind of validation or postData
modification. In the answer I show how one can subclass the reloadGrid
event handler of jqGrid and stop reloading of the grid if needed. The demo shows this. In the same way here one could do any other modification of the data. It's pity, but the answer will be very bad indexed and will not be fond on the most searching on the stackoverflow. So one don't know it.
In you case you need only to modify the postdata before submitting and don't need to stop the grid reloading. So you can solve the problem using at least two standard jqGrid events: beforeRequest and serializeGridData. In both you can access the search
(as this.p.search
) and postData
(as this.p.postData
) parameters. The value of search
parameter will be send to the server as _search
and will be set to true
if any of searching/filtering method are used. So inside of one from the event handler you can modify this.p.postData
.
Inside of serializeGridData you have even a way to define which data exactly will be send to the server without require to modify the data. The advantage is that if you open the advance searching dialog next time you will see (and can modify if needed) the last searching request.
If you need to have the same implementation of beforeRequest or serializeGridData on many grids of your web site you can set your default implementation of the function with respect of $.jgrid.defaults
:
$.extend($.jgrid.defaults, {
datatype: 'json', // overwrite default value of any jqGrid parameter
serializeGridData: function(postData) {
// your implementation
}
});
精彩评论