I'm trying to find index number in a nested foreach. For example, I'm trying to do this:
<!-- ko foreach:array1 -->
<!-- ko foreach:array2 -->(this index)
<!-- ko foreach:array3 -->
<!-- ko foreach:array4 -->
----( i want index of foreach array2 in here...)
<!-- /ko -->
<!-- /ko -->
<!-- /ko -->
<!-- /ko -->
I tried with $parentContext.$index()
, with which I'm able to find array index of 1 level of nested foreach, but not more than 1 level.
Please help me to find correct binding context for the above scenario.
In addition to wayne ellery
answer if you want to know/compare things in a detailed way check this out .
Check here for working fiddle
View :
<div class="loop" data-bind="foreach: rows">
<br/>
<div class="nested-loop" data-bind="foreach: cells">
<br/>
<div class="nested-nested-loop" data-bind="foreach: candidates"> index: <span data-bind="text: $index()"></span> ||
Cell index: <span data-bind="text: $parentContext.$index()"></span>||
Row index: <span data-bind="text: $parentContext.$parentContext.$index()"></span>
<br/>
</div>
</div>
</div>
This will really help you to understand things better well indeed helped me once
As Super cool mentioned above you can use $parentContext.$parentContext.$index()
<!-- ko foreach:array1 -->
<!-- ko foreach:array2 -->
<!-- ko foreach:array3 -->
<!-- ko foreach:array4 -->
<!-- ko text: $parentContext.$parentContext.$index() --><!-- /ko -->
<!-- /ko -->
<!-- /ko -->
<!-- /ko -->
<!-- /ko -->
http://jsfiddle.net/9k8a7cx2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With