在jqgrid插件中实现'keypress'过滤的真正方法是什么? 我有文本输入字段,我需要通过自定义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;
});
答案 0 :(得分:2)
一个。将输入添加到模板中:
<input id="globalSearchText" class="x-campaigns-filter" type="text" placeholder="Filter by Name, Status or Advertiser">
湾自定义过滤规则。 search:false表示在此字段中禁用搜索。
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: '100%',
resizable: false,
rowNum: 50,
groupColumnShow: false
});
$("#globalSearchText").keyup(function () {
var rules = [], i, cm,
postData = grid.jqGrid("getGridParam", "postData"),
colModel = grid.jqGrid("getGridParam", "colModel"),
searchText = $("#globalSearchText").val(),
l = colModel.length;
for (i = 0; i < l; i++) {
cm = colModel[i];
if (cm.search !== false && (typeof cm.stype === "undefined" || cm.stype === "text")) {
rules.push({
field: cm.name,
op: "cn",
data: searchText
});
}
}
$.extend (postData, {
filters: {
groupOp: "OR",
rules: rules
}
});
grid.jqGrid("setGridParam", { search: true, postData: postData });
grid.trigger("reloadGrid", [{page: 1, current: true}]);
return false;
});