element/packages/pagination/src/pager.vue

164 lines
4.2 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
2016-07-29 09:46:16 +00:00
<ul @click="onPagerClick" class="el-pager">
2016-07-27 06:15:02 +00:00
<li
2018-03-07 03:47:19 +00:00
:class="{ active: currentPage === 1, disabled }"
2016-07-27 06:15:02 +00:00
v-if="pageCount > 0"
class="number">1</li>
<li
2016-08-31 03:04:16 +00:00
class="el-icon more btn-quickprev"
2018-03-07 03:47:19 +00:00
:class="[quickprevIconClass, { disabled }]"
2016-07-27 06:15:02 +00:00
v-if="showPrevMore"
2018-03-07 03:47:19 +00:00
@mouseenter="onMouseenter('left')"
2016-09-27 09:33:18 +00:00
@mouseleave="quickprevIconClass = 'el-icon-more'">
2016-07-27 06:15:02 +00:00
</li>
<li
v-for="pager in pagers"
2018-02-01 07:16:21 +00:00
:key="pager"
2018-03-07 03:47:19 +00:00
:class="{ active: currentPage === pager, disabled }"
2016-07-27 06:15:02 +00:00
class="number">{{ pager }}</li>
<li
2016-08-31 03:04:16 +00:00
class="el-icon more btn-quicknext"
2018-03-07 03:47:19 +00:00
:class="[quicknextIconClass, { disabled }]"
2016-07-27 06:15:02 +00:00
v-if="showNextMore"
2018-03-07 03:47:19 +00:00
@mouseenter="onMouseenter('right')"
2016-09-27 09:33:18 +00:00
@mouseleave="quicknextIconClass = 'el-icon-more'">
2016-07-27 06:15:02 +00:00
</li>
<li
2018-03-07 03:47:19 +00:00
:class="{ active: currentPage === pageCount, disabled }"
2016-07-27 06:15:02 +00:00
class="number"
v-if="pageCount > 1">{{ pageCount }}</li>
</ul>
</template>
2016-07-29 09:46:16 +00:00
<script type="text/babel">
2016-07-27 06:15:02 +00:00
export default {
name: 'ElPager',
props: {
currentPage: Number,
2018-03-07 03:47:19 +00:00
pageCount: Number,
pagerCount: Number,
2018-03-07 03:47:19 +00:00
disabled: Boolean
2016-07-27 06:15:02 +00:00
},
2016-09-27 09:33:18 +00:00
watch: {
showPrevMore(val) {
if (!val) this.quickprevIconClass = 'el-icon-more';
},
showNextMore(val) {
if (!val) this.quicknextIconClass = 'el-icon-more';
}
},
2016-07-27 06:15:02 +00:00
methods: {
onPagerClick(event) {
const target = event.target;
2018-03-07 03:47:19 +00:00
if (target.tagName === 'UL' || this.disabled) {
2016-07-27 06:15:02 +00:00
return;
}
let newPage = Number(event.target.textContent);
const pageCount = this.pageCount;
const currentPage = this.currentPage;
const pagerCountOffset = this.pagerCount - 2;
2016-07-27 06:15:02 +00:00
2016-08-31 03:04:16 +00:00
if (target.className.indexOf('more') !== -1) {
2016-07-27 06:15:02 +00:00
if (target.className.indexOf('quickprev') !== -1) {
newPage = currentPage - pagerCountOffset;
2016-07-27 06:15:02 +00:00
} else if (target.className.indexOf('quicknext') !== -1) {
newPage = currentPage + pagerCountOffset;
2016-07-27 06:15:02 +00:00
}
}
2016-10-21 08:14:14 +00:00
/* istanbul ignore if */
2016-07-27 06:15:02 +00:00
if (!isNaN(newPage)) {
if (newPage < 1) {
newPage = 1;
}
if (newPage > pageCount) {
newPage = pageCount;
}
}
if (newPage !== currentPage) {
this.$emit('change', newPage);
2016-07-27 06:15:02 +00:00
}
2018-03-07 03:47:19 +00:00
},
onMouseenter(direction) {
if (this.disabled) return;
if (direction === 'left') {
this.quickprevIconClass = 'el-icon-d-arrow-left';
} else {
this.quicknextIconClass = 'el-icon-d-arrow-right';
}
2016-07-27 06:15:02 +00:00
}
},
computed: {
pagers() {
const pagerCount = this.pagerCount;
const halfPagerCount = (pagerCount - 1) / 2;
2016-07-27 06:15:02 +00:00
const currentPage = Number(this.currentPage);
const pageCount = Number(this.pageCount);
let showPrevMore = false;
let showNextMore = false;
if (pageCount > pagerCount) {
if (currentPage > pagerCount - halfPagerCount) {
2016-07-27 06:15:02 +00:00
showPrevMore = true;
}
if (currentPage < pageCount - halfPagerCount) {
2016-07-27 06:15:02 +00:00
showNextMore = true;
}
}
const array = [];
if (showPrevMore && !showNextMore) {
const startPage = pageCount - (pagerCount - 2);
for (let i = startPage; i < pageCount; i++) {
array.push(i);
}
} else if (!showPrevMore && showNextMore) {
for (let i = 2; i < pagerCount; i++) {
array.push(i);
}
} else if (showPrevMore && showNextMore) {
const offset = Math.floor(pagerCount / 2) - 1;
for (let i = currentPage - offset ; i <= currentPage + offset; i++) {
array.push(i);
}
} else {
for (let i = 2; i < pageCount; i++) {
array.push(i);
}
}
this.showPrevMore = showPrevMore;
this.showNextMore = showNextMore;
return array;
}
},
data() {
return {
current: null,
showPrevMore: false,
showNextMore: false,
2016-08-31 03:04:16 +00:00
quicknextIconClass: 'el-icon-more',
quickprevIconClass: 'el-icon-more'
2016-07-27 06:15:02 +00:00
};
}
};
</script>