2014-02-17 116 views
0
var data = [{"Id":40072,"Id2":40071,"SmDetails":{"Id1":40071,"Id2":40072}}] 

我想在列中顯示SmDetails.Id1。這怎麼可能?我想:如何將列映射到SlickGrid中的複雜對象

var columns = [{name:'Personnel',field:SmDetails.id1,id:'detailId'}]; 

請幫我 請幫我

**My latest code** 
     var data = [{"Id":40072,"Id2":40071,"allocationDetails":{"Id1":40071,"allocationDetails":{"accommodationId":4007}}}] 
    var grid; 
       var columns = [          {name:"Personnel",field:"allocationDetails",fieldIdx:'accommodationId', id:"accommodationId"}]; 
       var options = { 
        enableCellNavigation: true, 
        enableColumnReorder: false, 
        dataItemColumnValueExtractor: 
                   function getValue(item, column) { 
         var values = item[column.field];        
         if (column.fieldIdx !== undefined) { 
       return values && values[column.fieldIdx]; 
       } else { 
       return values; 
       }}}; 
       var gridData=$scope.Vo;//This return as json format       
       grid = new Slick.Grid("#testGrid",gridData, columns); 
    This is the code tried recently. 
+0

嘗試'SmDetails.Id1'(我是大寫) –

+0

是的,我嘗試過相同的方式,但它沒有工作。 – user3319065

回答

0

您需要提供custom value extractor告訴電網如何解讀的對象。

var options = { 
    enableCellNavigation: true, 
    enableColumnReorder: false, 
    dataItemColumnValueExtractor: 
    // Get the item column value using a custom 'fieldIdx' column param 
    function getValue(item, column) { 
    var values = item[column.field]; 
    if (column.fieldIdx !== undefined) { 
     return values && values[column.fieldIdx]; 
    } else { 
     return values; 
    } 
    } 
}; 

列定義看起來像:

{ 
    id: "field1", 
    name: "Id1", 
    field: "SmDetails", 
    fieldIdx: 'Id1' 
}, { 
    id: "field2", 
    name: "Id2", 
    field: "SmDetails", 
    fieldIdx: 'Id2' 
} //... etc 

退房this fiddle的工作示例。

+0

我在我的應用程序中試過這個。但是值顯示爲[object Object]。 – user3319065

+0

將您的最新代碼添加到原始文章的末尾,以便我可以看到如何設置它。 – Origineil

+0

請檢查最新評論添加 – user3319065

0

試試這個你data轉換成單一的長度值的對象...

newData = {}; 
for(key in data[0]){ 
    parentKey = key; 
    if(typeof(data[0][key]) == "object"){ 
     childData = data[0][key]; 
     for(key in childData){ 
      childKey = key; 
      newKey = parentKey+childKey; 
      newData[newKey] = childData[childKey]; 
     } 
    } else { 
     newData[key] = data[0][key]; 
     } 
} 

這將您data對象轉換這樣

newData = {Id: 40072, Id2: 40071, SmDetailsId1: 40071, SmDetailsId2: 40072}; 

現在使用這個newData你的數據映射項目在網格

-1

我覺得這適用於嵌套屬性,例如:

var columns = [ 
    { id: "someId", name: "Col Name", field: "myRowData.myObj.myProp", width: 40} 
.. 
]; 

var options { 
... 
dataItemColumnValueExtractor: function getItemColumnValue(item, column) { 
    var val = undefined; 
    try { 
     val = eval("item." + column.field); 
    } catch(e) { 
     // ignore 
    } 
    return val; 
    } 
}; 
+1

-1使用'eval' –

相關問題