Last active
April 1, 2022 15:40
-
-
Save Andaeiii/8d9016134042e9ccf2b942dd89ebf8a8 to your computer and use it in GitHub Desktop.
My pagination construct usinga arrays to build up links... with " . . . . " used to denote existing links before and after the active link
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
let arr = paginate(index, len); | |
const paginate = (index, len) => { | |
let curIndex = (index === null) ? 1 : index; | |
let prvArr = (curIndex == 1) ? [] : (curIndex == 2) ? [1] : (curIndex == 3) ? [1, 2] : ['..', curIndex - 1]; | |
let nxtArr = (curIndex == len) ? [] : (curIndex == len - 1) ? [len] : (curIndex == len - 2) ? [len - 2, len - 1] : [curIndex + 1, '...']; | |
return [1, ...prvArr, curIndex, ...nxtArr, len] | |
.filter((v, i, s) => s.indexOf(v) === i) | |
.map(v => v === '..' ? '...' : v) | |
.map(v => v === '...' ? '.....' : v); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
provide the given length and the current index, and this code gives you the page index and the