Last active
June 9, 2020 15:58
-
-
Save genakim/9c842465b9512aaf8c2a33397374faa3 to your computer and use it in GitHub Desktop.
Логика пагинации (laravel + vuejs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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