【问题标题】:jqGrid JSON Data Can't Shown On TablejqGrid JSON数据无法显示在表格上
【发布时间】:2017-07-30 21:34:13
【问题描述】:

我想在我的表中显示 JSON 数据,如下所示:example

我使用了那个 CSS 导入:

<link rel="stylesheet" href="/css/ui.jqgrid.css"/>
<link rel="stylesheet" href="/css/ui.multiselect.css"/>
<link rel="stylesheet" href="/css/jquery-ui-1.8.1.custom.css"/>

那个 JS 导入:

<script type=text/javascript src="/js/jquery.js"></script>
<script type=text/javascript src="/js/jquery_ui_1.8.1.js"></script>
<script type=text/javascript src="/js/jquery.layout.js"></script>
<script type=text/javascript src="/js/grid.locale-en.js"></script>
<script type="text/javascript">
    $.jgrid.no_legacy_api = true;
    $.jgrid.useJSON = true;
</script>
<script type="text/javascript" src="/js/ui.multiselect.js"></script>
<script type=text/javascript src="/js/jgrid_4.1.js"></script>
<script type="text/javascript" src="/js/jquery.tablednd.js"></script>
<script type="text/javascript" src="/js/jquery.contextmenu.js"></script>

(有些文件名称不同,但都可以)

我从 URL 获取 JSON 数据,当我检查它时,它在 Firebug 中正确显示。 这是我的 HTML 数据:

<table id="confTable"></table>
<div id="pconfTable"></div>

这是我填充数据的脚本:

jQuery("#confTable").jqGrid({ ajaxGridOptions : {type:"GET"}, serializeGridData : function(postdata) {
            postdata.page = 1;
            return postdata;
        }, url:'/ui/webapp/conf', datatype: 'json', colNames:['Value','Type', 'Target Module', 'Configuration Group', 'Name', 'Description', 'Identity', 'Version', 'System Id', 'Active'],
            colModel:[
                {name:'value',index:'value', width:55},
                {name:'type',index:'type', width:55},
                {name:'targetModule',index:'targetModule', width:150},
                {name:'configurationGroup',index:'configurationGroup', width:180},
                {name:'name',index:'name asc', width:90},
                {name:'description',index:'description', width:90},
                {name:'identity',index:'identity', width:90},
                {name:'version',index:'version', width:90},
                {name:'systemId',index:'systemId', width:100},
                {name:'active',index:'active', width:100}
            ], rowNum:10, width:980, rowList:[10,20,30], pager: '#pconfTable', sortname: 'name', viewrecords: true, sortorder: "desc", caption:"Configuration Information" });
        jQuery("#pconfTable").jqGrid('navGrid', '#pconfTable', {edit:false,add:false,del:false});

这是我得到的 JSON 数据:

[{
        "value":"10",
        "type":"Tip 1",
        "targetModule":"Target 1",
        "configurationGroup":null,
        "name":"Configuration Deneme 1",
        "description":null,
        "identity":"Configuration Deneme 1",
        "version":0,
        "systemId":0,
        "active":true
    },
    {
        "value":"50",
        "type":"Tip 2",
        "targetModule":"Target 2",
        "configurationGroup":null,
        "name":"Configuration Deneme 2",
        "description":null,
        "identity":"Configuration Deneme 2",
        "version":0,
        "systemId":0,
        "active":true
    },
    {
        "value":"100",
        "type":"Tip 3",
        "targetModule":"Target 3",
        "configurationGroup":null,
        "name":"Configuration Deneme 3",
        "description":null,
        "identity":"Configuration Deneme 3",
        "version":0,
        "systemId":0,
        "active":true
    }
]

我已格式化缩进以便轻松阅读。

但是我没有从 Firebug 收到任何错误,也没有任何无法导入的文件,但我仍然有一个空表。

有什么想法吗?

PS: 我的 JSON 数据有什么问题吗,我应该发送一个像 => 开头的数据 => 总:xxx,页面:yyy,记录:zzz,行:还是不是必须的?

【问题讨论】:

    标签: javascript jquery plugins jqgrid


    【解决方案1】:

    要查看填充的网格,您应该使用以下 jsonReader 作为附加的 jqGrid 参数

    jsonReader: {
        repeatitems: false,
        id: "value",
        root: function (obj) { return obj; },
        page: function (obj) { return 1; },
        total: function (obj) { return 1; },
        records: function (obj) { return obj.length; }
    }
    

    我想,'value' 列中的值是唯一的,所以我在上面的jsonReader 中使用了id: "value"。

    顺便说一句,ajaxGridOptions : {type:"GET"} 什么都不做。默认的mtype: 'GET' 也是如此。在调用navGrid 方法时,您应该使用jQuery("#confTable") 而不是jQuery("#pconfTable")。

    在描述的更改后,您将拥有以下demo。在演示中,我添加了height: 'auto' 以减少网格中的空白空间。

    【讨论】:

    • @kamaci:它应该不起作用,因为您不使用loadonce:true 选项,并且排序和分页应该在服务器端。因为我只使用您保存在文件中的 JSON 数据,所以服务器会返回与您的 JSON 中相同的按“名称”ASC 排序的数据。
    • 还有,我可以关闭左下角的搜索按钮吗?
    • 所以如果我将 "loadonce:true" 更改为 false 是否会在 客户端 对其进行排序这可能是解决方案 => trirand.com/blog/?page_id=393/bugs/jqgrid-loadonce-problem,我会检查
    • @kamaci: "loadonce:true" 表示加载数据一次和本地排序、分页和过滤。见here。您甚至可以使用本地分页和数据过滤(如toolbar searching 和advanced searching)。您应该只对从服务器加载的初始数据进行排序,并使用sorttype: 'int' 等其他列设置来获得更正确的排序顺序。
    • @kamaci:我不能跟着你。你想拥有什么?如果您写“将“loadonce:true”更改为false”,您的意思是“loadonce:false”,这是默认设置,它与您在开始时的设置相同。如果您必须在服务器端实现排序、分页和可选的过滤/搜索。顺便说一句,the reference 是从 2009 年开始的。当时设置 loadonce 不存在。它描述了loadonce做什么的主要思想。
    猜你喜欢
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多