javascriptindexingforeachknockout.jspagination

How can i access index of foreach when iterating through ko.computed in knockout.js


i'm building a paged list on the client side with knockout.js and im trying to output the page index with the below code so i get clickable links with numbers so people can switch page.

<ul data-bind="foreach:Paging">
 <li>
<a href="#" data-bind="click: $root.SetCurrentPage(), text: WHATTOWRITEHERE "></a>
</li>
 </ul>

In my viewmodel

this.Paging = ko.computed(function () 
{
  return ko.utils.range(1, this.TotalPages);
});

Everything works, tried just outputtung text:test and it writes test for each page but i want numbers. So the easiest way is of course to access current index in the foreach and + 1.

How would i be able to do this?


Solution

  • The problem could be with your computed ko. You have not bound it to this. So instead of:

    this.Paging = ko.computed(function ()   
    {  
      return ko.utils.range(1, this.TotalPages);  
    }); 
    

    .. try ...

    this.Paging = ko.computed(function ()  
    { 
      return ko.utils.range(1, this.TotalPages); 
    }, this); 
    

    You can then try ColinE suggestion of text: this