5

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

driedger
  • 230
  • 1
  • 4
  • 9
  • Welcome on the stackoverflow with very good first question. +1 from me for the question. In a short time I will try to describe a possible way to solve you problem. Till later... – Oleg Mar 22 '11 at 20:07

1 Answers1

4

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
         }
});
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • 1
    Thank you for your response on this subject. I have learned much from the demo's that you create for your answers, and it is greatly appreciated. Im sure I will be able to work my way through this one as well with your demos. If all else fails, it is good to know that the upcoming version will add a method to deal with this problem. – driedger Mar 23 '11 at 12:32
  • is there a way to cycle only on column where search is true? – frabiacca Mar 21 '13 at 17:14
  • @frabiacca: Sorry, but I don't understand what you mean. which cycle you mean? – Oleg Mar 21 '13 at 17:18
  • @oleg if I have 10 columns declared in my ColModel, but just 3 of those are searchable (I mean with search:true property), is it possible to make a "for" cycle on those 3 columns? – frabiacca Mar 21 '13 at 17:22
  • I asked this question 'cause postData.filters is undefined in my grid :( – frabiacca Mar 21 '13 at 17:23
  • @frabiacca: Sorry, but I don't understand context of your question. You posted comment to my answer where no loop is included. So I still don't understand your problem. `postData.filters` are typically filled by jqGrid and jqGrid allows to set filters only on searchable fields. So I don't understand what you mean, which code you use and in which situation (in which context). – Oleg Mar 21 '13 at 17:35