木偶骨干 如何制作带有动态行和列标题的网格表

Marionette-Backbone how do I make a grid table with a dynamic row and column headers

本文关键字:标题 网格 动态 作带 何制      更新时间:2024-03-30

我正在尝试使用木偶制作一个具有动态行数和带有标题的列的表格网格。

我想要一个看起来像这样的网格:http://jsfiddle.net/zaphod013/c3w61gf6/

所以有

列 = ["早餐"、"午餐"、"晚餐"]

行 = ["碳水化合物"、"蛋白质"、"脂肪"]

网格的其余部分是复选框。

我已经为列和行制作了视图,但我对如何将它们放入表中以及如何添加复选框视图感到相当迷茫。

我拥有的代码在 http://jsfiddle.net/zaphod013/qkctrLxn/

html:

<div id="main-region"></div>
<script id="food-table" type="text/template">
    <thead id="column-id">
    </thead>
    <tbody id="row-id">
    </tbody>
</script>
<script id="food-col-item" type="text/template">
    <th><%= col %></th>
</script>
<script id="food-row-item" type="text/template">
    <td><%= row %></td>
</script>

script:

FoodManager = new Backbone.Marionette.Application();
FoodManager.addRegions({
    mainRegion: "#main-region",
});
FoodManager.FoodLayout = Backbone.Marionette.Layout.extend({
    template: "#food-table",
    regions: {
       colRegion:"#column-id",
       rowRegion:"#row-id"
    }
});
FoodManager.Col = Backbone.Model.extend({});
FoodManager.ColCollection = Backbone.Collection.extend({
                                    model: FoodManager.Col
                                });
FoodManager.Row = Backbone.Model.extend({});
FoodManager.RowCollection = Backbone.Collection.extend({
                                    model: FoodManager.Row
                                });
FoodManager.ColItemView = Marionette.ItemView.extend({
    template: "#food-col-item",
    tagName: "th",
});
FoodManager.ColView = Marionette.CompositeView.extend({
    template: "#food-table",
    tagName: "thead",
    itemView: FoodManager.ColItemView
});
FoodManager.RowItemView = Marionette.ItemView.extend({
    template: "#food-row-item",
    tagName: "th",
});
FoodManager.RowView = Marionette.CompositeView.extend({
    template: "#food-table",
    tagName: "table",
    itemView: FoodManager.RowItemView
});
FoodManager.on("initialize:after", function(){
    var columns = new FoodManager.ColCollection([
                    {col: 'Breakfast'},
                    {col: 'Lunch'},
                    {col: 'Dinner'}
            ]);
    var rows = new FoodManager.RowCollection([
                    {row: 'Carbs'},
                    {row: 'Protein'},
                    {row: 'Fats'}
            ]);
    var colListView = new FoodManager.ColView({
                            collection: columns
                        });
    var rowListView = new FoodManager.RowView({
                            collection: rows
                        });
    var foodLayout = new FoodManager.FoodLayout();    
    foodLayout.render();
    FoodManager.colRegion.show(colListView);
    FoodManager.rowRegion.show(rowListView);
    FoodManager.mainRegion.show(foodLayout);
});
 FoodManager.start();

我将非常感谢有关如何执行此操作的一些指示。

感谢您通读。

这个答案有两个部分。首先,我建议您将LayoutViewCollectionViews一起使用,因为您不需要集合本身的模板(不过,您仍将使用ItemView模板(。其次,您必须让您的Row视图知道您需要多少个复选标记列(正如您将看到的那样,这将是微不足道的(,我们必须在Row视图中创建这些列。

加载您的LayoutView

您的FoodLayout视图和模板是完美的。你奠定了基础。您需要填充它是两个CollectionView视图:

FoodManager.ColItemView = Marionette.ItemView.extend({
    template: "#food-col-item",
    tagName: "th",
});
FoodManager.ColView = Marionette.CollectionView.extend({
    itemView: FoodManager.ColItemView
});
FoodManager.RowItemView = Marionette.ItemView.extend({
    template: "#food-row-item",
    tagName: "tr",
});
FoodManager.RowView = Marionette.CollectionView.extend({
    itemView: FoodManager.RowItemView
});

请注意,我将您的CompositeView更改为CollectionView,并将您是ItemView中的tagName更改为tr Row视图。(注意:您将要删除#food-col-item中的<th>标签,Backbone将为您生成它们。

在视图中生成动态列Row

现在有趣的部分来了。我们将使用templateHelpersRow视图中创建复选标记行。如果您要查看文档,templateHelpers是一个哈希,可让您在呈现之前将数据添加到模板中。"数据"或JavaScript对象可以是函数(因为函数是JavaScript中的第一类对象(。因此,我们将使用 templateHelpers 来传递我们需要复选标记的食物列,并将一个函数放在一起,该函数将食物列作为参数,并将返回这些复选标记列的 html。

将此templateHelpers属性放在您的FoodManager.FoodLayout视图中:

templateHelpers: function () {
  return {
    foods: function () {
      return this.foodColumns
    },
    addColumns: function (foodcols) {
      var html = '';
       for (food in foodcols)
         html += "<td><input type="checkbox" class=" + 
                    food + "-check"></td>";
       return html;
    }
  }
} 

您的Row模板将如下所示:

<script id="food-row-item" type="text/template">
    <td><%= row %></td><% addColumns(foods) %>
</script>

您需要注意的是,您需要为FoodManager.FoodLayout提供用于ColCollection的食物列,以便您可以填充this.templateHelpers.foods。有多种方法可以做到这一点。我只是用this.foodColumns作为虚拟占位符。