简体   繁体   中英

Searching for a value in a column, with multiple values, in Jqgrid

Suppose I have a column which contains multiple values eg 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).

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM