接缝的使用tableToGrid
仅在第一眼看上去就很好。你会尝试的越多,你就越会发现它会以错误的方式引导你。jqGrid 旨在将数据与其可视化表示区分开来 - 格式化。只有输入数据才能正确排序和过滤网格中的数据。
这是一个小演示,演示了我的意思:
由于数据的分离和数据的可视化,可以得到正确的排序和搜索/过滤:
对应的 JavaScript 代码很简单:
$(function () {
var d = [
{id: "r1", name: "Bob", age: 10, married: false },
{id: "r2", name: "Jim", age: 5, married: false },
{id: "r3", name: "Marry", age: 29, married: true }
];
$("#grid").jqGrid({
datatype: "local",
data: d,
colNames: ["Name", "Age", "Married"],
colModel: [
{name: "name", width: 220},
{name: "age", formatter: "integer", align: "right", sorttype: "integer", width: 90,
searchoptions: {sopt: ['eq','ne','lt','le','gt','ge'] }},
{name: "married", formatter: "checkbox", align: "center", width: 80,
stype: "select", searchoptions: { sopt: ['eq', 'ne'], value: ":Any;true:Yes;false:No" }}
],
sortname: "age",
sortorder: "desc",
ignoreCase: true,
height: "auto",
rowNum: 10000 // no paging of data
});
$("#grid").jqGrid("filterToolbar", {defaultSearch: "cn", searchOnEnter: false, stringResult: "true"});
});
更新:从服务器加载的 JSON 的使用也非常简单。演示演示了这一点。
代码是
$(function () {
$("#grid").jqGrid({
url: "Murali.json",
datatype: "json",
colNames: ["Name", "Age", "Married"],
colModel: [
{name: "name", width: 220},
{name: "age", formatter: "integer", align: "right",
sorttype: "integer", width: 50,
searchoptions: {sopt: ['eq','ne','lt','le','gt','ge'] }},
{name: "married", formatter: "checkbox", align: "center",
width: 80, stype: "select",
searchoptions: { sopt: ['eq', 'ne'], value: ":Any;true:Yes;false:No" }}
],
ignoreCase: true,
height: "auto",
rowNum: 10,
sortname: "name",
pager: "#pager",
loadonce: true,
jsonReader: {
repeatitems: false,
root: function (obj) { return obj; }
}
});
$("#grid").jqGrid("navGrid", "#pager", {add: false, edit: false, del: false});
$("#grid").jqGrid("filterToolbar",
{defaultSearch: "cn", searchOnEnter: false, stringResult: "true"});
});