2012-11-14 50 views
0

jqGrid的填充

$.ajax({ 
     type: "POST", 
     url: "populateAllRulesJson.action", 
     data: "", 
     dataType: "json", 
     success: function(result) 
     { 
      alert("SUCCESS ::: " + JSON.stringify(result.jSONResponseObject)); 
      data = JSON.stringify(result.jSONResponseObject); 
      jQuery("#rulesTable").jqGrid({ 
        url:"populateAllRulesJson.action", 
        datatype: "jsonstring", 
        height: 'auto', 
        width: 'auto', 
        colNames:['Rule ID','Description', 'Geograph', 'Process', 'Rules Classification', 'Types', 'UDAC'], 
        colModel:[ {name:'ruleID',index:'ruleID', width:65, sorttype:'int'}, 
           {name:'description',index:'description', width:150}, 
           {name:'geograph',index:'geograph', width:100}, 
           {name:'process',index:'process', width:100},    
           {name:'rulesClassification',index:'rulesClassification', width:100}, 
           {name:'types',index:'types', width:100}, 
           {name:'udac',index:'udac', width:100} 
          ], 
        datastr : data, 
        jsonReader: { repeatitems: false }, 
        rowNum:10, 
        rowList : [10,20,30], 
        loadonce:true, 
        mtype: "GET", 
        rownumbers: true, 
        rownumWidth: 10, 
        gridview: true, 
        pager: '#rulesDivPager', 
        sortname: 'ruleID', 
        viewrecords: true, 
        sortorder: "asc", 
        caption: "Searching Rules ..." 

       }); 
     }, 
     error: function(x, e) 
     { 
      alert(x.readyState + " "+ x.status +" "+ e.msg); 
     } 

}); 

我送以下JSON對象到網格我創建網格具有以下配置的空行。但網格填充了四個空白行。

{ 
"total":2, 
"page":1, 
"records":7, 
"rows":[{"id":"1","cell":"{\"ruleID\":\"43\",\"description\":\"Images, text and voice over should synchronize as best as possible.\",\"geograph\":\"Yell US\",\"process\":\"Photomotion\",\"rulesClassification\":\"Image\",\"types\":\"New\",\"udac\":\"NPM\"}"}, 
{"id":"2","cell":"{\"ruleID\":\"48\",\"description\":\" For profile pages UDAC Mismatch in a Control sheet can be ignored.\",\"geograph\":\"Yell US\",\"process\":\"Profile Pages\",\"rulesClassification\":\"Copysheet\",\"types\":\"New\",\"udac\":\"NGP\"}"}, 
{"id":"3","cell":"{\"ruleID\":\"51\",\"description\":\" Ignore all requests for logo sized artwork in NSP ads.\",\"geograph\":\"Yell US\",\"process\":\"Profile Pages\",\"rulesClassification\":\"Logo\",\"types\":\"New\",\"udac\":\"NSP\"}"}, 
{"id":"4","cell":"{\"ruleID\":\"47\",\"description\":\" Irregular borders are not allowed in banner ads..\",\"geograph\":\"Yell US\",\"process\":\"Profile Pages\",\"rulesClassification\":\"Border\",\"types\":\"New\",\"udac\":\"MRB\"}"} 
]} 

我在這個網格配置中犯了什麼錯誤。請告訴我。

+0

我更新了我的答案 – Oleg

回答

1

我認爲你的主要錯誤是使用cell作爲字符串。你有代替

{"id":"1","cell":"{\"ruleID\":\"43\", ...}"}, 

{"id":"1","cell":{"ruleID":"43", ...}}, 

的第二個問題是,您使用的輸入數據的一些非常奇怪的格式。一般來說,您可以使用jsonReader選項指定輸入數據的格式。如果您不使用任何jsonReader,將使用默認值(請參閱the documentation)。所以cell屬性的值必須是項目的陣列

{"id":"1","cell":["43", ...., "New","NPM"]}, 

可以減少發送"id"(尤其是虛值這是不相等的ruleID)和每一行中發送固定文本"cell"的需要數據的。在這種情況下,可以指定jsonReader: {cell: "", id: 0}和數據直接作爲數組:

["43", ...., "New","NPM"] 

用法id: 0意味着該陣列的第一個元素指定唯一的rowid。

或者您可以使用jsonReader: {repeatitems: false, id: "ruleID"},並命名屬性發送的項目爲對象:

{"ruleID":"43", ....,"types":"New","udac":"NPM"} 

所以你有很多選擇,但當前的JSON數據的格式是明確錯誤的,你必須改變它。

此外我認爲你應該更好地使用datatype: "json"而不是datatype: "jsonstring"。如果您確實使用datatype: "jsonstring",則可以使用datastr : result.jSONResponseObjectdatastr一定不能是JSON字符串。這可能是對象。

最後一句:我建議你使用loadError回調。有關更多詳細信息,請參見the answer