JqGrid not able to bind JSON data with “dot”

筅森魡賤 提交于 2019-12-25 03:36:30

问题


I have a JSON response from server which I am trying to bind to JQgrid. However, the response is a JSON string has "dot" as a part of the object name. I am unable to get the JQGrid work with the "dot"

Here is the sample fiddle for the problem I am facing 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"  
});

As shown the fiddle, I am not able to bind the property "cluster.first" even while using jsonmap. Whereas if the property name is "location-latitude" the grid works fine.

Let me know what I am doing wrong.


回答1:


The reason is very easy. The property jsonmap will be ignored in case of usage datatype: "local" in jqGrid 4.6. I changed the behavior in free jqGrid (see the wiki). So one possible solution will be to use free jqGrid 4.8 or higher instead of jqGrid 4.6.

One more simple way to solve the problem will be usage of datatype: "jsonstring". You can verified

$("#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"
});

See http://jsfiddle.net/OlegKi/rpdfrb0L/5/. You can see additionally that I changed name property of all colModel items to have no dots inside. I recommend to follow the rule always.



来源:https://stackoverflow.com/questions/30255044/jqgrid-not-able-to-bind-json-data-with-dot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!