2012-08-10 58 views
0

我的回应数据看起来像这样如何获得jqGrid的重新加载的数据

{"page":"1","total":"10","records":"8","message":null,"rowdata": 
[{"status":null,"contactNo":null,"approvalStatus":null,"userInfoID":32,"userName":"bbb 
1234","userFullName":"bbb 1234","profiles":[{"profileInfoID":10,"profileName":"Profile 
Ganda"},{"profileInfoID":15,"profileName":"Sample Profile 123"}]}, 
{"status":null,"contactNo":null,"approvalStatus":null,"userInfoID":28,"userName": 
"yyyy1234","userFullName":"yyyy 1234","profiles":  
"profileInfoID":10,"profileName":"Profile Ganda"}]},.......]} 

我有使用AJAX的响应和传递的响应数据的jqGrid的和数据类型设置为本地及代码下面给出。

function buildUserGridAttach(response){ 
var jq = jQuery.noConflict(); 
jq("#attachTable").jqGrid({ 
url:"searchUsers.htm", 
data: response.rowdata, 
datatype: "local", 
mtype:"post", 
width: 1100, 
height: "auto", 
rowNum: 5, 
rowList: [5, 10, 20], 
viewrecords: true, 
rownumbers: false, 
toppager: true, 
bottompager:true, 
pager: jQuery("#resultsPagerAttach"), 
sortname: "userName", 
sortorder: "asc", 
caption: "Search Results", 
    // Specify the column names 
    colNames: ["Select","User ID", "User Name", "Profiles"], 
    // Configure the columns 
    colModel: [ 
    { name: "userInfoID",hidden:true,index: "userInfoID",editable:true, width: 25},       
    { name: "userName", index: "userName", width: 25, align: "left", search:true, sorttype:'text',title:false}, 
    { name: "userFullName", index: "userFullName", width: 25, align: "left" ,search:false, sorttype:'text'}, 
    { name:"profiles", index:"profiles", width:60, editable: true,edittype:"select",editoptions:{multiple:true,width:50} },  
    ], 

    // Grid total width and height 
    autowidth: true, 
    height: 300,  
    // Paging 
    rownumbers: true, 
    toppager: true, 
    bottompager : true, 
    pager: jq("#paging"), 
    rowNum: 5, 
    rowList: [5, 10, 20], 
    viewrecords: true, // Specify if "total number of records" is 

    // Default sorting 
    sortname: "userName", 
    sortorder: "asc", 
    hidegrid: false, 
    emptyrecords: "No records to view", 

    // Grid caption 
    caption: "Search Results", 
    multiselect:true, 

    gridComplete: function() { 
    var grid = jQuery("#attachTable");   
     jQuery.each(response.rowdata, function(ID,row) { 
     var rowId = (ID+1); 
     grid.editRow(rowId, true); 
     var de = "<select style='width: 150px;' name=" + rowId + " id=" + rowId + " multiple=true >";    
     jQuery.each(row.profiles, function(Id,profile) { 
       de = de + '<option value="' + row.userInfoID + ":" + profile.profileInfoID + '">'+ profile.profileName+'</option>'; 
      }); 
     de = de + '</select>'; 
     // de = de + "<input type=hidden id='userInfoID' value='"+ row.userInfoID + "'>"; 
     jQuery("#attachTable").jqGrid('setRowData', rowId, { profiles: de }); 
     jQuery("#attachTable").jqGrid('setRowData', rowId, { userInfoID: row.userInfoID}); 
     jQuery("#attachTable").jqGrid('setRowData', rowId, { userName: row.userName }); 
     jQuery("#attachTable").jqGrid('setRowData', rowId, { userFullName: row.userFullName}); 
     }); 
    }, 
    jsonReader : {root: "rowdata", page: "page", total: "total", records: "records", repeatitems: false, id: "userInfoID"} 
}); 
} 

数据填写正确,我可以对网格进行所有操作。最初我遇到的问题是我无法使用新数据重新加载网格。 阅读了大量的论坛后,我与

grid.jqGrid('clearGridData').jqGrid('setGridParam', { data: response.rowdata}) 
.trigger('reloadGrid', [{ page: 1}]); 

试过上面的命令作品好,恢复数据时。但是,当gridComplete函数被调用时,以前的数据将覆盖新的数据。在发表gridComplete函数后,我发现了这个错误。

我需要解决这个问题。过去3天我一直在做这件事。

我的JqGrid表看起来如下。

http://i49.tinypic.com/2n9fsx.png

+0

是否有你使用'datatype:local'而不是'datatype:json'的理由?如果你想加载数据一次,编辑,然后在提交后重新加载,有更简单的方法.... – 2012-08-10 18:00:29

+0

我的代码为豆类似于这个。 A类包含B类的另一个列表。虽然我尝试为特定A类记录检索对象B的列表,但我没有得到B类的值。而是得到一个字符串[Object],[Object] (指示A类对象的B类对象的两个值)。因此,我试图从响应中构建网格来解决这个问题。 – 2012-09-03 05:27:19

回答

0

尝试使用GridUnload,然后再建立同样的网格。做这样的事:

jQuery("#attachTable").jqGrid("GridUnload"); 
buildUserGridAttach(response); 

这是残酷的,但应该工作。

+0

宾果!这工作!我想知道为什么我以前没有使用这个! – 2012-08-13 03:16:15

+0

@BalasubramanianJayaraman,如果它适合你,你应该将其标记为答案。 – 2012-09-04 12:56:27