【发布时间】:2017-10-24 05:32:51
【问题描述】:
我正在尝试使用主干创建代码。用户将文本输入到项目和价格两个字段中。然后将此信息附加到表中,并为输入的每个输入对创建一个新行。
我已经为表格创建了一个模板。创建视图时的问题,因为项目和价格的文本字段中的数据需要分类到相应的单元格中,我如何正确附加新行。我可以只查看该行吗?还是我需要为每个单元格都这样做?
<div id="container">
<script id="grocery-list-template" type="text/template">
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td><%=something to get item%></td>
<td><%=something to get the price%></td>
<td></td>
<td><button class="complete"></button><button class="remove"></td>
</tr>
</tbody>
</table>
</script>
</div>
这基本上是我想要做的,但我不知道如何指定行和单元格?我是否必须使用 tagname: td 为每个单元格创建一个视图,或者我可以只保留 tagname: tr 并将数据路由到适当的单元格中吗?我找到了一个示例,它只是使用 li 元素附加了一个列表,所以我试图将其中的一些内容调整到我的代码中
var TableView = Backbone.View.extend({
tagname: "tr",
classname: "itemAndprice",
template:_.template($("grocery-list-template").html()),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
this.$el.attr("id", this.model.id);
if (this.model.get('done')) this.$el.addClass('done');
$("#GroceryList").append(this.$el);
return this;
},
【问题讨论】:
-
尝试查看此示例小提琴jsfiddle.net/jrsalunga/EYWD7/4
-
这个例子真的很有帮助
-
没问题!快乐编码! =)
标签: javascript jquery backbone.js html-table underscore.js