【问题标题】:Backgrid formatter adding values from other columnsBackgrid 格式化程序从其他列添加值
【发布时间】:2013-12-04 07:43:32
【问题描述】:

是否可以使用 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 吗?

【问题讨论】:

    标签: backbone.js backgrid


    【解决方案1】:

    我认为获得所需结果的最佳方法是使用自定义单元格而不是自定义格式化程序。你可以为那个特定的列做这样的事情:

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

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

    【讨论】:

    • 谢谢,钥匙。我不知道我在 render 而不是 formatter 方法上有完整的行模型。这是有道理的,因为我正在修改数据结构而不是表示。
    • 没问题,很高兴能帮上忙! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多