JqGrid 无法将 JSON 数据与 "dot" 绑定
JqGrid not able to bind JSON data with "dot"
我收到来自服务器的 JSON 响应,我正尝试将其绑定到 JQgrid。但是,响应是 JSON 字符串,其中 "dot" 作为对象名称的一部分。我无法让 JQGrid 与 "dot"
一起工作
这是我面临的问题的示例 fiddle http://jsfiddle.net/sharathchandramg/rpdfrb0L/2/
$("#grid").jqGrid({
data: data,
datatype: "local",
height: 250,
colNames: ['Name', 'Cluster', 'Location'],
colModel: [{
name: 'name',
width: 120
}, {
name: 'metrics.cluster.first.value',
width: 60,
jsonmap: function (obj) {
return obj.metrics['cluster.first'].value
}
}, {
name: 'metrics.location-latitude.value',
width: 60
}, ],
caption: "Example"
});
如 fiddle 所示,即使使用 jsonmap,我也无法绑定 属性 "cluster.first"。而如果 属性 名称是 "location-latitude",则网格工作正常。
让我知道我做错了什么。
原因很简单。在 jqGrid 4.6 中使用 datatype: "local"
时 属性 jsonmap
将被忽略。我更改了 free jqGrid (see the wiki) 中的行为。因此,一种可能的解决方案是使用免费的 jqGrid 4.8 或更高版本而不是 jqGrid 4.6。
解决问题的一种更简单的方法是使用 datatype: "jsonstring"
。您可以验证
$("#grid").jqGrid({
datastr: data,
datatype: "jsonstring",
height: "auto",
colNames: ['Name', 'Cluster', 'Location'],
colModel: [{
name: 'name',
width: 120
}, {
name: 'metrics_cluster_first_value',
width: 60,
jsonmap: function (obj) {
return obj.metrics['cluster.first'].value
}
}, {
name: 'metrics_location_latitude_value',
jsonmap: 'metrics.location-latitude.value',
width: 60
}],
caption: "Example"
});
参见 http://jsfiddle.net/OlegKi/rpdfrb0L/5/。您还可以看到,我将所有 colModel
项中的 name
属性 更改为内部没有点。我建议始终遵守规则。
我收到来自服务器的 JSON 响应,我正尝试将其绑定到 JQgrid。但是,响应是 JSON 字符串,其中 "dot" 作为对象名称的一部分。我无法让 JQGrid 与 "dot"
一起工作这是我面临的问题的示例 fiddle http://jsfiddle.net/sharathchandramg/rpdfrb0L/2/
$("#grid").jqGrid({
data: data,
datatype: "local",
height: 250,
colNames: ['Name', 'Cluster', 'Location'],
colModel: [{
name: 'name',
width: 120
}, {
name: 'metrics.cluster.first.value',
width: 60,
jsonmap: function (obj) {
return obj.metrics['cluster.first'].value
}
}, {
name: 'metrics.location-latitude.value',
width: 60
}, ],
caption: "Example"
});
如 fiddle 所示,即使使用 jsonmap,我也无法绑定 属性 "cluster.first"。而如果 属性 名称是 "location-latitude",则网格工作正常。
让我知道我做错了什么。
原因很简单。在 jqGrid 4.6 中使用 datatype: "local"
时 属性 jsonmap
将被忽略。我更改了 free jqGrid (see the wiki) 中的行为。因此,一种可能的解决方案是使用免费的 jqGrid 4.8 或更高版本而不是 jqGrid 4.6。
解决问题的一种更简单的方法是使用 datatype: "jsonstring"
。您可以验证
$("#grid").jqGrid({
datastr: data,
datatype: "jsonstring",
height: "auto",
colNames: ['Name', 'Cluster', 'Location'],
colModel: [{
name: 'name',
width: 120
}, {
name: 'metrics_cluster_first_value',
width: 60,
jsonmap: function (obj) {
return obj.metrics['cluster.first'].value
}
}, {
name: 'metrics_location_latitude_value',
jsonmap: 'metrics.location-latitude.value',
width: 60
}],
caption: "Example"
});
参见 http://jsfiddle.net/OlegKi/rpdfrb0L/5/。您还可以看到,我将所有 colModel
项中的 name
属性 更改为内部没有点。我建议始终遵守规则。