代码之家  ›  专栏  ›  技术社区  ›  learnerplates

如何在jqgrid中使用json字符串或json对象?

  •  8
  • learnerplates  · 技术社区  · 15 年前

    当JSON数据在静态文件中时,我的jqgrid可以工作,但是如果我将数据复制到var,然后尝试将var加载到jqgrid的URL中,它就不会显示出来。

    你能把一个字符串传给jqgrid吗

    例如 这工作:

    function GetJSON() {
        var jsonFile = "EntityWithChildren.json";
        return jsonFile;//returning a file works fine.
    }
    
    $("#jsonmap").jqGrid({
        url: GetJSON(),
        datatype: 'json',
    

    这不是:

    function GetJSON() {
        var json = '{"page":"1","total":"10",   "records":"10", "Entities": [       {"Fields":["Entity1", "field1", "11"]},     {"Fields":["", "field2", "22"]},        {"Fields":["Entity2", "field3", "33"]},     {"Fields":["ChildEntity1", "cfield1", "111"]}   ]}';
        return json; //doesnt work
    
    }
    
    $("#jsonmap").jqGrid({
        url: GetJSON(),
        datatype: 'json',
        //datatype: 'jsonstring',//this doesnt work either
    
    1 回复  |  直到 15 年前
        1
  •  16
  •   learnerplates    15 年前

    知道了。 需要使用datastr而不是url

    datatype: 'jsonstring',
    datastr: GetJSON(),