Knockout.js foreach:但仅当比较为真时 [英] Knockout.js foreach: but only when comparison is true
问题描述
如何通过比较来控制foreach忽略某些元素?
How can I control the foreach to ignore certain elements by using a comparison?
例如,我想要的是这样的东西:
What I want for example is something like this:
<div data-bind="foreach: entry where (entry.number > 10)">
所以我想做的是遍历entry
,但仅在当前entry
的number
值大于10时执行.
So what I would want it to do is loop through entry
's but only execute when that current entry
has a number
value of more than 10.
这可能吗?
推荐答案
knockout.js目前无法实现,但这是一个有趣的功能.您应该提交错误报告/与作者联系以考虑将其用于将来的版本.
Currently that's not possible with knockout.js, but it's an interesting feature. You should file a bug report/contact the author to consider it for a future version.
方法1:
<div data-bind="foreach: entry">
<div data-bind="if: entry.number > 10"></div>
</div>
方法2:
编写一个自定义过滤器方法,该方法将为您提供符合条件的元素数组,并在您的foreach
中使用它.
Way 2:
Write a custom filter method that gives you an array of elements that match your conditions and use this in your foreach
.
这篇关于Knockout.js foreach:但仅当比较为真时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!