问题描述
原始问题
是否有任何人拥有或知道针对淘汰赛的绑定会允许类似于for循环的行为?我可以让foreach做我想做的事情,但如果我没有这样做就会很好。
Does any one have or know of a binding for knockout that would allow behavior similar to a for loop? I can make a foreach do what I want but it would be nice if I didn't have to do it that way.
编辑2
我正在尝试根据用户的选择创建表行。在某些情况下,我需要x行,其中x是数组的长度,其他时间x代表显示n个数组所需的最大行数。
I am trying to create table rows based on a selection the user makes. In some cases I need x rows where x is the length of an array, other times x represents the largest number rows that will be needed to display n number of arrays.
例如:image1是基于4个不同的数组构建的,所有数组的大小各不相同
image2是从同一个数组构建的,在这种情况下加倍。
Ex: image1 is built based on 4 different arrays all which vary in sizeimage2 is built from the same array and is doubled in this case.
<div data-bind="if: selectedTab()">
<table>
<thead>
<tr>
<td>
<div class="a-i-post-All"></div>
</td>
<!-- ko foreach:$root.selectedTab().races-->
<td>
<input type="checkbox" />
</td>
<!-- /ko -->
</tr>
</thead>
<tbody data-bind="foreach: selectedTab().runners"> // <-- This is an empty array created by the max number of Runners in the selectedTabs array of Races
<tr>
<td>
<div class="a-i-post"></div>
</td>
<!-- ko foreach:$root.selectedTab().races-->
<td>
<!-- ko if: Runners.length > $parentContext.$index()-->
<input type="checkbox" />
<!-- /ko -->
</td>
<!-- /ko -->
</tr>
</tbody>
以上工作正常并创造了我想要的东西,但我不喜欢转向selectedTab。从一个数字到一个空数组的runners只是为了让它循环n次来创建行。我愿意接受建议。 注意当我最初发布此问题时,我已经对此代码进行了相当大的修改,现在只涉及与我的初始问题相关的一次。
The above works fine and creates what i want, but i don't like having to turn selectedTab.runners from a number into an empty array just to make it loop n times to create the rows. I am open for suggestions. Note As of the time I posted this question originally I have revised this code considerably and am now down to only one occurrence related to my initial question.
推荐答案
我的绑定就是这样做的。
My Repeat binding does exactly this.
<tbody>
<tr data-bind="repeat: { foreach: selectedTab().runners, index: '$runner' }">
<td>
<div class="a-i-post"></div>
</td>
<td data-bind="repeat: selectedTab().races">
<!-- ko if: $item().Runners.length > $runner -->
<input type="checkbox" />
<!-- /ko -->
</td>
</tr>
</tbody>
这篇关于敲门'绑定'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!