我想在此基础上的解决方案在这里找出分页: https://stackoverflow.com/a/44844/3214545算法页面链接
我从来没有真正做过,所以我与它有困难复杂的逻辑。这是我的代码:
var allPages = 50;
var current = 16;
var pageLinks = [];
function updatePageLinks(current, allPages) {
if (current > 1) {
pageLinks.push('<', 1);
}
if (current > 2) {
pageLinks.push('...');
if (current == allPages && allPages > 3) {
pageLinks.push((current - 2), (current - 1));
}
}
if (current != 1 && current != allPages) {
pageLinks.push(current);
}
if (current < allPages - 1) {
pageLinks.push((current + 1), (current + 2), '...');
if (current == 1 && allPages > 3) {
pageLinks.push((current + 2), '...');
}
}
pageLinks.push(allPages);
if (current < allPages) {
pageLinks.push('>');
}
$('#pages').append("<a href='#'> " + pageLinks + '</a>');
}
我会从应用程序的另一部分被加载页面的数字,但目前我刚才设置的数字,所以我可以测试不同的边缘情况。 当前页面上方是16和总页数为50所以我所要的输出是这样的:
< 1 2 ... 15 16 17 ... 49 50>
,其中前2最后2页总是显示与当前页面+和 - 问题1
实例:
- 如果当前页面被设置为1,则输出为2,3,...,50>
如果当前页面为2,则输出为<,1,2,3,5,>不打印4
如果当前页面设置为2且总数为50,则输出为<, 1,2,3,10>
- 如果当前页面设置为5,输出为<,1,...,5,6,50>
谁能帮助我?
你的算法似乎并不匹配连接的一种'printPageLinksFinalReally'。 – Danny