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>
感谢您的帮助。
Firdous Amir
答案 0 :(得分:5)
您的主要错误是数据格式错误。你应该使用
{
"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数据不在数组中,而是用于具有命名属性的对象。需要jsonmap: "cell.userId"
之类的属性来指定相应网格列的值不应该是行对象的默认userId
属性,而是另外是“cell”属性的子属性。顺便说一句,您使用'userid'作为列名称,并使用'userId'作为JSON数据。最好使用与JSON数据相同的名称。在您使用与'name'相同的'index'属性时,您可以删除'index'。如果'name'属性的值将用作'index'。
由于您对网格的所有列都使用了“width:100”属性,因此我使用cmTemplate: {width: 100}
参数使colModel
定义更短,更易于阅读。
您可以看到修改过的网格here。
我建议您另外在ISO表单YYYY-mm-dd中发布日期并使用colModel
的{{3}}和formatter:'date'属性(例如formatter:'date', formatoptions:{newformat:'d-m-Y'}, datefmt: 'd-m-Y'
)< / p>