使用jsonstring数据类型加载jqgrid表

Loading a jqgrid table using jsonstring datatype

本文关键字:jqgrid 加载 数据类型 jsonstring 使用      更新时间:2023-09-26

我想用我从velocity生成的json字符串加载一个jqGrid表;这些是表格参数:

var gridParams = {
    datatype: "jsonstring", 
    data: content,
    pager: $('#pagernav'), 
    rowNum: 5000,
    rownumbers: true,
    rowList: [50,100,200,500,1000,2000],
    ignoreCase: true,
    colNames: columns,
    colModel: tableColModel,
}

当在控制台上打印时,内容和列的值为:

[{"ID":"7", "fname":"Bob", "addr":"18" }, {"ID":"8", "fname":"Sue" }]
["ID", "fname", "addr"]

事实上,网格是用正确的列创建的,但它是空的。。。我写的东西出了什么问题?谢谢你的回答。

首先,datatype: "jsonstring"的90%的使用都是在错误使用jqGrid的情况下进行的。您没有更详细地描述您最初的问题,但我建议您考虑使用datatype: "json"而不是datatype: "jsonstring"

如果您确实需要使用datatype: "jsonstring",那么您应该使用datastr选项而不是datatype: "local"中使用的data选项来提供输入数据。

来自content的数据真的是JSON字符串(typeof content === "string")还是将数据作为对象(typeof content === "string")?在我看来,在您的情况下,最好使用datatype: "local", data: contentdatatype: "local", data: $.parseJSON(content),而不是使用datatype: "jsonstring", datastr: content,,因为输入数据的格式不是datatype: "jsonstring"所需的标准格式(请参阅文档),因此为了能够成功读取数据,您必须提供相应的jsonReader(请参阅此处的示例)

无论如何,我建议您使用gridview: true选项,将pager: $('#pagernav')替换为pager: '#pagernav',并为'ID'列定义key: true属性。此外,你应该在问题的正文中始终包含colModelcolModel: tableColModel的文本指向代码$.jqGrid(myOptions)-如果没有显示所需的详细信息,这对于找到问题的解决方案非常重要。