58

I want users to be able to filter grid data without using the intrinsic search box.

I have created two input fields for date (from and to) and now need to tell the grid to adopt this as its filter and then to request new data.

Forging a server request for grid data (bypassing the grid) and setting the grid's data to be the response data wont work - because as soon as the user tries to re-order the results or change the page etc. the grid will request new data from the server using a blank filter.

I cant seem to find grid API to achieve this - does anyone have any ideas? Thanks.

Jimbo
  • 20,449
  • 37
  • 111
  • 151
  • Revisa este enlace (check this link) http://stackoverflow.com/questions/17179777/search-with-autocomplete-in-codeigniter-and-jqgrid/17195094#17195094 – Rudolph Fentz Jun 19 '13 at 15:43

3 Answers3

80

You problem can be very easy solved with respect of postData parameter including functions and trigger('reloadGrid'). I try explain the idea more detailed.

Let us use mtype: "GET". The only thing which standard search/filter box do after displaying the interface is appending of some additional parameters to the url, sending to server and reloading the grid data. If you have your own interface for searching/filtering (some select controls or checkboxes, for example) you should just append your url yourself and reload the grid with respect of trigger('reloadGrid'). For resetting of the information in the grid you can choose any way which you prefer. For example, you can include in the select controls which you have an option like "no filtering".

To be more exact your code should looks like the code from the answer how to reload jqgrid in asp.net mvc when i change dropdownlist:

var myGrid = jQuery("#list").jqGrid({
    url: gridDataUrl,
    postData: {
        StateId: function() { return jQuery("#StateId option:selected").val(); },
        CityId: function() { return jQuery("#CityId option:selected").val(); },
        hospname: function() { return jQuery("#HospitalName").val(); }
    }
    // ...
});
//
var myReload = function() {
    myGrid.trigger('reloadGrid');
};
var keyupHandler = function (e,refreshFunction,obj) {
    var keyCode = e.keyCode || e.which;
    if (keyCode === 33 /*page up*/|| keyCode === 34 /*page down*/||
        keyCode === 35 /*end*/|| keyCode === 36 /*home*/||
        keyCode === 38 /*up arrow*/|| keyCode === 40 /*down arrow*/) {

        if (typeof refreshFunction === "function") {
            refreshFunction(obj);
       }
    }
};

// ...
$("#StateId").change(myReload).keyup(function (e) {
    keyupHandler(e,myReload,this);
});
$("#CityId").change(myReload).keyup(function (e) {
    keyupHandler(e,myReload,this);
});

If user change selected option in select box with id=StateId or another select box with id=CityId (with mouse or keyboard), the function myReload will be called, which just force reloading of data in jqGrid. During corresponding $.ajax request, which jqGrid do for us, the value from postData parameter will be forwarded to $.ajax as data parameter. If some from properties of data are functions, these function will be called by $.ajax. So the actual data from select boxes will be loaded and all the data will be appended to the data sent to the server. You need only implement reading of this parameters in your server part.

So the data from the postData parameter will be appended to the url (symbols '?' and '&' will be added automatically and all special symbols like blanks will be also encoded as usual). The advantages of the usage of postData is:

  1. usage of functions inside postData parameter allows you to load actual values from all used controls to the moment of reloading;
  2. Your code stay have very clear structure.
  3. All works fine not only with HTTP GET requests, but with HTTP POST also;

If you use some "no filtering" or "all" entries in the select box StateId, you can modify the function which calculate the value of StateId parameter which should appended to the server url from

StateId: function() { return jQuery("#StateId option:selected").val(); }

to something like following:

StateId: function() {
    var val = jQuery("#StateId option:selected").val();
    return val === "all"? "": val;
}

On the server side you should makes no filtering for StateId if you receive an empty value as a parameter.

Optionally you can use myGrid.setCaption('A text'); to change a grid title. This allow user to see more clear, that the data in grid are filtered with some criteria.

Community
  • 1
  • 1
Oleg
  • 217,934
  • 30
  • 386
  • 757
  • @Oleg, can i override the default `postData` functions with a `grid.setGridParam`? I'm trying to do this, but with no success. It appears that even though i set a param through `grid.setGridParam` it stills get the default `postData` function i defined previously. – AdrianoRR Apr 16 '12 at 12:53
  • @AdrianoRR: Probably you should write a new question where you describe the problem which you want to solve. What you need? Do you want that some parameters will be not send to the server? Do you need custom formatting of existing parameters (like converting to JSON)? Or you probably don't want to send any additional parameters to the server? – Oleg Apr 16 '12 at 14:26
  • @Oleg, by seeing some other posts of your i manage to get my own answer. By using `grid.getGridParam` i can get postData attributes and set them which any values i want. Then all i have to do is call `grid.trigger("reloadGrid");` and the changes are done. Anyway, thanks for your S.O. posts! – AdrianoRR Apr 16 '12 at 15:21
  • @AdrianoRR: You are welcome! Sorry, but I still don't understand what you want to do. Do you want to post an **additional** custom parameter or to *change in some way an existing standard parameter*? I suggested you to ask new question to give you more place to describe your problem in details and to explain what you need to implement, what information you need to send to the server and so on. – Oleg Apr 16 '12 at 15:27
  • hi mr Oleg, i have problem in jqgrid searching i ask question in this address, please check my question, thanks http://stackoverflow.com/questions/12309625/search-in-jqgrid – Pouya Sep 07 '12 at 09:43
  • @Oleg , I don't understand myGrid.trigger('reloadGrid'); works fine, but $("#list").jqGrid.trigger('reloadGrid') gives huge list of errors with 'method trigger not defined for...' at the end. Thanks for being so helpful in SO – abhihello123 Aug 23 '13 at 09:59
  • 1
    @abhihello123: You are welcome! The usage `$("#list").jqGrid.trigger('reloadGrid')` is definitively wrong. The expression `$("#list").trigger('reloadGrid')` is correct. The method [trigger](http://api.jquery.com/trigger/) is common **jQuery method** which you can apply on any jQuery objects. `$("#list")` or `myGrid` are jQuery objects so you can use `trigger` method on the objects. The expression `$("#list").jqGrid` is the *method* applied on `$("#list")` object. You can use `$("#list").jqGrid({...})` or `$("#list").jqGrid("jqGridMethid", ...)`, but not `$("#list").jqGrid.trigger(...)`. – Oleg Aug 23 '13 at 11:31
2

I had the same requirements, and (with Oleg's help) came up with this:

enter image description here

Basically, the user starts typing in the "Employee name" textbox, and immediately the results get shown in the jqGrid.

Details of how I implemented this are here:

jqGrid - Change filter/search pop up form - to be flat on page - not a dialog

Note that I specifically load all of the JSON data for my jqGrid in advance, and that for large data sets, there is a delay when running this code on an iPhone/Android device as you type each character.

But, for desktop web apps, it's a great solution, and makes jqGrid much friendler for the user.

Community
  • 1
  • 1
Mike Gledhill
  • 23,658
  • 6
  • 133
  • 143
0

By using ReloadGrid() and jquery functions you can make your own custom filtering functions.

J. Chomel
  • 7,605
  • 14
  • 37
  • 61