获取嵌套for-each binding knockout.js中的迭代索引

时间:2017-01-06 11:39:52

标签: knockout.js

我想在ParentArray代码中获取td的索引。代码绑定ChildArray索引下面。我该怎么做?

<table class="table-container-inner">
    <tbody data-bind="foreach:ParentArray">

        <tr data-bind="foreach:ChildArray">
            <td data-bind="text:$index"></td>
        </tr>

    </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

$index将获取ChildArray中的索引。在foreach:ChildArray内,$parentContext.$index()将获得ParentArray的索引

<table class="table-container-inner">
    <tbody data-bind="foreach:ParentArray">

        <tr data-bind="foreach:ChildArray">
            <td data-bind="text:$index">
              <span data-bind="text: $parentContext.$index()"></span>
            </td>
        </tr>

    </tbody>
</table>