敲门'绑定'?

Zho*_*len 10 javascript knockout-2.0 knockout.js

原始问题

是否有任何人拥有或知道敲除的绑定会允许类似于for循环的行为?我可以让foreach做我想做的事,但如果我没有这样做就会很好.

编辑2

我试图根据用户的选择创建表行.在某些情况下,我需要x行,其中x是数组的长度,其他时间x表示显示n个数组所需的最大行数.

例如:image1是基于4个不同的阵列构建的,所有阵列的大小各不相同image2是从同一个阵列构建的,在这种情况下加倍.

在此输入图像描述 在此输入图像描述

<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>
Run Code Online (Sandbox Code Playgroud)

以上工作正常并创建我想要的,但我不喜欢必须将selectedTab.runners从一个数字转换为一个空数组,只是为了让它循环n次来创建行.我愿意接受建议.注意截至我最初发布此问题的时候,我已经对此代码进行了相当大的修改,现在只涉及与我的初始问题相关的一个问题.

Mic*_*est 13

我的重复绑定正是这样做的.

<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>
Run Code Online (Sandbox Code Playgroud)


Leo*_*huk 5

您可以创建一个Array对象:

  <!-- ko foreach: new Array(the_length_you_need) -->
       <span>&#9733;</span>
  <!-- /ko -->
Run Code Online (Sandbox Code Playgroud)

这将在the_length_you_need次打印星星