javascript - Create numbered list in KnockoutJS using each item's position in an observable array for the numbers -
i create numbered list , use knockout bind data. data binding works fine, unable come way smoothly generate numbers based on position in observable array. observable array may vary in future, knockout dynamically handle numbering of list.
here html:
<ul class="nav nav-list" data-bind="foreach: sidebaritems"> <li class="" data-bind="css: isactive"> <a href="#dropdowns" data-bind="text: text"></a> </li> </ul>
here javascript code:
self.sidebaritems = ko.observable([ {text: 'option'}, {text: 'option'}, {text: 'option'}, {text: 'option'}, {text: 'option'} ]);
i list say:
1 - option
2 - option
3 - option
etc.
see documentation: http://knockoutjs.com/documentation/foreach-binding.html
use $index
:
<ul class="nav nav-list" data-bind="foreach: sidebaritems"> <li class="" data-bind="css: isactive"> <a href="#dropdowns" data-bind="text: $index() + text()"></a> </li> </ul>
Comments
Post a Comment