2010-12-09 54 views
0

点击一个按钮我想加载与json datadtype jqgrid。 我尝试了很多方法来加载数据,但它失败并显示空网格。 请告知我在这里失踪。Json数据不加载在弹簧3 jqGrid mvc

我网

$("#bedata").click(function(){ 
    jQuery("#list2").jqGrid({ 
     url:'/mso/interop/interopcompanycfg/getDSAccounts?companyId=${interopcompcfg.company.id}', 
     datatype: "json", 
     mtype: 'GET', 
     colNames:['Id','DTCCID','COMPANYID','DTCCACCOUNTID'],   
     colModel:[ 
     {name:'ID',index:'id', sortable:true, align:'center',width:90}, 
     {name:'DTCCID',index:'dsId', sortable:true, align:'center',width:90}, 
     {name:'COMPANYID',index:'companyId',sortable:true, align:'center', width:120}, 
     {name:'DTCCACCOUNTID',index:'dsLegalEntityId', sortable:true, align:'center',width:130}  
     ], 
     rowNum:10, 
     rowList:[10,20,30], 
     pager: '#pager2', 
     sortname: 'dsId', 
     viewrecords: true, 
     sortorder: "desc", 
     caption:"DS ACCOUNTS", 
     jsonReader: { 
     repeatitems : false,  
     root:"rows", 
     cell: "", 
     id: "0" 
     } 
    }); 
    jQuery("#list2").jqGrid('navGrid','#pager2',{edit:true,add:true,del:true}); 
}); 


spring requestmapping 
@RequestMapping(value="/getDSAccounts",method= RequestMethod.GET) 
public @ResponseBody List<Vector<String>> getDSAccountsJSON(HttpServletRequest request,HttpServletResponse httpResponse) { 
    try{ 

    UsersJsonDTO usersJsonDTO = new UsersJsonDTO(); 
     usersJsonDTO.setPage("1"); 
    usersJsonDTO.setRecords("8");  
    usersJsonDTO.setTotal("20"); 

    Company cmp=(Company) request.getSession().getAttribute("company"); 
    List<DSAccounts> message = interopService.getDSAccounts(cmp); 

    httpResponse.setContentType("text/javascript"); 
    int i=0; 
    List<DTCCAccounts> rowJsonList = new ArrayList<DTCCAccounts>(); 
    for (DSAccounts dsAccountDTO:message) 
    { 
    DTCCAccounts rowJson = new DTCCAccounts();  
    rowJson.setId(String.valueOf(i+1)); 
    rowJson.setDsId(String.valueOf(dsAccountDTO.getDsId())); 
    rowJson.setCompanyId(String.valueOf(dsAccountDTO.getCompany().getId())); 
    rowJson.setDsLegalEntityId(dsAccountDTO.getDsLegalEntityId()); 
    rowJsonList.add(rowJson); 
    } 

    usersJsonDTO.setRows(rowJsonList); 
     return usersJsonDTO; 
+0

你可以发布你的JSON,以便我们验证它吗?您也可以使用http://www.jsonlint.com/ – thomaspaulb 2010-12-09 23:29:44

+0

自己验证它,我在jsonlimt.com中进行了验证,Json是有效的。{ “页”: “1”, “记载”: “8”, “行”:[ { “companyId”: “1661”, “DSID”: “72”, “dsLegalEntityId”: “SELLSIDE01”, “ID”: “1” }, { “companyId”: “1661”, “DSID”: “74”, “dsLegalEntityId”: “SELLSIDE03”, “ID”:“ 1“ } ], ”total“:”20“ } – Sabari 2010-12-10 02:22:30

回答

0

如果你的JSON数据包含类似

{ 
    "page": "1", 
    "records": "8", 
    "rows": [ 
     { 
      "companyId": "1661", 
      "dsId": "72", 
      "dsLegalEntityId": "SELLSIDE01", 
      "id": "1" 
     }, 
     { 
      "companyId": "1661", 
      "dsId": "74", 
      "dsLegalEntityId": "SELLSIDE03", 
      "id": "1" 
     } 
    ], 
    "total": "20" 
} 

元素你都应该列name属性更改为您当前使用的index值和网格将充满数据。

还有一个问题存在于您的数据中:您有两个元素具有相同的编号。所以如果你选择第二行,第一行将被选中。所以你应该选择“dsId”作为ID或者生成具有唯一ID的数据。

更新:Here是相同的代码,其中仅名字是固定的并且是here相同网格,其中的ID也被固定。

0

尝试显式设置您的jsonReader的所有属性。像这样

jsonReader : { 
     root: "rows", 
     page: "page", 
     total: "total", 
     records: "records", 
     repeatitems: false, 
     cell: "cell", 
     id: "id" 
} 

你这是什么:

jsonReader: { 
    repeatitems : false,  
    root:"rows", 
    cell: "", 
    id: "0" 
} 

为什么ID设置为? jqGrid如何知道你的JSON数据是什么ID?此外,为什么你删除了总计,页面,单元性能?

JSON格式从我的Spring MVC 3应用程序返回正好是以下几点:

{ 
"total":"10", 
"page":"1", 
"records":"3", 
"rows":[{"id":1,"firstName":"John","lastName":"Smith"}, 
    {"id":2,"firstName":"Jane","lastName":"Adams"}, 
    {"id":3,"firstName":"Jeff","lastName":"Mayer"}] 

}

另外,我注意到你做了以下内容:

httpResponse.setContentType("text/javascript"); 

我没在我的Spring 3 MVC中不必这样做。检查我写的教程http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration.html