javascript - I need sme help automating jqGrid filters, please -
okay, in nutshell, need automatically apply set of sorting criteria , data filters jqgrid when loads. intent user start 10 pre-filled filters , then, if choose, can alter filters or sorting see fit.
so far, google-ing, trial , error , sweat, have following working:
-> can load/save sort column & sort order in session cookie.
-> can load search dialog pre-defined search filters. after grid loads, can open modal dialog , see proper filters , if click "find" appropriate data posted server , right results returned screen.
the thing biting me in butt right is, think, easy part, escapes me. can't seem either of following:
( ) ideal thing if attach these filters grid , it's post data in advance of initial load there single trip server.
( b ) workable solution, though less ideal, grid load first page of unfiltered data first, , apply filters , re-query server filtered data.
since can manually click "find" button today , works, thought "b" next-step. so, in gridcomplete function, have following code:
$('#accountgrid').clearfilter({gridname:'accountgrid', pagername:'accountpager'}); $('#accountgrid').addfilter({gridname:'accountgrid', field:'accountid', data:1, op:'ne'}); $('#accountgrid').addfilter({gridname:'accountgrid', field:'accountid', data:3, op:'ne'}); // $('#fbox_accountgrid').searchfilter().search(); // $('#fbox_accountgrid .ui-search').click(); $('#accountgrid').trigger('reloadgrid'); note: "clearfilter" , "addfilter" extension functions have added jqgrid simplify adding , removing filters on grid. work desired @ point. can see last 3 lines of code, have tried using built-in function, going after find button directly , forcing entire grid refresh. either way, there no attempt grid go new data (i using fiddler watch it). doing wrong in trying force grid reload new filters? and, if know how, can give me direction on how initial load of grid respect these filters? tia tony here full grid configuration (minus columns , colmodel "cruft"): jquery('#accountgrid').jqgrid({ url: '<my url>', width: 950, height: 330, shrinktofit: 'true', datatype: 'json', mtype: 'post', multiselect: true, multiboxonly: true, multiselectwidth: 20, colnames: [ 'account id' ], colmodel: [ { name: 'accountid', index: 'accountid', sortable: false, hidden:false, search:true } ], gridcomplete: function () { // add search criteria grid if (initialload == true){ $('#accountgrid').clearfilter({gridname:'accountgrid', pagername:'accountpager'}); $('#accountgrid').addfilter({gridname:'accountgrid', field:'accountid', data:1, op:'ne'}); $('#accountgrid').addfilter({gridname:'accountgrid', field:'accountid', data:3, op:'ne'}); // $('#fbox_accountgrid').searchfilter().search(); // $('#fbox_accountgrid .ui-search').click(); $('#accountgrid').trigger('reloadgrid'); initialload = false; } }, jsonreader: { repeatitems: false, id: 'accountid' }, pager: jquery('#accountpager'), rownum: 50, rowlist: [10, 15, 25, 50, 75, 100], onsortcol : function (sortname, indexcolumn, sortorder){ $.cookie('accountgrid_sortname', sortname); $.cookie('accountgrid_sortorder' , sortorder); }, sortname : $.cookie('accountgrid_sortname') ? $.cookie('accountgrid_sortname') : 'accountid', sortorder: $.cookie('accountgrid_sortorder') ? $.cookie('accountgrid_sortorder') : 'asc', viewrecords: true, imgpath: '' }); $('#accountgrid').jqgrid('navgrid','#accountpager', { view: false, add: false, edit: true, del: false, alertcap:'no account selected', alerttext: 'please select account grid before performing operation.', editfunc: showaccounteditdialog }, {}, // default settings edit {}, // default settings add {}, // delete {closeonescape: true, multiplesearch: true, closeaftersearch: true }, // search options {} );
and, request, here code have add/clear filter:
/* grid extension function insert new filter criteria on specified grid provided field, operation & data values */ (function ($) { jquery.jgrid.addsearchfilter = { // get/set parameters addfilter: function (options) { var grid = $(this); // offset values or assign defaults var settings = $.extend({ gridname: '', field: '', data: '', op: '' }, options || {}); // column model object grid matches provided name var colmodel = grid.getgridparam('colmodel'); var column; (var = 0; < colmodel.length; i++) { if (colmodel[i].name == options.field){ column = colmodel[i]; break; } } colmodel = null; if (column){ // if last filter has value, need create new 1 , not overwrite existing ones if ($('#fbox_' + options.gridname + ' .sf .data input').last().val()){ $('#fbox_' + options.gridname).searchfilter().add(); } // assign selections search dialog $('#fbox_' + options.gridname + ' .sf .fields select.field').last().val(column.index).change(); $('#fbox_' + options.gridname + ' .sf .data input').last().val(options.data); $('#fbox_' + options.gridname + ' .sf .ops select.default').last().val(options.op).change(); } } } })(jquery); jquery.fn.extend({ addfilter: jquery.jgrid.addsearchfilter.addfilter }); /* grid extension function clear & reset filter criteria */ (function ($) { jquery.jgrid.clearsearchfilter = { // get/set parameters clearfilter: function (options) { var grid = $(this); // offset values or assign defaults var settings = $.extend({ gridname: '', pagername: '' }, options || {}); // clear filters , "pop" dialog force html rendering $('#fbox_' + options.gridname).searchfilter().reset(); $('#' + options.pagername + ' .ui-icon-search').click(); $('#fbox_' + options.gridname).searchfilter().close(); } } })(jquery); jquery.fn.extend({ clearfilter: jquery.jgrid.clearsearchfilter.clearfilter });
first of because don't post code define jqgrid make assumption myself. try base on indirect information question.
1) suppose use server side datatype
parameter of jqgrid 'json' or 'xml'. 2) don't use loadonce:true
parameter. in general if possible make server reload grid having loadonce:true
, in case have reset value of datatype
parameter initial value (one value 'json' or 'xml').
the following 3 old answer: this (in case of single value searching) , this (in case of advanced searching or the toolbar searching additional parameter stringresult:true
) give enough information setting searching filters , reloading grid corresponds new filters. another answer shows how clear searching filter if no more needed.
loading of grid @ first time filters question. can easy implemented. should use datatype:"local"
instead of datatype:"json"
or datatype:"xml"
need. in case url
parameter of jqgrid ignored @ first load , jqgrid send no request server. set filters need , use $("#yougridid").trigger("reloadgrid");
the reason why reloadgrid
didn't work in case not know exactly, suppose didn't set search:true
parameter of jqgrid 1 confuses _search
property of postdata
parameter (see here).
Comments
Post a Comment