寻呼机在带有JSON的jqGrid中没有按预期工作

时间:2012-01-12 16:20:54

标签: json jsp jqgrid pager

我对jqGrid和Json有疑问。

这是我的jsp文件的代码,代码为Jquery:

    <body >

    <script type="text/javascript">
        jq(function() {
            jq("#grid").jqGrid({
                url:'/test-security-client/crud',
                datatype: 'json',
                mtype: 'GET',
                colNames:['Id', 'Log Message'],
                colModel:[
                    {name:'id',index:'id', width:55,editoptions:{readonly:true,size:10},hidden:true},
                    {name:'logMsg',index:'logMsg', width:300,editable:true, editrules:{required:true}, editoptions:{size:10}}
                ],
                rowNum:20,
                rowList:[20,40,60],
                height: 450,
                autowidth: true,
                rownumbers: true,
                pager: '#pager',
                sortname: 'id',
                viewrecords: true,
                sortorder: "asc",
                caption:"Error Logs",
                emptyrecords: "Empty records",
                loadonce: false,
                loadComplete: function() {
                },
                jsonReader : {
                    root: "rows",
                    page: "page",
                    total: "total",
                    records: "records",
                    repeatitems: false,
                    cell: "cell",
                    id: "id"
                }
            });

            jq("#grid").jqGrid('navGrid','#pager',
                    {edit:false,add:false,del:false,search:false},
                    { },
                    { },
                    { }, 
                    { 
                        sopt:['eq', 'ne', 'lt', 'gt', 'cn', 'bw', 'ew'],
                        closeOnEscape: true, 
                            multipleSearch: true, 
                            closeAfterSearch: true }
            );

            jq("#grid").navButtonAdd('#pager',
                {   caption:"Delete", 
                    buttonicon:"ui-icon-trash", 
                    onClickButton: deleteRow,
                    position: "last", 
                    title:"", 
                    cursor: "pointer"
                } 
            );

            jq("#btnFilter").click(function(){
                jq("#grid").jqGrid('searchGrid',
                        {multipleSearch: false, 
                            sopt:['eq']}
                );
            });

        });
    </script>
   ...

这是我发送的JSON:

{"total":"2","page":"1","records":"28","rows":[{"id":1,"logMsg":"ID of log:1-Requesting authentication. (SMA/SmaAdmin/password)"},{"id":2,"logMsg":"ID of log:2-Authentication received."},{"id":3,"logMsg":"ID of log:3-Test"},{"id":4,"logMsg":"ID of log:4-Test 2"},{"id":5,"logMsg":"ID of log:5-Test 3"},{"id":6,"logMsg":"ID of log:6-Test 4"},{"id":7,"logMsg":"ID of log:7-Test 5"},{"id":8,"logMsg":"ID of log:8-Test 6"},{"id":9,"logMsg":"ID of log:9-Test 7"},{"id":10,"logMsg":"ID of log:10-Test 8"},{"id":11,"logMsg":"ID of log:11-Test 9"},{"id":12,"logMsg":"ID of log:12-Test 10"},{"id":13,"logMsg":"ID of log:13-Test 11"},{"id":14,"logMsg":"ID of log:14-Test 12"},{"id":15,"logMsg":"ID of log:15-Test 13"},{"id":16,"logMsg":"ID of log:16-Test 14"},{"id":17,"logMsg":"ID of log:17-Test 15"},{"id":18,"logMsg":"ID of log:18-Test 16"},{"id":19,"logMsg":"ID of log:19-Test 17"},{"id":20,"logMsg":"ID of log:20-Test 18"},{"id":21,"logMsg":"ID of log:21-Test 19"},{"id":22,"logMsg":"ID of log:22-Test 20"},{"id":23,"logMsg":"ID of log:23-Test 21"},{"id":24,"logMsg":"ID of log:24-Test 22"},{"id":25,"logMsg":"ID of log:25-Test 23"},{"id":26,"logMsg":"ID of log:26-Test 24"},{"id":27,"logMsg":"ID of log:27-Test 25"},{"id":28,"logMsg":"ID of log:28-Test 26"}]}

这是我的问题:这段代码正常运行,jqgrid显示良好,所有信息都很好。但寻呼机效果不佳。因为,我无法在页面之间移动。 (我有2页,我在页码1中启动了jqGrid ..但我不能去2号)。

如果我将属性loadonce更改为true ..那么正在工作。但我不想使用这个属性,并且所有记录都在错误的位置。

据我所知,JSON是正确的,代码是正确的,我不知道我做错了什么。 (我是jqGrid和JQuery的新手)。我想我的寻呼机有一个问题。

知道我做错了什么?我是否必须向Json发送每页的信息?

1 个答案:

答案 0 :(得分:2)

您JSON是正确的JSON,但它不对应于jqGrid请求的数据。我们来解释一下。

您有rowNum: 20sortname: 'id'sortorder: "asc"。在开始时(在第一次填充网格时),jqGrid会在网址'/test-security-client/crud'向您的服务器发送网址末尾为page=1&rows=20&sidx=id&sord=asc的请求:/test-security-client/crud?page=1&rows=20&sidx=id&sord=asc。这意味着在服务器的响应中,您应该只包含结果集中的前20个项目,这些项目按id 排序。因此,您必须按id对结果集进行排序(请参阅sidx=idsord=asc - ORDER BY id),然后获取第一页(page=1)的大小该页面的排名为20(rows=20 - SELECT (TOP 20))。

您从服务器获得的当前响应包括结果集的全部28个。对于第一页并不重要,但我认为第二页的请求响应包含相同的28项而不是最后8项(排序对于获得正确的8项非常重要)。

如果您要解决服务器代码中的问题,网格将显示有关更改页面的正确数据。