2

Is it possible, with BackGrid, to build a custom Cell vía formatter, composing values from hidden columns?

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
});

Can I get the half_value_1 and half_value_2 inside the fromRaw function?

4

1 回答 1

6

我认为获得所需结果的最佳方法是使用自定义单元格而不是自定义格式化程序。您可以为该特定列执行以下操作:

{
    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;

        }

    })
}

这对你来说应该是完美的——我在我的项目中将它用于少数网格。不过我没有测试这段代码,所以我可能有错别字:)

钥匙

于 2013-11-27T03:49:21.443 回答