Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find nested foreach index number

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.

like image 369
shailendra kumar Avatar asked Oct 14 '25 18:10

shailendra kumar


2 Answers

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

like image 151
super cool Avatar answered Oct 18 '25 09:10

super cool


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/

like image 27
Wayne Ellery Avatar answered Oct 18 '25 10:10

Wayne Ellery



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!