Skip to content

Instantly share code, notes, and snippets.

@genakim
Last active June 9, 2020 15:58
Show Gist options
  • Select an option

  • Save genakim/9c842465b9512aaf8c2a33397374faa3 to your computer and use it in GitHub Desktop.

Select an option

Save genakim/9c842465b9512aaf8c2a33397374faa3 to your computer and use it in GitHub Desktop.

Revisions

  1. genakim revised this gist Jun 9, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion paginate.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ this.pages = [];
    if (startPage <= 1) {
    startPage = 1;
    } else {
    if(totalPages - startPage < pages){ //вмещается ли нужный диапазон
    if((totalPages - startPage) < pages){ //вмещается ли нужный диапазон
    startPage = totalPages - pages;
    if (startPage <= 1){
    startPage = 1;
  2. genakim created this gist Jun 9, 2020.
    32 changes: 32 additions & 0 deletions paginate.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    this.pages = [];
    const pages = 5;
    const currentPage = this.data.current_page;
    const totalPages = this.data.last_page;
    const offSide = Math.floor(pages / 2);

    // первая страница
    let startPage = currentPage - offSide;
    if (startPage <= 1) {
    startPage = 1;
    } else {
    if(totalPages - startPage < pages){ //вмещается ли нужный диапазон
    startPage = totalPages - pages;
    if (startPage <= 1){
    startPage = 1;
    }
    }
    }

    // последняя страница
    let lastPage = startPage + pages - 1;
    if (lastPage > totalPages) {
    lastPage = totalPages;
    }

    for (let i = startPage; i <= lastPage; i++) {
    this.pages.push({
    url: this.data.path + '?page=' + i,
    number: i,
    active: i === currentPage
    });
    }