2013-08-30 67 views
0

海蘭傢伙,所以我的數據是這樣的:如何映射多層jsondata到的jqGrid

TimeSheetData

[ 
    {ID:2, Name:QXC, Items:[{Status:1, Hours: 8},{Status:1, Hours: 4}]}, 
    {ID:5, Name:ABC, Items:[{Status:1, Hours: 6},{Status:1, Hours: 0}]} 
] 

和我的格子看起來像:

jQuery("#rowed5").jqGrid({   
    data: timeSheetData, 
    datatype: "local", 
    height: 250, 
    colNames:['ID','Name','Monday','Tuesday'], 
    colModel:[ 
       {name:'ContractID', index:'ContractID', jsonmap:'?????', width:200, editable:false, sortable:false}, 
       {name:'EmployeeName', index:'EmployeeName', jsonmap:'?????'' ,width:200, editable:false, sortable:false}, 
       {name:'Monday', index:'Monday', jsonmap:'?????', width:200, editable:false, sortable:false}, 
       {name:'Tuesday', index:'Tuesday'jsonmap:'?????'jsonmap:'timeSheetRow1.timeSheetItem2.WorkedHours', width:200, editable:false, sortable:false} 
      ], 
    caption: "Input Types", 
    jsonReader: {repeatItems: false, root: "timeSheetRow1"} 
    }); 

而且我很感興趣什麼我需要什麼來代替?????在我的jqgrid表中獲取以下兩行:

2, QXC, 8, 4 
    5, ABC, 6, 0 

Thx提前!

回答

0

所以,我想它了。我的問題是,我只看着對象的價值,而不看它的結構。因此,考慮到這一點的對象結構如下:

//TimeSheetData: 
var timeSheetData = new Object(); 
timeSheetData.Rows = [row1,row2]; /*Rows is an 
Array containing Row objects*/ 

//Row: 
var row1 = new Object(); 
row1.ID = 19; 
row1.Name = "F.L."; 
row1.Items = [item1, item2]; /*Items 
is an object with containing other objects(Item) and some properties*/ 

//Item: 
var item1 = new Object(); 
item1.Status = 0; 
item1.Hours = 6; 

所以,概括起來,回答我的問題(如何colModel看起來像我的情況)是:

..., 
colModel:[ 
      {name:'ContractID', index:'ContractID', jsonmap:'ID', width:200, editable:false, sortable:false}, 
      {name:'EmployeeName', index:'EmployeeName', jsonmap:'Name' ,width:200, editable:false, sortable:false}, 
      {name:'Monday', index:'Monday', jsonmap:'Items.0.Hours', width:200, editable:false, sortable:false}, 
      {name:'Tuesday', index:'Tuesday', jsonmap:'Items.1.Hours', width:200, editable:false, sortable:false} 
     ], 
jsonReader: {repeatItems: false, root: "Rows"} 
}); 
jQuery("#rowed5")[0].addJSONData(timeSheetData.Rows);