Knockout: Table Paging

2019-08-15 03:56发布

问题:

I tried paging under my Model. I could correctly get Arrow to move depending on how many Records I posess. But data is not refelcted/decreased/increased depending on Max rows to show on a Page.

Please find below Fiddle

Refrrence Example I used is Here

/*************
  Start of logic for Paging

  ******/


  self.items = ko.observableArray();
   this.all =  self.items;

 self.pageNumber = ko.observable(0);
    self.nbPerPage = 2;

    // I think this is somewhere I am missing the functionality.

    this.totalPages = ko.computed(function() {
        var div = Math.floor(self.all().length / self.nbPerPage);
        div += self.all().length % self.nbPerPage > 0 ? 1 : 0;
        return div - 1;
    });

this.paginated = ko.computed(function() {
        var first = self.pageNumber() * self.nbPerPage;
        return self.all.slice(first, first + self.nbPerPage);
    });

    this.hasPrevious = ko.computed(function() {
        return self.pageNumber() !== 0;
    });

    this.hasNext = ko.computed(function() {
        return self.pageNumber() !== self.totalPages();
    });

  this.next = function() {
        if(self.pageNumber() < self.totalPages()) {
            self.pageNumber(self.pageNumber() + 1);
        }
    }

  this.previous = function() {
        if(self.pageNumber() != 0) {
            self.pageNumber(self.pageNumber() - 1);
        }
    }  


/***********
End of Logic for Paging

*/

回答1:

You just need to update your view from...

<tbody data-bind="foreach: items " >

to

<tbody data-bind="foreach: paginated " >