What is the true way to implement 'keypress'-filtering in jqgrid plugin? I have text input field and i need filter results on keypress in this input by custom jqgrid-table columns.
filterToolbar method added search field for each column, but i need to one global search field for filtering by three custom columns.
For example:
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,
...
});
I need to live sort the table by 'name' and 'advertiser' attributes.
UPD. I found the answer, but it does not work in my jqGrid table. My code here:
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;
});