Reputation: 336
Suppose I have a column which contains multiple values e.g Lets say we have a table which contains teacher's Name & departments (comma separated). In which a teacher can belong to many departments.
I want to search for the rows which contains the department starting with the letter typed in search field.
Right now I am using bw filter which works for only the first entry in the comma separated list (as it takes whole list as a single string).
Upvotes: 1
Views: 1537
Reputation: 222017
jqGrid don't provide you any possibility to custom operation like "begin with" searching in comma-separated substrings. So one have to use standard operation. On the other side one can subclass methods of $.jgrid.from
object used by jqGrid during filtering. I posted already some other answers which demonstrates the technique. It's still not so easy. So I created the demo which demonstrates how to subclass startsWith
method of $.jgrid.from
so that it works in special way like you need, but only in special column (see the constant "departments"
in the code below)
var oldFrom = $.jgrid.from;
$.jgrid.from = function (source, initalQuery) {
var result = oldFrom(source, initalQuery),
old_startsWith = result.startsWith;
result.startsWith = function (f, v) {
if (f !== "departments") {
return old_startsWith.call(result, f, v);
}
var self = result, val = (v === null) ? f : v, length = val.toString().length;
self._append(
'jQuery.map(' +
self._getStr('jQuery.jgrid.getAccessor(this,\'' + f + '\')') +
'.split(","),function(item) {if (item.substr(0,' + length + ')===' +
self._getStr('"' + self._toStr(v) + '"') +
") { return item; }}).length > 0"
);
self._setCommand(self.startsWith, f);
self._resetNegate();
return self;
};
return result;
};
If one filter the column "departments" for the string "d2" one will see the following results
Upvotes: 1