- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Marionette 制作一个表格网格,其中包含动态行数和列数以及标题。
我想要一个如下所示的网格:http://jsfiddle.net/zaphod013/c3w61gf6/
所以有
列 = ['早餐', '午餐', '晚餐']
行 = ['碳水化合物', '蛋白质', '脂肪']
网格的其余部分是复选框。
我已经制作了列和行的 View ,但我完全不知道如何将它们放入表中,以及如何添加复选框 View 。
我的代码在 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>
脚本:
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
View 知道您需要多少复选标记列(如您所见,这将是微不足道的),我们必须在 Row
中创建这些列查看。
LayoutView
你的 FoodLayout
View 和模板是完美的。你奠定了基础。你需要填充它的是两个 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
,并更改了您的 tagName
在ItemView
至 tr
对于 Row
看法。 (注意:您将要删除 <th>
中的 #food-col-item
标签,Backbone 将为您生成它们。)
Row
中生成动态列查看现在有趣的部分来了。我们将使用 templateHelpers
在你的 Row
中打勾意见。如果你看看 docs , templateHelpers
是一个散列,让您在呈现之前将数据添加到模板。 “数据”或 JavaScript 对象可以是函数(因为函数是 JavaScript 中的一流对象)。所以,我们将使用 templateHelpers
传递我们需要复选标记的食物列,并将一个函数组合在一起,该函数将食物列作为参数,并将返回这些复选标记列的 html。
把这个 templateHelpers
在你的属性(property) 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
作为虚拟占位符。
关于javascript - Marionette-Backbone 如何制作带有动态行和列标题的网格表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28652632/
我真的不明白它们是如何有用的。原文article引入了初始化程序,这是代码示例: App = new Backbone.Marionette.Application(); App.addInitial
我正在准备使用 Backbone Marionette 迁移我的应用程序,但路由中出现错误: window.App = new Backbone.Marionette.Application
我正在尝试将 marionette.js 用于我的 Backbone 应用程序,但我对如何组织布局/区域并呈现它们感到有些困惑。 我正在寻找具有嵌套布局和区域的示例应用程序,以便我能更好地理解它。有什
我有一个带有区域的布局。当 Layout 初始化时,我希望它自动初始化一个预设 View 以进入它的区域,并在 Layout 本身显示/关闭时显示/关闭它。 当前示例来自 https://github
//当我点击 kickassessor 按钮时,它会更新数据库,但 View 不会更新。这是我的代码。 返回 Marionette.ItemView.extend( { initialize
我有需要显示/编辑的数据,这些数据似乎与 Marionette 提供的内置 View 不自然。让我举个例子来说明。 假设我想列出/编辑书籍。每本书都有一个 ISBN、一个书名和 0 个或多个标签。基本
我一直在查看各种 Marionette 问题,但没有找到我想要的,希望有人能给我一些合理的建议和一些建议。我是新手,只是想在我在 github 上选择的样板启动包上构建 https://github.
随着我的应用程序的增长,我觉得需要更结构化的路由器/ Controller 设置。 Marionette 文档提到以下内容: It is recommended that you divide you
在我的应用程序中,我添加了 Marionette.sync 插件并覆盖这些方法: Backbone.Marionette.TemplateCache.prototype.loadTemplate =
我想知道是否有可能以某种方式扩展 Marionette Layout 机制基于创建一种类似导航的堆栈。 Marionette 行为。 在区域 show() 的 View 之前,它会在当前显示的 Vie
看起来 Backbone.Marionette View 为事件等操作 DOM,所以我猜这不像更改基类引用那样微不足道,例如Backbone.View 到 BaseView。 有没有人解决过这个问题?
我正在使用 Marionette Layout .show渲染 CollectionView并想知道是否有办法检测 全部 ItemView children 已经完成了渲染? 我的代码的简化版本是:
因此,我不确定我是否完全理解应该如何触发此回调。如果您采用准系统模型、集合和 View : PatchModel = Backbone.Model.extend({}); PatchCollectio
我正在查看 Backbone Marionette,它似乎对我当前的项目很有前途。但是,无论 startWithParent 设置如何,添加模块时它们似乎都会自动启动。这是我遇到问题的一段代码(其中一
我是 Marionette 的新手,无法理解事件... 我有一个触发事件的 ItemView,我想在应用程序级别接收此事件,但是当我的应用程序监听此事件时,没有任何 react ... 如果事件聚合器
我有一个 CollectionView class MyCollectionView extends Backbone.Marionette.CollectionView itemView: My
我有一个复合 View ,在将模型添加到 View 集合时抛出以下错误:Uncaught ItemViewContainerMissingError: Missing itemViewContaine
我已经建立了一个项目,在其中扩展了包含两个不同区域的 Backbone.Marionette.Layout。该布局可以用作整个应用程序的组件。特别是,区域设置如下。 regions : { m
基本上,我试图将 CompositeView 呈现为一个简单的四列列表,其中包含一个表头,其中集合中的每个模型都呈现为一个并附加到 .我正在关注 an example of Derick's非常接近,
我正在尝试包含应用程序实例以使用其事件聚合器,如图所示 here 当我将实例包含在 View 中时,出现错误。 在 Requirejs 配置文件中启动 App.Bootloader.js: requi
我是一名优秀的程序员,十分优秀!