Knockout.js - foreach 绑定 - 测试是否是最后一个元素
我正在使用以下模板:
<div class="datatypeOptions" data-bind="if: $data.datatypeTemplate().allowOptions">
<h3>Allowed responses</h3>
<p data-bind="if: $data.datatypeTemplate().datatypeOptions().length == 0">There are no responses for this question, yet. <a href="#" data-bind="click: function(d, e){$root.addDatatypeOption($data.datatypeTemplate());}">Add one</a>
<ul data-bind="foreach: $data.datatypeTemplate().datatypeOptions()">
<li>
<a href="#" data-bind="text: name, click: $root.selectedDatatypeOption, visible: $data !== $root.selectedDatatypeOption()"></a>
<input data-bind="value: name, visibleAndSelect: $data === $root.selectedDatatypeOption(), event: { blur: $root.clearDatatypeOption }, executeOnEnter: { callback: function(){ $root.addDatatypeOption($parent.datatypeTemplate()); } }" />
//I want to show this a tag only if $data is the last element in the array.
Problem here ===> <a href="#" data-bind="if: $data == $parent.datatypeTemplate().datatypeOptions()[ $parent.datatypeTemplate().datatypeOptions().length - 1 ], click: function(d, e){$root.addDatatypeOption($data.datatypeTemplate());}"><img src='/static/img/icons/custom-task-wizard/black/plus_12x12.png' title='Add option'></a>
</li>
</ul>
</div>
我在控制台中收到此错误:
Uncaught Error: Unable to parse bindings.
Message: TypeError: Object [object Object] has no method 'datatypeTemplate';
Bindings value: if: $data == $parent.datatypeTemplate().datatypeOptions()[ $parent.datatypeTemplate().datatypeOptions().length - 1 ], click: function(d, e){$root.addDatatypeOption($data.datatypeTemplate());}
如果传递的元素位于数组中的最后一个,则将函数添加到我的视图模型中,该函数返回 true/false 是我唯一的选择吗?
I am using the following template:
<div class="datatypeOptions" data-bind="if: $data.datatypeTemplate().allowOptions">
<h3>Allowed responses</h3>
<p data-bind="if: $data.datatypeTemplate().datatypeOptions().length == 0">There are no responses for this question, yet. <a href="#" data-bind="click: function(d, e){$root.addDatatypeOption($data.datatypeTemplate());}">Add one</a>
<ul data-bind="foreach: $data.datatypeTemplate().datatypeOptions()">
<li>
<a href="#" data-bind="text: name, click: $root.selectedDatatypeOption, visible: $data !== $root.selectedDatatypeOption()"></a>
<input data-bind="value: name, visibleAndSelect: $data === $root.selectedDatatypeOption(), event: { blur: $root.clearDatatypeOption }, executeOnEnter: { callback: function(){ $root.addDatatypeOption($parent.datatypeTemplate()); } }" />
//I want to show this a tag only if $data is the last element in the array.
Problem here ===> <a href="#" data-bind="if: $data == $parent.datatypeTemplate().datatypeOptions()[ $parent.datatypeTemplate().datatypeOptions().length - 1 ], click: function(d, e){$root.addDatatypeOption($data.datatypeTemplate());}"><img src='/static/img/icons/custom-task-wizard/black/plus_12x12.png' title='Add option'></a>
</li>
</ul>
</div>
I get this error in the console:
Uncaught Error: Unable to parse bindings.
Message: TypeError: Object [object Object] has no method 'datatypeTemplate';
Bindings value: if: $data == $parent.datatypeTemplate().datatypeOptions()[ $parent.datatypeTemplate().datatypeOptions().length - 1 ], click: function(d, e){$root.addDatatypeOption($data.datatypeTemplate());}
Is my only option to add a function to my viewmodel that returns true/false if the passed element is last in the array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经简化了问题,但是这个 jsFiddle 演示了一种可能的解决方案。
“if”绑定:
无容器“if”绑定:
您可以在
foreach
绑定中使用$index
来获取当前绑定项的索引。用它来与父级的原始集合进行比较。有关绑定上下文的更多信息,请参阅此处。
I've simplified the problem, but this jsFiddle demonstrates a possible solution.
"if" binding:
Containerless "if" binding:
You can use
$index
within aforeach
binding to get the index of the currently bound item. Use that to compare against the original collection of the parent.See HERE for more information regarding binding contexts.
我注意到有许多请求增强 KO 以支持
$length
、$last
或$array
中的保留属性code>foreach 绑定,尽管由于各种原因(通常是性能),它们还没有进入代码库。如果有人有兴趣使用自定义绑定针对特定情况公开这些元素,这里有一个公开
$length
变量以打印“漂亮”列表的简单示例。您只需在通常使用
foreach
的任何地方使用forEachWithLength
即可。用法示例:
输入:
["One", "Two", "Three", "Four"]
输出:
One,二、三、四
小提琴
进一步阅读
I noticed there are a number of requests for enhancements to KO to support the
$length
,$last
, or$array
reserved properties in theforeach
binding although, for a variety of reasons (often performance), they have not made it into the codebase.If anyone is interested in exposing these elements for a specific case using a custom binding, here is a simple example of exposing the
$length
variable in order to print a "pretty" list.You simply use
forEachWithLength
anywhere you would normally useforeach
.Example usage:
Input :
["One", "Two", "Three", "Four"]
Output :
One, Two, Three and Four
Fiddle
Further reading
如果您不在
foreach
绑定中使用as
选项,那么请转到此问题得票最多的答案。如果您在
foreach
绑定中DO使用as
运算符。那么这个答案将不起作用。这是这种情况下的解决方案
秘诀是用您正在使用的可观察数组的名称替换
$parent.data()
。在我的例子中,它被命名为
Items
,因此我将$parent.data()
替换为$parent.Items ()
注意此解决方案在所有情况下都有效(无论您是否在 foreach 中使用
as
选项)If you are NOT using
as
option in theforeach
binding, then go to the most-upvoted answer to this question.If you are DO using
as
operator in theforeach
binding. then that answer will NOT work.Here is the solution in that case
The secret with replacing the
$parent.data()
with the name of the observable array you are using.In my case it was named
Items
, so I replaced the$parent.data()
with$parent.Items()
Note this solution is working in all cases, (in case you are using
as
option in foreach or not)请尝试以下操作:
$root
而不是$parent
Try the following:
$root
instead of$parent