Last active
September 1, 2022 15:26
-
-
Save dptole/139ee24b91b57624fcd64a317e7bfe55 to your computer and use it in GitHub Desktop.
Nodejs simple pagination
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 help = { | |
genPagination: (pageLength, fullPageSelectedIndex) => { | |
const fullPage = Array.from(Array(pageLength)).map((v, i) => i) | |
if (fullPage.length < 11) return fullPage | |
let minLeftCut = 2 | |
let maxLeftCut = 6 | |
let minRightCut = 2 | |
let maxRightCut = 6 | |
let leftPrefix = 2 | |
let minLeftPrefix = 6 | |
let rightPrefix = 2 | |
let minRightPrefix = 6 | |
let leftSide = fullPage.slice(0, fullPageSelectedIndex) | |
let rightSide = fullPage.slice(fullPageSelectedIndex + 1) | |
let selectedValue = fullPage[fullPageSelectedIndex] | |
if (leftSide.length >= minLeftPrefix) { | |
let leftCut = maxLeftCut - rightSide.length < minLeftCut ? minLeftCut : maxLeftCut - rightSide.length | |
leftSide = fullPage.slice(0, leftPrefix).concat(['...']).concat(leftSide.slice(-leftCut)) | |
} | |
if (rightSide.length >= minRightPrefix) { | |
let rightCut = maxRightCut - leftSide.length < minRightCut ? minRightCut : maxRightCut - leftSide.length | |
rightSide = rightSide.slice(0, rightCut).concat(['...']).concat(fullPage.slice(-rightPrefix)) | |
} | |
return leftSide.concat(selectedValue).concat(rightSide) | |
}, | |
} | |
module.exports = help |
Author
dptole
commented
Sep 1, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment