Javascript 因为select2具有集成的搜索输入字段,并且在用户键入文本期间,所选数据将减少。查看来自的演示。我建议您另外阅读,这说明在处理之前,您写的“我们将数据从本地更改为url”。这似乎是不正确的。您的意思可能是将datatype:“local”,data:my
因为Javascript 因为select2具有集成的搜索输入字段,并且在用户键入文本期间,所选数据将减少。查看来自的演示。我建议您另外阅读,这说明在处理之前,您写的“我们将数据从本地更改为url”。这似乎是不正确的。您的意思可能是将datatype:“local”,data:my,javascript,jquery,jqgrid,Javascript,Jquery,Jqgrid,因为select2具有集成的搜索输入字段,并且在用户键入文本期间,所选数据将减少。查看来自的演示。我建议您另外阅读,这说明在处理之前,您写的“我们将数据从本地更改为url”。这似乎是不正确的。您的意思可能是将datatype:“local”,data:mydata更改为datatype:“json”,url:“someUrl”,loadonce:true。是这样吗? var getUniqueNames = function(columnName) { var texts =
select2
具有集成的搜索输入字段,并且在用户键入文本期间,所选数据将减少。查看来自的演示。我建议您另外阅读,这说明在处理之前,
您写的“我们将数据从本地更改为url”。这似乎是不正确的。您的意思可能是将datatype:“local”,data:mydata
更改为datatype:“json”,url:“someUrl”,loadonce:true
。是这样吗?
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');
});
var getUniqueNames = function(columnName, mydata) {
var texts = $.map(mydata, function(item) {
return item[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;
},
buildSearchSelect = function(uniqueNames) {
var values = "";
$.each(uniqueNames, function() {
values += this + ":" + this + ";";
});
values = values.slice(0, -1);
return values;
},
setSearchSelect = function(columnName, data) {
$(this).jqGrid("setColProp", columnName, {
stype: "select",
align: "center",
searchoptions: {
value: buildSearchSelect(getUniqueNames.call(this, columnName, data)),
sopt: ["eq", "ne"]
}
});
},
myDefaultSearch = "cn";
$("#jqGrid").jqGrid({
url: 'https://api.myjson.com/bins/42mdc',
datatype: 'json',
colModel: [
{ name: 'OrderID', key: true, width: 75 },
{ name: 'ProductName' },
{ name: 'CategoryName' }
],
beforeProcessing: function (data) {
// !!! it will be called only after loading from the server
// datatype is always "json" here
setSearchSelect.call(this, "ProductName", data.rows);
if (this.ftoolbar === true) {
// if the filter toolbar already exist: we have to recreate it
$(this).jqGrid('destroyFilterToolbar');
}
$(this).jqGrid("filterToolbar", {
stringResult: true,
defaultSearch: myDefaultSearch
});
},
viewrecords: true,
height: "auto",
autoencode: true,
rowNum: 20,
loadonce: true
});