【问题标题】:jqGrid fetched json but has empty rows and no datajqGrid 获取 json 但有空行且没有数据
【发布时间】:2011-07-18 17:09:26
【问题描述】:

JSON 由 Spring 3 MVC 生成 @ResponseBody

{
    "total": "1",
    "page": "1",
    "records": "2",
    "rows": [
        {
            "id": "1",
            "cell": {
                "accountId": 1,
                "userId": 1,
                "transactionId": 6,
                "subCatId": 0,
                "accountName": "Credit Card",
                "remarks": "Movie Hall Pass",
                "amount": 250.0,
                "transactionDate": "2011-03-16",
                "subCatName": "Entertainment"
            }
        },
        {
            "id": "2",
            "cell": {
                "accountId": 2,
                "userId": 1,
                "transactionId": 7,
                "subCatId": 1,
                "accountName": "Savings Bank",
                "remarks": "Part at Besand Nagar",
                "amount": 5000.0,
                "transactionDate": "2011-03-16",
                "subCatName": "Dine Out"
            }
        }
    ]
}

JQGrid 初始化代码:

$("#transactionLogTable").jqGrid({
                url: '/et/transaction/getTransactions?dateValue=03%2F16%2F2011',
                datatype: "json",
                loadError: function(xhr,status,error){alert(status+" "+error);},
                colNames:['Transaction ID', 'User ID', 'Subcat ID', 'Subcat Name',
                          'Account ID', 'Account Name', 'Date', 'Amount', 'Notes'],

                colModel:[
                    {name: 'transactionId', index: 'transactionId', width: 100},
                    {name: 'userid', index: 'userId', width: 100},
                    {name: 'subCatId', index: 'subCatId', width: 100},
                    {name: 'subCatName', index: 'subCatName', width: 100},
                    {name: 'accountId', index: 'accountId', width: 100},
                    {name: 'accountName', index: 'accountName', width: 100},
                    {name: 'transactionDate', index: 'transactionDate', width: 100},
                    {name: 'amount', index: 'amount', width: 100},
                    {name: 'remarks', index: 'remarks', width: 100}
                ],
                pager: "#pager",
                rowNum: 10,
                rowList: [10,20,30],
                sortname: 'userId',
                sortorder: 'asc',
                viewrecords: true,
                caption: 'Transactions'
            });

使用 QueryString 成功命中服务器:

dateValue=03%2F16%2F2011&_search=false&nd=1300532086871&rows=10&page=1&sidx=userId&sord=asc

现在很好,我得到一个屏幕,其中显示了 jqGrid 和 2 个空行。我无法在行内显示数据。

我猜这与映射有关,但我已经尝试了尽可能多的组合。

包含的文件和版本:

<script type="text/javascript" language="javascript" src="/et/resources/js/jquery.jqGrid-3.8.2.full/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" language="javascript" src="/et/resources/js/jquery-ui-1.8.10.start/js/jquery-ui-1.8.10.custom.min.js"></script>
<script type="text/javascript" language="javascript" src="/et/resources/js/form-2.52.js"></script>
<script type="text/javascript" language="javascript" src="/et/resources/js/validate-1.7.js"></script>
<script type="text/javascript" language="javascript" src="/et/resources/js/jquery.jqGrid-3.8.2.full/js/i18n/grid.locale-en.js" charset="utf-8"></script>
<script type="text/javascript" language="javascript" src="/et/resources/js/jquery.jqGrid-3.8.2.full/js/jquery.jqGrid.min.js"></script>

感谢您的帮助。

冷酷的阿米尔

【问题讨论】:

    标签: jquery json model-view-controller spring jqgrid


    【解决方案1】:

    您的主要错误是数据格式错误。你应该使用

    {
        "total": "1",
        "page": "1",
        "records": "2",
        "rows": [
            {
                "id": "1",
                "cell": ["1", "1", "6", "0", "Credit Card", "Movie Hall Pass",
                         "250.0", "2011-03-16", "Entertainment" ]
            },
            ...
        ]
    }
    

    而不是

    {
        "total": "1",
        "page": "1",
        "records": "2",
        "rows": [
            {
                "id": "1",
                "cell": {
                    "accountId": 1,
                    "userId": 1,
                    "transactionId": 6,
                    "subCatId": 0,
                    "accountName": "Credit Card",
                    "remarks": "Movie Hall Pass",
                    "amount": 250.0,
                    "transactionDate": "2011-03-16",
                    "subCatName": "Entertainment"
                }
            },
            ...
        ]
    }
    

    一般来说,jqGrid 足够灵活,可以读取几乎任何 JSON 数据。您可以只定义jsonReader jqGrid 参数,有时还可以在列定义中定义jsonmap 属性。例如,在您的情况下,可以使用以下 jqGrid 定义读取您的数据

    $("#transactionLogTable").jqGrid({
        // ... other parameters
        cmTemplate: {width: 100},
        colModel:[
            {name:'transactionId',   jsonmap: 'cell.transactionId'},
            {name:'userId',          jsonmap: 'cell.userId'},
            {name:'subCatId',        jsonmap: 'cell.subCatId'},
            {name:'subCatName',      jsonmap: 'cell.subCatName'},
            {name:'accountId',       jsonmap: 'cell.accountId'},
            {name:'accountName',     jsonmap: 'cell.accountName'},
            {name:'transactionDate', jsonmap: 'cell.transactionDate'},
            {name:'amount',          jsonmap: 'cell.amount'},
            {name:'remarks',         jsonmap: 'cell.remarks'}
        ],
        height: "auto",
        jsonReader: { repeatitems: false }
    });
    

    这里我使用jsonReader: { repeatitems: false } 来定义一行的JSON 数据不在数组中,而是在具有命名属性的对象的for 中。需要jsonmap: "cell.userId" 之类的属性来指定相应网格列的值不应作为行对象的默认userId 属性,而应是“单元格”属性的子属性。顺便说一句,您在 JSON 数据中使用“userid”作为列名和“userId”。最好使用与 JSON 数据相同的名称。在您使用与“名称”相同的“索引”属性时,您可以删除“索引”。在这种情况下,“名称”属性的值将用作“索引”。

    因为您对网格的所有列都使用了“width:100”属性,所以我使用了cmTemplate: {width: 100} 参数来使colModel 定义更短且更易于阅读。

    你可以看到修改后的网格直播here

    我建议您始终以 ISO 格式 YYYY-mm-dd 发布日期,并使用 colModelformatter:'date'datefmt 属性(例如 formatter:'date', formatoptions:{newformat:'d-m-Y'}, datefmt: 'd-m-Y'

    【讨论】:

    • 谢谢@Oleg!这解决了问题。我只是在关注 jqGrid 文档,我永远无法找到您刚刚解释的内容。这是我第一次尝试使用 jqGrid,我更喜欢它而不是 DataTables。再次感谢!
    • @firdousamir:不客气!您在文档中找不到什么?日期格式,jsonReaderjsonmapcmTemplate 或更多...
    • 您的回答几乎确实可以更清楚地了解用法。作为我新项目的一部分,我正在学习/试验网格和 spring 3。有了您的帮助,我现在更有信心了。
    • @firdousamir:为了能够帮助您,我需要向您提出具体问题,您可以在其中解释您究竟不明白什么以及我可以在哪些方面为您提供帮助。您可以将您的代码与我来自ok-soft-gmbh.com/jqGrid/user667200.htm 的代码进行比较。您还可以更改您的服务器代码以生成类似于here 描述的默认输入格式的 JSON。那么您将不需要任何jsonReaderjsonmap
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2017-12-20
    相关资源
    最近更新 更多