对列排序后,某些列中的数据丢失

Losing data in some columns after sorting a column

本文关键字:数据 排序      更新时间:2023-09-26

我有一个网页,它在jqGrid中填充了一个JSON对象(版本4.6.0)。JSON对象是从java控制器返回的。我如何初始化网格以及如何填充JSON对象如下所示。

问题是,每当单击列标题时,我都会丢失以下列中的数据:组织、部门和标题。所谓"丢失数据",我的意思是这些列中的数据由于某种原因而消失。这些列恰好是一个嵌套的JSON对象。我在这里错过了什么?谢谢

这是从服务器返回的JSON字符串。

[
    {"id":51,"displayName":"John Doe",
     "currentExperience": {"id":26,"orgName":"compnay A","deptName":"MIS","title":"senior software engineer"}},
    {"id":52,"displayName":"Jane Doe",
     "currentExperience": {"id":29,"orgName":"compnay B","deptName":"MIS","title":"software engineer"}}
]

        function initializeGrid($grid, $pager, caption)
        {
            $grid.jqGrid({
                datatype: "local",
                colNames: ["VipId", "ExpId", "Name", "organization", "department", "title"],
                colModel: [
                    { name: "id", hidden: true },
                    { name: "currentExperience.id", hidden: true },
                    { name: "displayName", width: 100, sortable:true },
                    { name: "currentExperience.orgName", width: 100, sortable:true },
                    { name: "currentExperience.deptName", width: 100, sortable:true },
                    { name: "currentExperience.title", width: 100, sortable:true }
                ],
                pager: $pager,
                rowNum: 10,
                rowList: [10, 20, 30],
                sortname: "id",
                sortorder: "asc",
                viewrecords: true,
                gridview: true,
                autoencode: true,
                caption: caption,
                multiselect: true,
                width: 450,
                hidegrid: false
            });
        }
        function wireClickEventForSearchVipsButton($form)
        {
            $(document).on("click", "#btnSearchVips", function(event){
                event.preventDefault();
                var dataMap = composeSearchVipParameters($form);
                <c:url var="link" value="/protected/vips/lookup" />
                $.ajax({
                    url: '${link}',
                    type: 'GET',
                    dataType: 'json',
                    accepts: {
                        text: "application/json"
                    },
                    data: dataMap,
                    error: function() {
                        alert('an error occurred');
                    },
                    success: function(response) {
                        // this is how I populate response from the server
                        if (response) {
                            $grid = $('#gridAvailableVips');
                            if ($grid) {
                                $grid.jqGrid("clearGridData");
                                for(var i=0;i<=response.length;i++) {
                                    $grid.jqGrid('addRowData',i+1,response[i]);
                                }
                            }
                        }
                        else {
                        }
                    }
                }); // end of $.ajax() 
            }); // end of $(document).on()
        }

谢谢,奥列格。当使用以下方法时,问题就消失了。

    function initializeGrid($grid, $pager, caption, json)
    {
        $grid.jqGrid({
            datastr: json,
            datatype: "jsonstring",
            colNames: ["VipId", "ExpId", "Name", "organization", "department", "title"],
            colModel: [
                { name: "id", hidden: true },
                { name: "currentExperience.id", hidden: true },
                { name: "displayName", width: 100, sortable:true },
                { name: "currentExperience.orgName", width: 100, sortable:true },
                { name: "currentExperience.deptName", width: 100, sortable:true },
                { name: "currentExperience.title", width: 100, sortable:true }
            ],
            pager: $pager,
            rowNum: 10,
            rowList: [10, 20, 30],
            sortname: "id",
            sortorder: "asc",
            viewrecords: true,
            gridview: true,
            autoencode: true,
            caption: caption,
            multiselect: true,
            width: 450,
            hidegrid: false
        });
    }
    function wireClickEventForSearchVipsButton($form)
    {
        $(document).on("click", "#btnSearchVips", function(event){
            event.preventDefault();
            var dataMap = composeSearchVipParameters($form);
            <c:url var="link" value="/protected/vips/lookup" />
            $.ajax({
                url: '${link}',
                type: 'GET',
                dataType: 'json',
                accepts: {
                    text: "application/json"
                },
                data: dataMap,
                error: function() {
                    alert('an error occurred');
                },
                success: function(response) {
                    // this is how I populate response from the server
                    if (response) {
                        $grid = $('#gridAvailableVips');
                        if ($grid) {
                            $grid.jqGrid('GridUnload');
                            initializeGrid($('#gridAvailableVips'), $pagerAvailableVips, 'Available Vips', response);
                        }
                    }
                    else {
                    }
                }
            }); // end of $.ajax() 
        }); // end of $(document).on()
    }