jqGrid意外地改变了数据源的格式

时间:2016-03-30 20:33:56

标签: jquery json jquery-ui jqgrid

出于某种原因,如果我以下列格式提供我的jqGrid JSON源数据:

[
  { "Filter": { "UserID": 12, "FirstName": "Harry", "LastName": "Alfred", "FullName": "Harry Alfred", "Initials": "HA" }, "Count": 5, "OtherInfo": 29 },
  { "Filter": { "UserID": 2, "FirstName": "Larry", "LastName": "Anderson", "FullName": "Larry Anderson", "Initials": "LA" }, "Count": 3, "OtherInfo": 23 },
  { "Filter": { "UserID": 4, "FirstName": "Allen", "LastName": "Quayle", "FullName": "Allen Quayle", "Initials": "AQ" }, "Count": 4, "OtherInfo": 24 },
  { "Filter": { "UserID": 5, "FirstName": "Todd", "LastName": "Stevens", "FullName": "Todd Stevens", "Initials": "TS" }, "Count": 2, "OtherInfo": 59 },
  { "Filter": { "UserID": 6, "FirstName": "Cathy", "LastName": "Smith", "FullName": "Cathy Smith", "Initials": "CS" }, "Count": 6, "OtherInfo": 48 }
]

它将其更改为当我尝试通过jqGrid(“getGridParam”,“data”)函数访问数据时返回的内容:

[
  {"Filter.UserID":12,"Filter.FullName":"Harry Alfred","Count":5,"OtherInfo":29,"_id_":"12"},
  {"Filter.UserID":2,"Filter.FullName":"Larry Anderson","Count":3,"OtherInfo":23,"_id_":"2"},
  {"Filter.UserID":4,"Filter.FullName":"Allen Quayle","Count":4,"OtherInfo":24,"_id_":"4"},
  {"Filter.UserID":5,"Filter.FullName":"Todd Stevens","Count":2,"OtherInfo":59,"_id_":"5"},
  {"Filter.UserID":6,"Filter.FullName":"Cathy Smith","Count":6,"OtherInfo":48,"_id_":"6"}
]

当然,这会搞砸我需要访问rowObject属性的自定义列格式化程序函数。

例如,给定源JSON,在我的自定义列格式化函数中,我应该能够访问rowObject.Filter.LastName属性。当然,我可以在首次显示网格时这样做。

但是,如果我尝试对列进行排序,我会收到由我的自定义格式化程序函数抛出的“无法读取属性LastName of undefined”的javascript错误,这是由jqGrid对源数据应用的自动重新格式化引起的。

发生了什么以及如何阻止jqGrid这样做?

编辑:

仅供参考,我正在使用jqGrid 5.0.0,至少根据.js文件的评论,看起来像这样:

* jqGrid  5.0.0  
* Copyright (c) 2008, Tony Tomov, tony@trirand.com 
*  License: http://guriddo.net/?page_id=103334 

我从这里回来了一段时间:http://www.trirand.com/blog/?page_id=6(虽然看起来v5.1最近发布了)。这里也是完整的(精简的)来源,所以人们可以看到我在做什么。:

<table id="gridTest"></table>
<div id="gridPagerTest"></div>

<script type="text/javascript">
    $(function () {
        $("#gridTest").jqGrid({
            datatype: "jsonstring",
            datastr: [
              { "Filter": { "UserID": 12, "FirstName": "Harry", "LastName": "Alfred", "FullName": "Harry Alfred", "Initials": "HA" }, "Count": 5, "OtherInfo": 29 },
              { "Filter": { "UserID": 2, "FirstName": "Larry", "LastName": "Anderson", "FullName": "Larry Anderson", "Initials": "LA" }, "Count": 3, "OtherInfo": 23 },
              { "Filter": { "UserID": 4, "FirstName": "Allen", "LastName": "Quayle", "FullName": "Allen Quayle", "Initials": "AQ" }, "Count": 4, "OtherInfo": 24 },
              { "Filter": { "UserID": 5, "FirstName": "Todd", "LastName": "Stevens", "FullName": "Todd Stevens", "Initials": "TS" }, "Count": 2, "OtherInfo": 59 },
              { "Filter": { "UserID": 6, "FirstName": "Cathy", "LastName": "Smith", "FullName": "Cathy Smith", "Initials": "CS" }, "Count": 6, "OtherInfo": 48 }
            ],
            styleUI: 'Bootstrap',
            colModel: [
                { label: 'UserID', name: 'Filter.UserID', key: true, hidden: true },
                { label: 'LastName', name: 'Filter.LastName', hidden: true },
                { label: 'User', name: 'Filter.FullName', index: "Filter.LastName", width: 250, formatter: userNameFormatter },
                { label: 'Count', name: 'Count', index: "Count", width: 200 },
                { label: 'OtherInfo', name: 'OtherInfo', index: "OtherInfo", width: 200 },
            ],
            page: 1,
            sortname: "Filter.LastName",
            sortorder: "asc",
            viewrecords: true,
            width: '100%',
            height: '100%',
            rowNum: 10,
            pager: "#gridPagerTest"
        });
    });

    function userNameFormatter(cellvalue, options, rowObject) {
        return "<a href=\"SomePage?DisplayName=" + encodeURIComponent(rowObject.Filter.LastName) + "&UserID=" + rowObject.Filter.UserID + "\">" + cellvalue + "</a>";
    }
</script> 

0 个答案:

没有答案