Skip to content

Instantly share code, notes, and snippets.

@genakim
Last active June 9, 2020 15:58
Show Gist options
  • Save genakim/9c842465b9512aaf8c2a33397374faa3 to your computer and use it in GitHub Desktop.
Save genakim/9c842465b9512aaf8c2a33397374faa3 to your computer and use it in GitHub Desktop.
Логика пагинации (laravel + vuejs)
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
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment