dafriskymonkey
dafriskymonkey

Reputation: 2189

knockout js : foreach binding adding a static element

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.

Upvotes: 10

Views: 2161

Answers (1)

Chirag Arvadia
Chirag Arvadia

Reputation: 1200

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>

Upvotes: 29

Related Questions