gpt4 book ai didi

jqgrid列顺序

转载 作者:行者123 更新时间:2023-12-03 23:47:38 28 4
gpt4 key购买 nike

我有 jqgrid 从 json 服务读取数据

$('#list').jqGrid({
url: 'jsonservice',
datatype: 'json',
mtype: 'GET',
colNames: ['Id', 'Name', 'Street', 'City'],
colModel: [
{ name: 'Id', index: 'Id', width: 55, align: 'center', width: '25' }
{ name: 'Name', index: 'Name', width: 120 },
{ name: 'Street', index: 'Street', width: 90 },
{ name: 'City', index: 'City', width: 50 },
]
});

json服务返回这样的数据

{"page":1,
"total":37,
"records":722,
"rows":
[
{"id":1,"cell":[1, "Sample name 1","Sample Street 2","Sample City 3"]},
{"id":2,"cell":[2, "Sample name 2","Sample Street 2","Sample City 3"]}
]
}

如何将显示列的顺序更改为例如姓名、城市、街道、Id 不改变 json 数据中的顺序?

最佳答案

最简单的使用方法jsonReader在表格中

jsonReader: {repeatitems: false, id: "Id"}

如果表示行的数据应该是具有命名属性的对象:

{
"page": 1,
"total": 37,
"records": 722,
"rows": [
{"Id":1, "Name":"Sample name 1", "Street":"Sample Street 2", "City":"Sample City 3"},
{"Id":2, "Name":"Sample name 2", "Street":"Sample Street 2", "City":"Sample City 3"}
]
}

该格式的主要缺点是增加了传输数据的大小。然而,这将是解决您的问题的最简单方法。

另一种方法是将 repeatitems: falsejsonmap 结合使用。它允许指定如何从数据行中读取每一列的数据。可以为 jsonmap 使用带点的名称:

$('#list').jqGrid({
url: 'Marcin.json',
datatype: 'json',
colNames: ['Name', 'Street', 'City', 'Id'],
colModel: [
{ name: 'Name', width: 120, jsonmap: "cell.1" },
{ name: 'Street', width: 190, jsonmap: "cell.2" },
{ name: 'City', width: 90, jsonmap: "cell.3" },
{ name: 'Id', width: 55, align: 'center', jsonmap: "cell.0" }
],
height: "auto",
gridview: true,
jsonReader: { repeatitems: false, id: "Id" }
});

The corresponding demo看起来像

enter image description here

在更复杂的情况下,可以使用 jsonmap 作为函数,从表示行的对象中读取列的项目。例如,可以将 'Id' 列的定义重写为以下内容

{ name: 'Id', width: 55, align: 'center',
jsonmap: function (obj) { // "cell.0"
return obj.cell[0];
}}

The modified demo显示相同的结果。

关于jqgrid列顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14925351/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com