分页计算算法

时间:2016-09-23 23:24:25

标签: javascript node.js algorithm

我正在尝试计算分页:

var pagination = {
 total: result.length,
 per_page: itemsPerPage,    // required
 current_page: currentPage, // required
 last_page: users.length / itemsPerPage,    // required
 from: (itemsPerPage * pageNumber) + 1,
 to: itemsPerPage * (pageNumber + 1)           //required
};

假设结果长度为2,itemsPerPage为5,currentPage为1,我得到了这个:

total: 2
per_page: 5
current_page: 1
last_page: 0.4
from: 6
to: 10

我觉得有些不对劲。

4 个答案:

答案 0 :(得分:4)

要舍入最大值,您可以在Math.ceil上使用last_page

每页的项目可以是静态的,手动定义。然后,from可以是((currentPage - 1) * itemsPerPage) + 1

  • 如果当前页面为1,((1 -1)* 5)+ 1 = 1。
  • 第二页:((2 -1)* 5)+ 1 = 6,依此类推。

然后它可以是currentPage * itemsPerPage。例如,如果当前页面是1,那么1 * 5 = 5,如果它是第二页,它将是:2 * 5 = 10.看下面的例子:

var pagination = {
 total: result.length,
 per_page: itemsPerPage,    
 current_page: currentPage, 
 last_page: Math.ceil(result.length / itemsPerPage),
 from: ((currentPage -1) * itemsPerPage) + 1,
 to: currentPage  * itemsPerPage
};
Total = 15;
per_page = 5;
currentPage = 1;
last_page = truncate (15 / 5) = 3;
from: ((1-1) * 5) + 1 = 1 //first page
      ((2-1) * 5) + 1 = 6 //second page
to: 1 * 5 = 5 //first page
    2 * 5 = 10 // second page
From 1 to 5 // first page
From 6 to 10 // second page
From 11 to 15 // last page

答案 1 :(得分:2)

last_page应该使用Math.ceil(),因此没有浮点数。那么0.4将是1,这是正确的。 from应为itemsPerPage * (pageNumber - 1) - 这假设值为0,pageNumber从1开始。 然后应该是itemsPerPage * pageNumbertotal.length的Math.min(),以便使用from-count和itemsPerPage,或者如果此值大于项目总计数,则总项目数用来。

答案 2 :(得分:0)

以上函数形式的答案:

function pagination(length, currentPage, itemsPerPage) {
    return {
        total: length,
        per_page: itemsPerPage,
        current_page: currentPage,
        last_page: Math.ceil(length / itemsPerPage),
        from: ((currentPage - 1) * itemsPerPage) + 1,
        to: currentPage * itemsPerPage
    };
};

答案 3 :(得分:0)

对于将来到达这里的任何人,打勾的答案具有正确的“来自”公式。在最后一页的值少于“per_page”值的情况下,“to”公式不准确。您可以像这样获得“to”值:

// Here we want to know the total number of pages possible, we are rounding up to the nearest integer because if the total number of items is
// not divisible by the numbers of items per page, then there has to be an extra page to list those last items

const numberOfPages = Math.ceil(total/per_page);

// here, we check if the number of pages is same value as our current page, this lets us know if we are on the last page or not
// if we are on the last page, we calculate the "to" value for that would be the "previous page" (this works even if we are on the very first page though)
// and we add the length of the current list to the value

const to = numberOfPages === current_page ? (per_page * (current_page - 1)) + {{length of the current list in this page}} : per_page * current_page;

// For anyone who does not understand tenary operators, the line const to =... is the same as writing

let to;
if ( numberOfPages === current_page){
  to = (per_page * (current_page - 1)) + {{length of the current list in this page}}
} else {
 to = per_page * current_page
}