Last active
June 21, 2022 02:35
-
-
Save fronterior/92ad81df1b2cf04dc8be0341e647c789 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const createPaginationIndex = (currentPage: number, total: number, limit = 10, listLength = 10) => { | |
const totalPages = Math.ceil(total / limit); | |
const startSetIndex = Math.floor((currentPage - 1) / listLength); | |
const startList = listLength * startSetIndex + 1; | |
const length = startList + listLength - 1 > totalPages ? | |
listLength - (startList + listLength - 1 - totalPages) : | |
listLength; | |
return { | |
isFirst: currentPage <= listLength, | |
isLast: startList + listLength - 1 >= totalPages, | |
items: Array.from({length}, (_, i) => { | |
return startList + i; | |
}), | |
first: 1, | |
last: totalPages, | |
prevFirst: startList - listLength, | |
nextFirst: startList + listLength, | |
} | |
}; | |
const {isFirst, isLast, items: pagenation, prevFirst, nextFirst, first, last} = createPaginationIndex(page, total, limit); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment