i have a page with a jqgrid on it with filter row at the top. I want to have a link on another page that loads this grid page but with a filter set on one of the columns. is that possible to do from a link or any other workaround people can suggest?
-
Please look at my updated answer. Is it now work in your code? – Oleg Aug 05 '10 at 21:05
-
Could you explain which kind of "filter row at the top" you mean? Do you mean filter toolbar (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:toolbar_searching) or not? – Oleg Aug 07 '10 at 17:32
-
@Oleg - yes.. i want programmatically to mimic typing a value in the filter toolbar and hitting enter – leora Aug 07 '10 at 17:38
4 Answers
the way i solved this was to pass in the following code:
var myfilter = { groupOp: "AND", rules: [] };
myfilter.rules.push({ field: "DataIssuesYN", op: "eq", data: "Y" });
and then in the jqGrid setup, I pass into postData:
postData: (myfilter) ? { filters: JSON.stringify(myfilter)} : {},

- 188,729
- 360
- 878
- 1,366
You can try to use dataInit
property of searchoptions
in the colModel
. This function has one parameter elem
. The $(elem)
will represent the input
html element which you can initialize with any data which you need.
UPDATED: Try to include following option in colModel in the description of the column where you want set the filter:
searchoptions:{
dataInit:function(elem){
$(elem).val("Test");
setTimeout(function(){
$(elem).focus().trigger({ type: 'keypress', charCode: 13 });
},500);
}
}
in this example I set "Test" text as the filter and simulate press enter key. I suppose that searchOnEnter
set to default value true
. The forwarding of the filter string (like "Test") is very depended on the structure of your program, but I hope that it can be easy implemented.
UPDATED 2: Probably there are different understanding how should be understand "a page with a jqgrid on it with filter row at the top". I read it like a setting of filter in the filter toolbar, because the filter toolbar will be added as a row on the top of grids rows. My solution live can be seen here Setting filter in the filter toolbar
-
The distinction here is whether or not the desired functionality just works on first load, or if it should work off of something repeatable like following a link with parameters as she described. – Raevik May 10 '12 at 15:44
-
@Brent: Look at [the comment](http://stackoverflow.com/questions/3311929/is-there-a-way-to-programatically-set-a-filter-in-jquery-jqgrid/#comment3573544_3311929) which describe what wanted **leora** originally at the question was asked. I posted later many other answers which used `postData`. You should take in the consideration the requirements of the user at the moment of asking of the question at the date of the question (and the answer) too. – Oleg May 10 '12 at 18:33
You can modify the url that jqGrid calls, and add the filter option to the querystring, then handle it on the server side.
$(link).click(function(){
$(".mygrid").jqGrid('setGridParam',{url:"server.php?useMyFilter=1"})
});

- 63
- 6
-
This is almost right. After you do this, you need to `$("#grid").trigger("reloadGrid");` – Craig Stuntz Aug 05 '10 at 12:24
solution 1.
prgramatically in javascript: use the hideCol and give it the column name or a set of columns [colnames,otherone] the jqGrid object Given a single colname, will hide that column with that name. Given an array of colnames [“name1”,”name2”], it will hide the columns with those names, 'name1' and 'name2', in the example. The names in colname or colnames must all be valid names from the colModel. Remember that this will not change the width of the column so you still have to change tthe colModel example:
colModel :[{name:'photo', index:'photo', width:605, sortable:false} , ... ]
<script>
jQuery("#grid_id").setGridParam({...}).hideCol("photo").trigger("reloadGrid");
</script>
solution 2: solution 1:
jQuery(document).ready(function(){
jQuery("#list").jqGrid({
url:'json.php?myfilter=columnname',
datatype: 'json',//or xml?
mtype: 'GET', //<!--important
colNames:['Banner','name', 'city','state','Zip Code','Country'],
colModel :[
{name:'photo', index:'photo', width:605, sortable:false} ,
then in json.php you can take the column key out of your array before printing it

- 812
- 1
- 16
- 32

- 11,078
- 2
- 68
- 79
-
oh the first solution is awesome I didn't know I was doing it on the serverside like your second solution but I just tried this and it works! – Aug 07 '10 at 06:32
-
I am not sure that one asked for such kind of filter. There are filter in the fillter toolbar (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:toolbar_searching). If one do want to set some kind of filters which exist outside of jqGrid it seems to me better to do this with respect of `postData` (see http://stackoverflow.com/questions/2928371/how-to-filter-the-jqgrid-data-not-using-the-built-in-search-filter-box/2928819#2928819). This works not only with HTTP GET but with HTTP POST also. Morevere if you build filter url manually it should be `'myfilter='+encodeURIComponent(columnname)` – Oleg Aug 07 '10 at 09:49