Created
November 9, 2020 10:11
-
-
Save hamrammi/3da2f613d403d3726179ef11b9f9c130 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Paginator Vue component</title> | |
<script src="https://unpkg.com/vue@next"></script> | |
</head> | |
<body> | |
<div id="vueApp"> | |
<div style="border: 1px solid lightcoral; padding: 10px;"> | |
<paginator-component | |
v-bind="paginator" | |
></paginator-component> | |
</div> | |
</div> | |
<script> | |
const vueApp = Vue.createApp({ | |
data () { | |
return { | |
paginator: { | |
total: 20, | |
around: 3, | |
current: 6 | |
} | |
} | |
} | |
}) | |
vueApp.component('paginator-component', { | |
props: ['total', 'around', 'current'], | |
setup (props) { | |
let left = props.current - props.around | |
if (left < 1) { | |
left = 1 | |
} | |
let right = props.current + props.around | |
if (right > props.total) { | |
right = props.total | |
} | |
let s = [] | |
let t = left | |
while (t <= right) { | |
s.push(t) | |
t++ | |
} | |
if ((s[0] - 1) > 1) { | |
s.unshift('...') | |
} | |
if (props.total - s[s.length - 1] > 1) { | |
s.push('...') | |
} | |
let total = [1].concat(s).concat(props.total) | |
const pages = total.reduce((xs, x) => { | |
if (x === '...') { | |
xs.push(x) | |
} else { | |
if (xs.indexOf(x) === -1) { | |
xs.push(x) | |
} | |
} | |
return xs | |
}, []) | |
return { | |
pages | |
} | |
}, | |
template: ` | |
<div> | |
<span v-for="p in pages" style="margin-right: 10px;"> | |
<span v-if="p === '...'">{{ p }}</span> | |
<a v-else href="#">{{ p }}</a> | |
</span> | |
</div> | |
` | |
}) | |
vueApp.mount('#vueApp') | |
</script> | |
</body> | |
</html> |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Paginator</title> | |
</head> | |
<body> | |
<div id="paginator"></div> | |
<script> | |
const OPTS = { | |
total: 20, | |
around: 3, | |
current: 6 | |
} | |
let left = OPTS.current - OPTS.around | |
if (left < 1) { | |
left = 1 | |
} | |
let right = OPTS.current + OPTS.around | |
if (right > OPTS.total) { | |
right = OPTS.total | |
} | |
let s = [] | |
let t = left | |
while (t <= right) { | |
s.push(t) | |
t++ | |
} | |
if ((s[0] - 1) > 1) { | |
s.unshift('...') | |
} | |
if (OPTS.total - s[s.length - 1] > 1) { | |
s.push('...') | |
} | |
let total = [1].concat(s).concat(OPTS.total) | |
const unique = total.reduce((xs, x) => { | |
if (x === '...') { | |
xs.push(x) | |
} else { | |
if (xs.indexOf(x) === -1) { | |
xs.push(x) | |
} | |
} | |
return xs | |
}, []) | |
document.querySelector('#paginator').innerHTML = unique.join(' ') | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment