3.分頁處理-Node部分
這邊把分頁所需要的變數算出來 主要是
- items(頁數)
- currentpage(目前頁數)
- next(下一頁)
- prev(上一頁)
- total(總筆數)
- limit(每頁的資料筆數)
這裡是用bookshelf寫的,mongodb其實也是要算這些東西。
存在pagination傳回client。
generatePagination: function (totalRecords) { var self = this; var totalpages = Math.ceil(totalRecords / self.limit); var groups = Math.ceil(totalpages / self.paginationLimit); var currentpage = self.currentpage; var items = []; var lastpage = totalpages; var next = currentpage < lastpage ? currentpage + 1 : 1; var prev = currentpage < 2 ? lastpage : currentpage - 1; var isFirstPage = currentpage === 1; var isLastPage = currentpage === lastpage; var highestF = currentpage + 2; var lowestF = currentpage - 2; var counterLimit = totalpages - 2; if (groups > 1) { items.push(1); items.push(2); // if our current page is higher than 3 if (lowestF > 3) { items.push('...'); //lets check if we our current page is towards the end if (lastpage - currentpage < 2) { lowestF -= 3; // add more previous links } } else { lowestF = 3; // lowest num to start looping from } for (var counter = lowestF; counter < lowestF + 5; counter++) { if (counter > counterLimit) { break; } items.push(counter); } // if current page not towards the end if (highestF < totalpages - 2) { items.push('...'); } items.push(lastpage - 1); items.push(lastpage); } else { // no complex pagination required for (var counter2 = 1; counter2 <= lastpage; counter2++) { items.push(counter2); } } self.pagination = { items: items, currentpage: currentpage, base: self.base, isFirstPage: isFirstPage, isLastPage: isLastPage, next: next, prev: prev, total: totalRecords, limit: self.limit }; return self.pagination; }