regex for number check

This commit is contained in:
Regis 2016-11-08 13:04:30 -07:00
parent 4dd82e1d49
commit 39a8f1aaf4
2 changed files with 12 additions and 7 deletions

View file

@ -9,9 +9,14 @@
'pagenum', 'pagenum',
], ],
methods: { methods: {
pagenumberstatus(n) { pagestatus(n) {
if (n - 1 === +this.pagenum) return 'active'; if (n - 1 === +this.pagenum) return true;
return ''; return false;
},
prevstatus(index) {
if (index > 0) return false;
if (+this.pagenum < 2) return true;
return false;
}, },
createSection(n) { return Array.from(Array(n)).map((e, i) => i); }, createSection(n) { return Array.from(Array(n)).map((e, i) => i); },
}, },
@ -22,7 +27,7 @@
const pages = this.createSection(+this.last + 1); const pages = this.createSection(+this.last + 1);
pages.shift(); pages.shift();
if (+this.pagenum !== 1) items.push({ text: 'Prev' }); items.push({ text: 'Prev', class: this.prevstatus() });
pages.forEach(i => items.push({ text: i })); pages.forEach(i => items.push({ text: i }));
@ -34,8 +39,8 @@
}, },
template: ` template: `
<div class="gl-pagination"> <div class="gl-pagination">
<ul class="pagination clearfix" v-for='(item, index) in getItems'> <ul class="pagination clearfix" v-for='(item, i) in getItems'>
<li :class='pagenumberstatus(index + 1)'> <li :class="{active: pagestatus(i + 1), disabled: prevstatus(i)}">
<span @click='changepage($event, last)'>{{item.text}}</span> <span @click='changepage($event, last)'>{{item.text}}</span>
</li> </li>
</ul> </ul>

View file

@ -37,7 +37,7 @@
}, },
changepage(event, last) { changepage(event, last) {
const text = event.target.innerText; const text = event.target.innerText;
if (typeof text === 'number') this.pagenum = +text; if (/^-?[\d.]+(?:e-?\d+)?$/.test(text)) this.pagenum = +text;
if (text === 'Last »') this.pagenum = last; if (text === 'Last »') this.pagenum = last;
if (text === 'Next') this.pagenum = +this.pagenum + 1; if (text === 'Next') this.pagenum = +this.pagenum + 1;
if (text === 'Prev') this.pagenum = +this.pagenum - 1; if (text === 'Prev') this.pagenum = +this.pagenum - 1;