Skip to content

Instantly share code, notes, and snippets.

@fronterior
Last active June 21, 2022 02:35
Show Gist options
  • Save fronterior/92ad81df1b2cf04dc8be0341e647c789 to your computer and use it in GitHub Desktop.
Save fronterior/92ad81df1b2cf04dc8be0341e647c789 to your computer and use it in GitHub Desktop.
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