I am applying client-side search to a jqGrid without the search dialog, but I need to perform the search with a custom search function or a more clever search operator, one of which will essentially do $.inArray([searchString],[searchField])
.
My grid has datatype: 'json'
and loadonce: 'true'
and does not use multisearch
. The relevant column I want to search has colModel: [ {name: 'ancestorIds', hidden: true } ]
and it will contain an array of ids like [1,2,26,42]
.
Based on this answer, I am initializing the single field search from a click event outside the grid, something like this:
var selectedId = $('#selectedId').val();
var $grid = $('#grid');
var postdata = $grid.jqGrid('getGridParam','postData');
$.extend(postdata, {
filters: '',
searchField: 'ancestorIds',
searchOper: 'cn',
searchString: selectedId
});
$grid.jqGrid('setGridParam', { search: true, postData: postdata });
$grid.trigger("reloadGrid", [{page: 1}]);
This works fine, except if selectedId
is 2, using searchOper: 'cn'
will incorrectly return rows which include 26 or 42. My question is, how can I modify the search to only return rows where the selectedId is an actual value of the array?
EDIT:
Given several sample rows with the following values for ancestorIds
, and a selectedId
value of 2...
[1,2,26,42]
[2]
[2,42]
[42]
[22]
...the code above is selecting every row, but I only want it to return the first three rows.