JQGrid填充空行

时间:2012-11-14 07:38:40

标签: jquery 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\"}"}
]}

我在此网格配置中犯了什么错误。请告诉我。

1 个答案:

答案 0 :(得分: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