1

我正在从 API 加载 JQGrid,我的网格数据结构之一是 JSON 元素,格式如下:

{"id":123,"name":"John Doe","username":"john.doe"}

数据正确显示在网格中,但是如果我尝试在工具栏搜索中输入,我可能没有得到匹配,因为 jqgrid 仍然将上述 JSON 存储为对象。

我的网格的截断版本如下:

$('#test').jqGrid({
...
loadonce: true,
datatype: 'local',
colModel: [
{name:'test', index:'test', label:'Test', formatter:customFormatter}
],
...
});

function customFormatter (cellvalue,options) {
                return cellvalue.name;
}

我发现这篇文章似乎解决了这个问题,但是我很难弄清楚如何将它用于 JSON 对象。加载网格后,我看不出为什么本地数据应该是字符串以外的任何内容(直到重新加载网格)。

4

2 回答 2

0

You should choose name property of colModel which corresponds the names of the properties in the JSON input. So it's native to use just

colModel: [
    {name: "name", label: "Name"}
    {name: "username", label: "User Name"}
]

If you do need to remap some names in the JSON input to another names in the colModel you should use jsonmap property. For example

colModel: [
    {name: "test1", label: "Name", jsonmap: "name"}
    {name: "test2", label: "User Name", jsonmap: "username"}
]

The usage of index different as name is not possible in case of usage of loadonce: true. It is not recommended to use values of name property which have . or any other meta-character. The restriction exist because the name property will be used to build id property of many elements of the grid.

于 2012-12-15T17:22:26.710 回答
0

在偶然发现文档中的 JSON 点符号并将其添加到索引之后,现在可以使用:

colModel: [
    {name:'test.name', index:'test.name', label:'Test'}
]
于 2012-12-15T14:48:24.397 回答