如何呈现具有一些固定列和一些动态列的表格
我想得到一个包含这些列的表格:
- [Name]
- [Club]
- [Dynamic1]
- [Dynamic2]
- [Dynamic3]
- 等等。
我尝试了这个:
<table>
<tbody data-bind="template: { name: 'rowTmpl', foreach: runners }">
</tbody>
</table>
<script id="rowTmpl" type="text/html">
<tr data-bind="template: { name: 'colTmpl', foreach: radios }" >
<td data-bind="text: name"></td>
<td data-bind="text: club"></td>
</tr>
</script>
<script id="colTmpl" type="text/html">
<td>aa</td>
</script>
@section Scripts{
<script src="/Scripts/knockout-1.3.0beta.js" type="text/javascript"></script>
<script type="text/javascript">
var vm = {
id: 1,
name: 'H21',
radios: ['2km', '4km', 'mål'],
runners: ko.observableArray([
{ name: 'Mikael Eliasson', club: 'Göteborg-Majorna OK', radios: ko.observableArray([{}, {}, {}]) },
{ name: 'Ola Martner', club: 'Göteborg-Majorna OK', radios: ko.observableArray([{}, {}, {}]) }
])
};
ko.applyBindings(vm);
</script>
}
我的问题是 colTmpl
中的 tds 不是 databoud ,它是空的,放置在带有文本“aa”的第三列之后。请参阅此小提琴。
I want to get a table with these columns:
- [Name]
- [Club]
- [Dynamic1]
- [Dynamic2]
- [Dynamic3]
- etc etc.
I tried this:
<table>
<tbody data-bind="template: { name: 'rowTmpl', foreach: runners }">
</tbody>
</table>
<script id="rowTmpl" type="text/html">
<tr data-bind="template: { name: 'colTmpl', foreach: radios }" >
<td data-bind="text: name"></td>
<td data-bind="text: club"></td>
</tr>
</script>
<script id="colTmpl" type="text/html">
<td>aa</td>
</script>
@section Scripts{
<script src="/Scripts/knockout-1.3.0beta.js" type="text/javascript"></script>
<script type="text/javascript">
var vm = {
id: 1,
name: 'H21',
radios: ['2km', '4km', 'mål'],
runners: ko.observableArray([
{ name: 'Mikael Eliasson', club: 'Göteborg-Majorna OK', radios: ko.observableArray([{}, {}, {}]) },
{ name: 'Ola Martner', club: 'Göteborg-Majorna OK', radios: ko.observableArray([{}, {}, {}]) }
])
};
ko.applyBindings(vm);
</script>
}
My problem is that the tds inside colTmpl
is not databoud, it's empty and placed after the third column with the text 'aa'. See this fiddle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 1.3 beta (您的小提琴正在引用最新版本),那么你可以这样做:
示例:http://jsfiddle.net/rniemeyer/bd7DT/
如果您需要在 1.3 之前使用 jQuery 模板执行此操作,那么您需要通过 templateOptions 并执行
{{if}}
来检查您是否位于第一个无线电并渲染两个单元格。 jQuery 模板中的另一个选项是在动态单元格周围使用{{each}}
,而不是父级模板绑定的foreach
选项。如果您的列经常动态变化,您会损失一些效率。如有必要,我可以提供这两个选项的示例。If you are using 1.3 beta (your fiddle is referencing the latest build), then you can do this:
Sample here: http://jsfiddle.net/rniemeyer/bd7DT/
If you need to do this prior to 1.3 with jQuery templates, then you would want to pass the first item in your array into the template via templateOptions and do an
{{if}}
to check if you are on the first radio and render the two cells. Another option in jQuery templates is to use{{each}}
around your dynamic cells rather than theforeach
option of the template binding on the parent. You would lose some efficiency, if your columns are frequently changing dynamically. I can provide a sample for these two options, if necessary.这是因为
的内容被您指定的模板替换。
如果您这样做:
它将呈现。
It is because of the fact that the content of
<tr data-bind="template: { name: 'colTmpl', foreach: radios }" >
is getting replaced by the template you specify.If you instead do:
It will render.