Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout.js foreach: but only when comparison is true

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)">

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.

Is this possible to do?

like image 865
Pangolin Avatar asked Jan 15 '12 11:01

Pangolin


2 Answers

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.

Way 1:

<div data-bind="foreach: entry">
     <div data-bind="if: entry.number > 10"></div>
</div>

Way 2:
Write a custom filter method that gives you an array of elements that match your conditions and use this in your foreach.

like image 166
Björn Kaiser Avatar answered Oct 02 '22 13:10

Björn Kaiser


try this:

   <div data-bind="foreach: editingItem.columns">
         <!-- ko if: Selected-->
         <div data-bind="text: Name"></div>
          <input type="text"/>
             <!-- /ko -->
like image 43
JustMe Avatar answered Oct 02 '22 13:10

JustMe