Getting $index of grand parent in a nested loop

Getting $index of grand parent in a nested loop

Asked on December 20, 2018 in knockout js.
Add Comment


  • 1 Answer(s)

        For showing the  index form the grand-parent The user will require this $parentContext of the $parentContext , so The user will write like this :

    Row index: <span data-bind="text: $parentContext.$parentContext.$index()"></span>
    

        It is not working in the css binding due to the user have the binding on same element as the foreach so the binding context is not correctly place at the  point.

        Rectify this by adjusting  the foearch and the css on various elements like using the contain less binding syntax :

    <div class="loop" data-bind="foreach: rows">
        <div class="nested-loop" data-bind="foreach: cells">
            <!-- ko foreach: candidates -->
                <div class="nested-nested-loop"
                    data-bind="css : {selected :
                        $root.isSelected($parentContext.$parentContext.$index(),
                        $parentContext.$index(), $index())}">
                    Candidate 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>
                </div>
            <!-- /ko -->
        </div>
    </div>
    
    Answered on December 20, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.