Reputation: 3931
I am trying to apply foreach inside foreach in knockout but it's not working outer foreach is working fine for me but for inner foreach is it saying MyFMnu1 is undefined
.
If i put this inner foreach outside then it is working but why it is not working inside the foreach.
<div data-bind="foreach: MyFmnu">
<!-- ko if: key === 'ASM' -->
<input type="checkbox" value="ASM" class="styled"
name="fldMenuCheck_Acc1" id="fldMenuCheck_Acc1"
style="width:10%;"/>
<!-- /ko -->
<div data-bind="foreach: MyFmnu1">
<!-- ko if: key === 'ASMS' -->
<input type="checkbox" value="ASMS" class="styled"
name="fldMenuCheck_Acc12" id="fldMenuCheck_Acc12"
style="width:10%;"/>
<!-- /ko -->
</div>
</div>
My view model
var AccountsViewModel = function () {
var self = this;
self.myfavmenu = ko.observable();
self.PerLzdmenuList = ko.observable();
self.MyPerlzdMenus = ko.observable();
self.myfavmenuOtherCount = ko.observable();
self.MyFmnu = ko.observableArray([]);
};
Upvotes: 0
Views: 3333
Reputation: 2313
Inside foreach binding the binding context is item of collection, not the root view model. To get access to parent context in foreach binding use $parent, like this:
<div data-bind="foreach: MyFmnu">
<!-- ko if: key === 'ASM' -->
<input type="checkbox" value="ASM" class="styled"
name="fldMenuCheck_Acc1" id="fldMenuCheck_Acc1"
style="width:10%;"/>
<!-- /ko -->
<div data-bind="foreach: $parent.MyFmnu1">
<!-- ko if: key === 'ASMS' -->
<input type="checkbox" value="ASMS" class="styled"
name="fldMenuCheck_Acc12" id="fldMenuCheck_Acc12"
style="width:10%;"/>
<!-- /ko -->
</div>
</div>
more info here: http://knockoutjs.com/documentation/binding-context.html
Upvotes: 2
Reputation: 835
If you have a MyFmnu1
array as a property for each object in the MyFmnu
array, it will work.
Upvotes: 0