2013-11-20 52 views
1

BackGrid有可能構建自定義單元格格式化程序,從隱藏列組成值?Backgrid格式化程序添加其他列中的值

var grid = new Backgrid.Grid({ 
    columns: [ 
    { 
    name:"half_value_1", 
    cell:"string", 
    rendered: false 
    }, 
    { 
    name:"half_value_2", 
    cell:"string", 
    rendered: false 
    }, 
    { 
    name: "composite", 
    cell: "string", 
    formatter: _.extend({}, Backgrid.CellFormatter.prototype, { 
     fromRaw: function (half_value_1, half_value_2) { 
     return half_value_1 + '/' + half_value_2; 
     } 
    }) 
    }], 
    collection: col 
}); 

我可以得到fromRaw函數內half_value_1half_value_2

回答

5

我認爲獲得所需結果的最佳方法是使用自定義單元格而不是自定義格式器。你可以爲特定列做這樣的事情:

{ 
    name: "composite", 
    cell: Backgrid.Cell.extend({ 

     render: function(){ 

      // You have access to the whole model - get values like normal in Backbone 
      var half_value_1 = this.model.get("half_value_1"); 
      var half_value_2 = this.model.get("half_value_2"); 

      // Put what you want inside the cell (Can use .html if HTML formatting is needed) 
      this.$el.text(half_value_1 + '/' + half_value_2); 

      // MUST do this for the grid to not error out 
      return this; 

     } 

    }) 
} 

這應該完全爲你工作 - 我用它的網格在我的項目屈指可數。我沒有測試這個代碼,所以我可能有錯別字:)

密鑰

+0

謝謝,關鍵。我不知道我在* render *上有完整的行模型,而在* formatter *方法上沒有。這是有道理的,因爲我正在修改數據結構而不是演示文稿。 – saski

+0

沒問題,很高興我能幫忙! :) – Key

相關問題