Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter jqGrid programmatically on client?

Is there a way to filter the data currently displayed in a jqGrid programmatically (in Javascript, not server-side)? All the search examples seem to depend on using jqGrid's own search UI, which doesn't work for me. For example, I'd like to be able to filter based on user actions elsewhere on a page.

I'm imagining something like

jQuery("#grid_id").filter('CategoryID', selectedCategoryID);

where CategoryID is a column in the grid and selectedCategoryID contains, for example, a value chosen by the user in a select element.

like image 728
Herb Caudill Avatar asked Jan 07 '10 02:01

Herb Caudill


1 Answers

If you want to pre-filter your data first:

$('#myGrid').setGridParam({ data: filtereddataarray }).trigger("reloadGrid");

where filtereddataarray contains only records you want to display for this view

If you want to construct your filter programmatically(I use this method, mostly):

var filters = { "groupOp": "AND", "rules": [{ "field": "id", "op": "eq", "data": "9" }, { "field": "amount", "op": "ge", "data": "10" }, { "field": "name", "op": "cn", "data": "do i"}] };

//To filter:
jqGridFilter(filters , $('#myGrid'));

//To reset: 
jqGridFilter(null, $('#myGrid'));

    function jqGridFilter(filtersparam, grid) {
        grid.setGridParam({
            postData: {
                filters: filtersparam
            },
            search: true
        });
        grid.trigger("reloadGrid");
    }
like image 114
Igor Avatar answered Sep 23 '22 06:09

Igor