Find the last iteration of foreach data-bind in knockout js

css, foreach, html, knockout.js

Solution

You can check if you are displaying the first element.

<div data-bind="foreach: items">
   <hr data-bind="visible : $index()!=0" /> 
   <span data-bind="text: $data"></span>
</div>

See fiddle

Or as RP Niemeyer said, you can omit the last hr :

<div data-bind="foreach: items">
   <span data-bind="text: $data"></span>
   <hr data-bind="visible : $index() != ($parent.length-1)" /> 
   // notice the hr is after the item.
</div>

Problem

Is there a way to find last iteration using foreach data-bind in knockout js? My problem is, I am iterating over a list of items and want to print all items separated by a line. I don't want to draw a line(hr) for the last item of that array.

Original source

Related problems