Javascript生成分页编号

Cha*_*ynn 2 javascript pagination

我正在尝试生成分页按钮.

我希望按钮看起来像这样:

  • 1,2,3 ... 7
  • 2,3,4 ... 7.
  • 3,4,5 ... 7.
  • 4,5,6 ...... 7.
  • 5,6,7.
  • 5,6,7.
  • 5,6,7.

粗体数字是选定页面,7是最高页面.

我的当前函数会生成此函数,直到您在最后3页上.

[1, 2, 3, "...", 7]
[2, 3, 4, "...", 7]
[3, 4, 5, "...", 7]
[4, 5, 6, "...", 7]
[5, 6, 7]
[6, 7]
[7]
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像:

[1, 2, 3, "...", 7]
[2, 3, 4, "...", 7]
[3, 4, 5, "...", 7]
[4, 5, 6, "...", 7]
[5, 6, 7]
[5, 6, 7]
[5, 6, 7]
Run Code Online (Sandbox Code Playgroud)

Javascript:

var test = function (count, current) {
    this.pageCountArray = [];

    var shownPageNumbers = 3;


    for (var i = current; i <= count; i++) {
        if (this.pageCountArray.length < shownPageNumbers) {
            this.pageCountArray.push(i);
        }
        else {
            this.pageCountArray.push('...');
            this.pageCountArray.push(count);
            break;
        }
    }

    console.log(this.pageCountArray);
}
Run Code Online (Sandbox Code Playgroud)

用法:

test(7, 1);
test(7, 2);
test(7, 3);
test(7, 4);
test(7, 5);
test(7, 6);
test(7, 7);
Run Code Online (Sandbox Code Playgroud)

console.clear();
var test = function (count, current) {
    this.pageCountArray = [];

    var shownPageNumbers = 3;


    for (var i = current; i <= count; i++) {
        if (this.pageCountArray.length < shownPageNumbers) {
            this.pageCountArray.push(i);
        }
        else {
            this.pageCountArray.push('...');
            this.pageCountArray.push(count);
            break;
        }
    }

    console.log(this.pageCountArray);
  
  document.write(JSON.stringify(this.pageCountArray))
}

test(7, 1);
test(7, 2);
test(7, 3);
test(7, 4);
test(7, 5);
test(7, 6);
test(7, 7);
Run Code Online (Sandbox Code Playgroud)

how*_*rek 6

以下是我将如何实现它:

function pageNumbers(count, current) {
    var shownPages = 3;
    var result = [];
    if (current > count - shownPages) {
        result.push(count - 2, count - 1, count);
    } else {
        result.push(current, current + 1, current + 2, '...', count);
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

示例:http://jsfiddle.net/howderek/x474jtsd/