Please see my model below:
viewModel = [ { StudentName : 'Ronald', Reviews : [ '3/5', '2/5', '4/5'], TeacherNames : [ 'Nema', 'Sarah', 'Vilson'] }, { StudentName : 'Chris', Reviews : [ '4.5/5', '2.5/5', '3.5/5'], TeacherNames : [ 'Nema', 'Sarah', 'Vilson'] } ]
In the HTML below, I am trying to display overviews in a nested foreach structure. Reviews are displayed based on expectations. but how can I post TeacherNames with this single review? I put TeacherNames[$index] , but it does not work.
Note 1: The number of elements in the array (i.e. reviews and author names) will be the same.
Note 2: I do not want to change the structure of this JSON model, something like adding an additional variable and placing both parameters in one array.
<div data-bind="foreach:viewModel"> <span data-bind="text: StudentName"></span> <ul data-bind="foreach:Reviews"> <li> <a href="#" data-bind="text:$data">Inbox </a> <span class="ui-li-count" data-bind="text:TeacherNames[$index]">123</span> </li> </ul> </div>
Please check Fiddle .
source share