JQGrid工具栏搜索:搜索列中的多个单词 [英] JQGrid Toolbar Searching: search for multiple words for a column
问题描述
我使用工具栏搜索.您可以搜索一列中的多个单词吗?分隔符是一个空格,应使用Like运算符进行搜索.结果,搜索应返回与搜索字符串中所有单词相遇的所有行,无论它们在行字段中的排列顺序如何.例如,有一列产品名称",我想查找所有具有产品名称的行,其中包含单词"lever"和单词"left".
I use the Toolbar Searching. Can you do a search for multiple words for a column? The delimiter is a space and the search should be done using the Like operator. As a result, the search should return all rows that have met all the words in the search string, no matter in what order they go in a field of row. For example there is a column "Product Name", I want to find all rows that have product name contains the word "lever" and contains the word "left."
推荐答案
一个有趣的问题!
我创建了演示,该演示演示了如何实现多单词搜索:
I created the demo which demonstrate how to implement multi-word searching:
相应的代码是:
$grid.jqGrid('filterToolbar', {
stringResult: true,
defaultSearch: "cn",
beforeSearch: function () {
modifySearchingFilter.call(this, ' ');
}
});
其中modifySearchingFilter
我是这样定义的:
var modifySearchingFilter = function (separator) {
var i, l, rules, rule, parts, j, group, str,
filters = $.parseJSON(this.p.postData.filters);
if (filters && typeof filters.rules !== 'undefined' && filters.rules.length > 0) {
rules = filters.rules;
for (i = 0; i < rules.length; i++) {
rule = rules[i];
if (rule.op === 'cn') {
// make modifications only for the 'contains' operation
parts = rule.data.split(separator);
if (parts.length > 1) {
if (typeof filters.groups === 'undefined') {
filters.groups = [];
}
group = {
groupOp: 'OR',
groups: [],
rules: []
};
filters.groups.push(group);
for (j = 0, l = parts.length; j < l; j++) {
str = parts[j];
if (str) {
// skip empty '', which exist in case of two separaters of once
group.rules.push({
data: parts[j],
op: rule.op,
field: rule.field
});
}
}
rules.splice(i, 1);
i--; // to skip i++
}
}
}
this.p.postData.filters = JSON.stringify(filters);
}
};
更新:免费jqGrid 支持
UPDATE: Free jqGrid supports Custom filtering searching Operation, which makes very easy the implementation of such scenarios like above.
这篇关于JQGrid工具栏搜索:搜索列中的多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!