木偶骨干 如何制作带有动态行和列标题的网格表
Marionette-Backbone how do I make a grid table with a dynamic row and column headers
我正在尝试使用木偶制作一个具有动态行数和带有标题的列的表格网格。
我想要一个看起来像这样的网格: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();
我将非常感谢有关如何执行此操作的一些指示。
感谢您通读。
这个答案有两个部分。首先,我建议您将LayoutView
与CollectionViews
一起使用,因为您不需要集合本身的模板(不过,您仍将使用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
现在有趣的部分来了。我们将使用templateHelpers
在Row
视图中创建复选标记行。如果您要查看文档,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
作为虚拟占位符。
- ng点击ag网格标题模板内部
- 网格视图:防止行移动到标题行上方
- 冻结动态网格的标题行
- UI网格AngularJS-如何更新UI网格中的列标题
- 第一页加载时,有角度的ng网格固定标题不是静态的
- 页面加载后,将FooTable断点应用于网格视图中的列标题
- 标题中大写字母之间的角度UI网格空间
- 木偶骨干 如何制作带有动态行和列标题的网格表
- 角度高图表和ng网格标题不考虑帧宽度
- jqGrid-如何获取当前网格的标题
- 剑道网格列标题
- 光滑的网格标题行被搞砸了
- 如何将剑道网格标题文本保持在顶部
- 滚动时修复网格视图标题的位置
- ExtJS 6 网格无法正确更新/刷新列标题
- 如何将工具提示 z 索引设置为大于剑道网格标题
- 如何在网格数据绑定为动态时为 jqgrid 单元格设置自定义标题
- Kendo UI网格-单元格上的标题属性
- 如何有一个淘汰网格显示标题和基于下拉结果的数据
- 如何创建一个简单的可重用网格,只在存储和标题上有所不同