【发布时间】:2014-12-23 12:54:21
【问题描述】:
在 jqgrid 插件中实现“按键”过滤的真正方法是什么? 我有文本输入字段,我需要通过自定义 jqgrid-table 列在此输入中过滤按键结果。
filterToolbar 方法为每一列添加了搜索字段,但我需要一个全局搜索字段来按三个自定义列进行过滤。
例如:
grid.jqGrid({
url: '/url/to/json',
datatype: 'json',
loadonce: true,
colModel: [
{ label: 'Last Modified', name: 'lastModified', width: 15, sorttype: 'date' },
{ label: 'Campaign Name', name: 'name', width: 35, sorttype: 'text' },
{ label: 'Camp ID', name: 'id', align: 'left', width: 10, sorttype: 'integer' },
{ label: 'Advertiser', name: 'advertiser', width: 15, sorttype: 'text' },
{ label: 'Status', name: 'status', width: 10, sorttype: 'text' },
{ label: 'Flight Dates', name: 'startDate', width: 15, sorttype: 'date' }
],
autowidth: true,
...
});
我需要按“名称”和“广告商”属性对表格进行实时排序。
UPD。我找到了answer,但它在我的 jqGrid 表中不起作用。 我的代码在这里:
var grid = $("#jqGrid");
grid.jqGrid({
url: '/reportingservice/api/cmp/tagCampaignList',
datatype: 'json',
loadonce: true,
colModel: [
{ label: 'Last Modified', name: 'lastModified', width: 15, sorttype: 'date', search: false },
{ label: 'Campaign Name', name: 'name', width: 35, sorttype: 'text', formatter: urlFormat },
{ label: 'Camp ID', name: 'id', align: 'left', width: 10, sorttype: 'integer', search: false },
{ label: 'Advertiser', name: 'advertiser', width: 15, sorttype: 'text' },
{ label: 'Status', name: 'status', width: 10, sorttype: 'text' },
{ label: 'Flight Dates', name: 'flightDates', width: 15, sorttype: 'date', search: false }
],
autowidth: true,
height: 500,
resizable: false,
rowNum: 50,
groupColumnShow: false,
pager: '#jqGridPager',
pgtext: '{0}',
toolbar: [true, "top"],
loadComplete: function () {
}
});
// live search
$('#t_' + $.jgrid.jqID(grid[0].id))
.append($("<div><label for=\"globalSearchText\">Global search in grid for: " +
"</label><input id=\"globalSearchText\" type=\"text\"></input> " +
"<button id=\"globalSearch\" type=\"button\">Search</button></div>"));
$("#globalSearchText").keypress(function (e) {
var key = e.charCode || e.keyCode || 0;
if (key === $.ui.keyCode.ENTER) { // 13
$("#globalSearch").click();
}
});
$("#globalSearch").button({
icons: { primary: "ui-icon-search" },
text: false
}).click(function () {
var rules = [], i, cm,
postData = grid.jqGrid("getGridParam", "postData"),
colModel = grid.jqGrid("getGridParam", "colModel"),
searchText = $("#globalSearchText").val(),
l = colModel.length;
console.log(searchText);
for (i = 0; i < l; i++) {
cm = colModel[i];
if (cm.search !== false && (cm.stype === undefined || cm.stype === "text")) {
rules.push({
field: cm.name,
op: "cn",
data: searchText
});
}
}
postData.filters = JSON.stringify({
groupOp: "OR",
rules: rules
});
grid.jqGrid("setGridParam", { search: true });
grid.trigger("reloadGrid", [
{page: 1, current: true}
]);
return false;
});
【问题讨论】:
-
你应该更清楚地说明你做什么和你需要什么。可能使用
filterToolbar(Toolbar Searching) 和searchOnEnter: false选项已经可以解决您的问题。 -
filterToolbar 为每一列添加了搜索字段,我需要一个全局搜索字段来过滤三个自定义列
-
大概the answer提供你需要的解决方案?
-
非常感谢!我读了。
-
不客气!我建议您改进问题的文本(点击文本下方的“编辑”)。如果您不这样做,问题可能会被关闭,并且您在 stackoverflow 上发布下一个问题时可能会遇到更多问题。