Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout js : foreach binding adding a static element

Tags:

knockout.js

I want to do some paging for a list of observables. I use bootstrap for the styling, and in their documentation they use unsorted list to display the links for the pages.

Let's suppose we have the following code in the view:

<ul class="pagination" data-bind="foreach : ko.utils.range(1, 10)">
    <li><a href="#" data-bind="text : $data"></a></li>
</ul>

This code will display this:

<ul class="pagination">
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    ...
    <li><a href="#">10</a></li>
</ul>

The question : How with knockout can I add static <li> at the top and the bottom of the unsorted list that will link to previous and next pages? This must be the displayed html:

<ul class="pagination">
    <li><a href="#">previous</a></li>
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    ...
    <li><a href="#">10</a></li>
    <li><a href="#">next</a></li>
</ul> 

Thank you.

like image 426
dafriskymonkey Avatar asked Oct 16 '13 03:10

dafriskymonkey


1 Answers

you can use below syntax..

<ul class="pagination">
    <li><a href="#">previous</a></li>
    <!-- ko foreach : ko.utils.range(1, 10) -->
    <li><a href="#" data-bind="text : $data"></a></li>
    <!-- /ko -->
    <li><a href="#">next</a></li>
</ul>
like image 167
Chirag Arvadia Avatar answered Oct 22 '22 22:10

Chirag Arvadia