Knockout: Table Paging

半城伤御伤魂 提交于 2019-12-11 08:46:07

问题


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 " >


来源:https://stackoverflow.com/questions/51638095/knockout-table-paging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!