我按照本教程学习如何进行分页。
https://github.com/feichao/angular-material-paging
我把一个json,myData和项目放在一起,看看它是如何工作的。因为我是一名初级程序员,所以我被封锁了gotoPage()。
在我的页面中,我改变了:
ctrl.total = ctrl.myData.length;
ctrl.currentPage = 1;
ctrl.step = 6;
ctrl.gotoPage = function() {
if(ctrl.total) {
for(var i=0; i < ctrl.total; i++) {
//
}
}
};
但是我不知道在这个函数中放什么只给我看,比如我的json每页10个项目。 您可以在演示中看到这些道具和方法。
有人可以提出想法吗?
答案 0 :(得分:0)
您需要计算起始金额,然后进行迭代,直至达到该页面的最大值
ctrl.total = ctrl.myData.length; //the total items in your set
ctrl.currentPage = 1;
ctrl.step = 6; //page size? this could be 10 if you want
ctrl.numpages = ctrl.total/ctrl.step; //the total number of items partitioned by page size. convert this to a whole number (the ceiling)
//go to page function here receives the indexes for that page, is that ok?
ctrl.gotoPage = function(Page) { //i modified this to take the page number as an argument
if(Page<= ctrl.numpages && Page>0) //we need to check that the page is valid
{
var startpos = ((Page-1)*(ctrl.step))+1; //this is virtually a skip amount. If its the first page, i = 0, otherwise we start with an increment of the page size. If its the second page, we skip the first 6 results
for(var i=startpos; i < startpos+ctrl.step; i++) {
if(i == ctrl.total) //if its the last page we cut it off early
break;
}
}
};