2010-12-09 56 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