$ knockout foreach绑定中的$ index + 1 [英] $index+1 in Knockout foreach binding
问题描述
我需要在表中显示$index+1
.
如果我只使用$index
,所有元素将从0开始,那么我需要从1开始.
If I just use the $index
all the elements will start from 0, I need to start at 1.
以下是淘汰赛的文档: http://knockoutjs.com/documentation/foreach-binding. html
Here's the documentation of knockout: http://knockoutjs.com/documentation/foreach-binding.html
在这里您可以找到以下示例:
In there you can find this example:
<h4>People</h4>
<ul data-bind="foreach: people">
<li>
Name at position <span data-bind="text: $index"> </span>:
<span data-bind="text: name"> </span>
<a href="#" data-bind="click: $parent.removePerson">Remove</a>
</li>
</ul>
<button data-bind="click: addPerson">Add</button>
因此它将显示以下内容:
So it will display the following:
人
0位置的名称:Bert Remove
Name at position 0: Bert Remove
在位置1的姓名:Charles Remove
Name at position 1: Charles Remove
第2位的姓名:Denise Remove
Name at position 2: Denise Remove
我真的需要这个只是为了显示目的.
I really need this to be just for display purposes.
位置1的名称:移除Bert
Name at position 1: Bert Remove
第2位的姓名:查尔斯·删除
Name at position 2: Charles Remove
第3位的姓名:Denise Remove
Name at position 3: Denise Remove
我没有成功尝试过<span data-bind="text: ($index + 1)"> </span>
推荐答案
$ index是可观察的. 因此,您需要以这种方式使用它:
$index is an observable. So you need to use it this way :
<span data-bind="text: ($index() + 1)"> </span>
这篇关于$ knockout foreach绑定中的$ index + 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!