We are using jquery grid 4.6. We want to set a select box
for filter toolbar. The select box should have columns unique name, this has already been answered and the solution could be found at: https://jsfiddle.net/4k0xgxok/
How ever when we change the data
from local
to url
the search box
will not appear at all. I tried to make a simple sample which only change the searchoptions:value
to some thing hard coded, but it does not work. https://jsfiddle.net/xdbw5hmy/
The code is
var getUniqueNames = function(columnName) {
var texts = $("#jqGrid").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;
};
var buildSearchSelect = function(uniqueNames) {
var values=":All";
$.each (uniqueNames, function() {
values += ";" + this + ":" + this;
});
console.log(values);
return values;
};
var mydata = [{
"OrderId": "1",
"CategoryName": "Beverages",
"ProductName": "Steeleye Stout"
}, {
"OrderId": "2",
"CategoryName": "Beverages",
"ProductName": "Laughing Lumberjack Lager"
}];
$("#jqGrid").jqGrid({
datatype: 'local',
data: mydata,
colModel: [{
label: 'OrderID',
name: 'OrderID',
key: true,
width: 75
}, {
label: 'ProductName',
name: 'ProductName',
width: 150
}, {
label: 'CategoryName',
name: 'CategoryName',
width: 150
}],
gridComplete: function() {
$('#jqGrid').jqGrid("setColProp", "ProductName", {
stype: "select",
align: "center",
searchoptions: {
value: buildSearchSelect(getUniqueNames("ProductName")),
}
});
},
viewrecords: true,
height: 250,
rowNum: 20,
});
$('#jqGrid').jqGrid('filterToolbar');
});