0

我第一次尝试从服务器加载动态列数据和列名,并使用服务器端分页。

问题是当我在 jqgrid AJAX 调用(第二个)中使用数据类型:“本地”时,显示数据并禁用服务器端分页。但是当我在 AJAX 调用中使用 datatype : 'json' 时,不会显示任何数据。

请帮助我获得一个简单、清晰的解决方案来实现动态列绑定、客户端排序和服务器端分页。

$(document).ready(function() {
                    $.ajax({
                                type : "POST",
                                url : contextPath + "/getEntities",
                                dataType : "json",
                                success : function(result) {
                                    var colD = result.response,
                                    colM = result.colModList,
                                    colN = result.colNames;

                                    $("#list").jqGrid({
                                        url : contextPath + "/getEntities",
                                        datatype : 'json',
                                        mtype : 'POST',

                                        data : colD,
                                        colNames : colN,
                                        colModel : colM,
                                        pager : '#pager',
                                        rowNum : 10,
                                        rowList : [ 10, 20, 30 ],
                                        sortname : 'column1',
                                        sortorder : 'asc',
                                        viewrecords : true,
                                        gridview : true,
                                        caption : 'My first grid',
                                    });

                                },
                                loadComplete : function() {
                                    $("#list").jqGrid('setGridParam',{datatype:'json'});
                                },

                                error : function(x, e) {
                                    alert(x.readyState + " " + x.status
                                            + " " + e.msg);
                                }
                            });
                });

从服务器接收的 JSON 数据没有问题,因为我可以在设置 datatype : 'local' 时看到数据(在第二个 AJAX 调用中)。如果有人提供一个工作示例,那将是一个很大的帮助。

另一个问题在我脑海中,为什么要重新加载网格以及如何做到这一点。在哪里放置重新加载代码。我没有得到这部分。

任何帮助是极大的赞赏。

更新:已发布答案。非常适合使用服务器端分页实现动态列。

4

1 回答 1

0

工作代码:

$(document).ready(function() {
$.ajax({
        type : "GET",
        url : contextPath + "/getEntities",
        dataType : "json",
        success : function(result) {
            var colM = result.colModList,
            colN = result.colNames;

            $("#list").jqGrid({
                url : contextPath + "/getEntities",
                datatype : 'json',
                mtype : 'GET',
                jsonReader : {
                    root : "response",
                    page : "page",
                    total : "total",
                    records : "records",
                    repeatitems : false,
                    id : "column1"
                },
                colNames : colN,
                colModel : colM,
                pager : '#pager',
                rowNum : 10,
                rowList : [ 10, 20, 30 ],
                sortname : 'column1',
                sortorder : 'asc',
                viewrecords : true,
                gridview : true,
                caption : 'My first grid',
            });

        },
        error : function(x, e) {
            alert(x.readyState + " " + x.status
                    + " " + e.msg);
        }
    });
});
于 2016-08-16T07:05:47.917 回答