Reputation: 33
I am using JQGrid (version 5.3.0) to display values from a dynamic table (that I don't know the column number and columns names in advance). I retreive the columns schema from an ajax call to the server, as well as a filter that needs to be applied to the grid, and create the JQGrid from there.
The problem I am facing is that I find no way to repopulate the filter toolbar correctly from the filter that is passed to the table.
The code is the following :
$.ajax({
url: {url to get columns and filter},
type: 'POST',
postData: { productID: 98 }
success: function (result) {
if (result) {
$('#grid').jqGrid({
url: {url to get data},
datatype: 'json',
search: true,
postData: { productID: 98, filters: result.filter },
myType: 'GET',
colModel: result.columns,
jsonReader: {
root: 'Data',
page: 'Page',
total: 'Total',
records: 'PageSize'
},
pager: $('#gridpager'),
rowNum: 25,
gridview: false,
afterInsertRow: function (rowid, rowdata, rowelem) {
var color = rowelem.Color;
if (color != 'white') {
$("tr.jqgrow#" + rowid).addClass("color-" + color);
}
},
}).filterToolbar({
groupOp: 'AND',
defaultSearch: "cn",
searchOnEnter: true,
searchOperators: true,
stringResult: true,
});
}
}
});
Here is an example of the Columns model:
[
{
"name":"DESCRIPTION",
"label":"Description",
"searchoptions":
{
"sopt": ["bw","cn","nc","ew","eq","ne","in"],
"attr":{"class":"glyphicon glyphicon-filter"}
}
},
{
"name":"SKU_CODE",
"label":"SKU Code",
"searchoptions":
{
"sopt":["bw","cn","nc","ew","eq","ne","in"],
"attr":{"class":"glyphicon glyphicon-filter"}
}
},
{...}
]
And here is the filter :
"{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"DESCRIPTION\",\"op\":\"bw\",\"data\":\"D\"}]}"
When executing this code, the data is loaded from my data source, the filter is applied, but the filter bar stays empty.
Is there a way to populate automatically the filter toolbar with values from the filter passed in parameter? I've already tried parsing the filter rules, and filling the inputs with matching values, but I could not find a way to restore the operator.
Upvotes: 3
Views: 962
Reputation: 3277
If you use the existing method without support for searchOperators your code should like this:
$("#grid").jqGrid({
...
}).filterToolbar({
groupOp: 'AND',
defaultSearch: "cn",
searchOnEnter: true,
searchOperators: true,
stringResult: true
}).refreshFilterToolbar({"filters": result.filter});
and if you use the version from github with support for searchOperators your code should be like this
$("#grid").jqGrid({
...
}).filterToolbar({
groupOp: 'AND',
defaultSearch: "cn",
searchOnEnter: true,
searchOperators: true,
stringResult: true
}).refreshFilterToolbar();
Note that we do not pass the filter parameter, which is get automatically.
In your case you can use rowattr event for this purpose. Please read more about it in the same documentation page. Do not forget to set gridview to true or with other words
...
rowNum: 25,
gridview: true,
rowattr: function (rowdata, rowelem) {
var color = rowelem.Color;
if (color != 'white') {
return { "class" : "color-" + color};
}
},
Upvotes: 1