我正在使用jqgrid和工具栏过滤器。默认情况下,它只是为您提供了一个输入数据的文本框。它是否支持下拉选择组合框,我可以给它一个值列表供他们选择过滤器吗?
答案 0 :(得分:68)
jqGrid中所有类型的排序都有common rules
{
name: 'Category', index: 'Category', width: 200, formatter:'select',
stype: 'select', searchoptions:{ sopt:['eq'], value: categoriesStr }
}
其中categoriesStr
定义为
var categoriesStr = ":All;1:sport;2:science";
此外,标准“1:sport; 2:science”值插入“:All”字符串,允许您不过滤列。你当然可以使用“:”或“:选择......”等等。
在为the demo准备的the answer上,您可以看到结果非常接近。
更新:我发现您的问题很有趣并且the demo。它显示了如何构建可在搜索工具栏或高级搜索对话框中使用的选择组合框基于相应列的文本包含。对于一列,我另外使用jQuery UI autocomplete。您可以修改代码以使用自动完成的更多不同强大选项。这是代码的代码:
var mydata = [
{id:"1", Name:"Miroslav Klose", Category:"sport", Subcategory:"football"},
{id:"2", Name:"Michael Schumacher", Category:"sport", Subcategory:"formula 1"},
{id:"3", Name:"Albert Einstein", Category:"science", Subcategory:"physics"},
{id:"4", Name:"Blaise Pascal", Category:"science", Subcategory:"mathematics"}
],
grid = $("#list"),
getUniqueNames = function(columnName) {
var texts = grid.jqGrid('getCol',columnName), uniqueTexts = [],
textsLength = texts.length, text, textsMap = {}, i;
for (i=0;i<textsLength;i++) {
text = texts[i];
if (text !== undefined && textsMap[text] === undefined) {
// to test whether the texts is unique we place it in the map.
textsMap[text] = true;
uniqueTexts.push(text);
}
}
return uniqueTexts;
},
buildSearchSelect = function(uniqueNames) {
var values=":All";
$.each (uniqueNames, function() {
values += ";" + this + ":" + this;
});
return values;
},
setSearchSelect = function(columnName) {
grid.jqGrid('setColProp', columnName,
{
stype: 'select',
searchoptions: {
value:buildSearchSelect(getUniqueNames(columnName)),
sopt:['eq']
}
}
);
};
grid.jqGrid({
data: mydata,
datatype: 'local',
colModel: [
{ name:'Name', index:'Name', width:200 },
{ name:'Category', index:'Category', width:200 },
{ name:'Subcategory', index:'Subcategory', width:200 }
],
sortname: 'Name',
viewrecords: true,
rownumbers: true,
sortorder: "desc",
ignoreCase: true,
pager: '#pager',
height: "auto",
caption: "How to use filterToolbar better locally"
}).jqGrid('navGrid','#pager',
{edit:false, add:false, del:false, search:false, refresh:false});
setSearchSelect('Category');
setSearchSelect('Subcategory');
grid.jqGrid('setColProp', 'Name',
{
searchoptions: {
sopt:['cn'],
dataInit: function(elem) {
$(elem).autocomplete({
source:getUniqueNames('Name'),
delay:0,
minLength:0
});
}
}
});
grid.jqGrid('filterToolbar',
{stringResult:true, searchOnEnter:true, defaultSearch:"cn"});
这是你想要的吗?
更新:另一个选项可能是select2插件的使用,它结合了下拉菜单和自动填充功能的舒适搜索功能。有关演示(the answer和this one)和代码示例,请参阅this one和this one(请参阅演示)。
更新2 :The answer包含对上述代码的修改,以便与jqGrid 4.6 / 4.7或free jqGrid 4.8配合使用。
答案 1 :(得分:2)
我有类似的情况。感谢Oleg上面的优秀例子,它几乎解决了这个问题。我需要一点点改进。我的网格是一个loadonce网格,大约有40行,每页10行。上面使用的getCol方法只返回当前页面的列值。但我想在整个数据集中使用唯一值填充过滤器。所以这里稍微修改了函数getUniqueNames:
var getUniqueNames = function(columnName) {
// Maybe this line could be moved outside the function
// If the data is really huge then the entire segregation could
// be done in a single loop storing each unique column
// in a map of columnNames -> unique values
var data = grid.jqGrid('getGridParam', 'data');
var uniqueTexts = [], text, textsMap = {}, i;
for (i = 0; i < data.length; i++) {
text = data[i][columnName];
if (text !== undefined && textsMap[text] === undefined) {
// to test whether the texts is unique we place it in the map.
textsMap[text] = true;
uniqueTexts.push(text);
}
}
// Object.keys(textsMap); Does not work with IE8:
return uniqueTexts;
}
答案 2 :(得分:1)
我自己就是这么做的。感觉有点像黑客,但它的工作原理!
然后我创建了一个onchange函数来处理网格重载时的值 改变。
$('#myGrid').jqGrid('navButtonAdd', '#myGrid_toppager', {
caption: "<select id='gridFilter' onchange='ChangeGridView()'><option>Inbox</option><option>Sent Messages</option></select>",
title: "Apply Filter",
onClickButton: function () {
}
});
function ChangeGridView() {
var gridViewFilter = $("#gridFilter").val();
$('#myGrid').setGridParam({ datatype: 'json', url: '../../Controller/ActionJSON', postData: { msgFilter: gridViewFilter } });
$('#myGrid').trigger("reloadGrid");
};
希望这有帮助!
答案 3 :(得分:0)
类别是列名。
loadComplete: function () {
$("#" + TableNames).setColProp('Category', {
formatter: 'select', edittype: "select",
editoptions: { value: "0:MALE;1:FEMALE;2:other;" }
});
},