2013-04-23 57 views
1

我使用KnockOut可觀察數組來填充wijgrid。在wijgrid中,我想使用JavaScript對象作爲某些單元格的值。不幸的是,似乎wijmo將對象轉換爲它自己模型中的字符串。是否有可能在wijgrid中有對象類型的單元格?

請有一個look at this example。我想在表格中顯示車主名稱,但我還需要保留id(和模型數據結構)。

淘汰賽視圖模型

var someData =[ { AssetCode: "Truck 5", 
       Owner: { 
       id: 1, 
       name: 'Pete'}, 
       VIN: "T3SN2ADN", 
       Odo:9, 
       TimeStamp: "2012-07-21T09:13:12Z"}, 
      { AssetCode: "Car 8", 
       Owner: { 
       id: 3, 
       name: 'Brian'}, 
       VIN: "COFAQ211", 
       Odo: 433299, 
       TimeStamp: "2012-07-17T15:34:54Z"}]; 

function ViewModel() { 
    var self = this; 
    self.gridData = ko.observableArray(someData); 
} 

ko.applyBindings(new ViewModel()); 

的wijgrid

<table id="t1" data-bind="wijgrid: { 
    data: gridData, 
    columns: [ 
    { headerText: 'Asset Code', dataKey: 'AssetCode', dataType: 'string'}, 
    { headerText: 'Owner name', dataKey: 'Owner'},   <!-- PROBLEM LINE --> 
    { headerText: 'VIN', dataKey: 'VIN', dataType: 'string' }, 
    { headerText: 'Odometer', dateKey: 'Odo', dataType: 'number' }, 
    { headerText: 'Time', dataKey: 'TimeStamp', dataType: 'datetime', dataFormatString: timePattern } 

]}"></table> 

我已經試過:

  • 標準的淘汰賽方式:{ headerText: 'Owner name', dataKey: 'Owner.name'}
  • 創建自定義cellFormatter:{ headerText: 'Owner name', dataKey: 'Owner', cellFormatter: MY_FORMATTER}

我已經試過幾乎所有我能想到的得到這個工作,但似乎wijmo這裏非常剛性....

此外,當我調試在Chrome中,似乎wijmo已經在格式化之前將對象轉換爲自己模型中的字符串。這不是非常有用..

編輯 - 我們正在使用Wijmo 2.3.9。我們在Wijmo 3上遇到了性能問題。*到目前爲止,升級並不是迫在眉睫。

回答

0

好吧,事實證明,您可以使用cellFormatter作爲對象獲取單元格值...要使其起作用,請指定dataKey屬性,請勿指定屬性。下面是修改後的代碼:

<table id="t1" data-bind="wijgrid: { 
    data: gridData, 
    columns: [ 
     { headerText: 'Asset Code', dataKey: 'AssetCode', dataType: 'string'}, 
     { headerText: 'Owner name', cellFormatter: MY_FORMATTER},   <!-- FIXED LINE --> 
     { headerText: 'VIN', dataKey: 'VIN', dataType: 'string' }, 
     { headerText: 'Odometer', dateKey: 'Odo', dataType: 'number' }, 
     { headerText: 'Time', dataKey: 'TimeStamp', dataType: 'datetime', dataFormatString: timePattern } 
]}"></table> 

而下面的JS:

var MY_FORMATTER = function(args) { 
    if (args.row.data && args.row.dataRowIndex >= 0) { 
    args.formattedValue = args.row.data.Owner.name; 
    } 
}; 
0

您可以設置自定義值的任何單元格(或顯示車主在您的情況名)通過處理cellStyleFormatter這樣的:

cellStyleFormatter: function (args) { 
//check for specific column and header row 
if (args.column.headerText == "Owner name" && args.row.dataRowIndex >= 0) { 
    //set the custom value to cell i.e. vehicle owner name 
    args.$cell.text(args.row.data.Owner.name); 
} 
} 

有關CellStyleFormatter的更多信息,你可以參考:http://wijmo.com/wiki/index.php/Grid#cellStyleFormatter

+0

不幸的是,'cellStyleFormatter'從相同的問題'cellFormatter'受到影響。 'args.row.data.Owner'的值是:「[object Object]」。即它是一個字符串不是所有者對象。這對我不起作用。 :( – Muel 2013-05-03 00:31:16

+0

雖然我忘了說謝謝你的答案,如果你有其他想法,我很樂意嘗試一下! – Muel 2013-05-03 00:31:59

相關問題