iview/src/components/page/page.vue

346 lines
12 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
2017-03-21 17:15:03 +08:00
<ul :class="simpleWrapClasses" :style="styles" v-if="simple">
2016-09-09 14:29:19 +08:00
<li
2017-01-12 10:21:47 +08:00
:title="t('i.page.prev')"
2016-09-09 14:29:19 +08:00
:class="prevClasses"
2020-09-03 16:11:11 +08:00
@click.stop="prev">
2018-06-25 14:42:59 +08:00
<a><i class="ivu-icon ivu-icon-ios-arrow-back"></i></a>
2016-09-09 14:29:19 +08:00
</li>
2017-03-07 10:32:46 +08:00
<div :class="simplePagerClasses" :title="currentPage + '/' + allPages">
2016-09-09 14:29:19 +08:00
<input
type="text"
2017-03-07 10:32:46 +08:00
:value="currentPage"
2017-11-04 16:37:29 +01:00
autocomplete="off"
spellcheck="false"
2020-09-03 16:15:54 +08:00
:disabled="disabled"
2016-09-09 14:29:19 +08:00
@keydown="keyDown"
@keyup="keyUp"
@change="keyUp">
<span>/</span>
{{ allPages }}
</div>
<li
2017-01-12 10:21:47 +08:00
:title="t('i.page.next')"
2016-09-09 14:29:19 +08:00
:class="nextClasses"
2020-09-03 16:11:11 +08:00
@click.stop="next">
2018-06-25 14:42:59 +08:00
<a><i class="ivu-icon ivu-icon-ios-arrow-forward"></i></a>
2016-09-09 14:29:19 +08:00
</li>
</ul>
2017-03-21 17:15:03 +08:00
<ul :class="wrapClasses" :style="styles" v-else>
2016-10-28 17:24:52 +08:00
<span :class="[prefixCls + '-total']" v-if="showTotal">
2017-01-12 10:21:47 +08:00
<slot>{{ t('i.page.total') }} {{ total }} <template v-if="total <= 1">{{ t('i.page.item') }}</template><template v-else>{{ t('i.page.items') }}</template></slot>
2016-09-09 14:29:19 +08:00
</span>
<li
2017-01-12 10:21:47 +08:00
:title="t('i.page.prev')"
2016-09-09 14:29:19 +08:00
:class="prevClasses"
2020-09-03 16:11:11 +08:00
@click.stop="prev">
2018-06-25 14:42:59 +08:00
<a><template v-if="prevText !== ''">{{ prevText }}</template><i v-else class="ivu-icon ivu-icon-ios-arrow-back"></i></a>
2016-09-09 14:29:19 +08:00
</li>
2020-09-03 16:11:11 +08:00
<li title="1" :class="firstPageClasses" @click.stop="changePage(1)"><a>1</a></li>
<li :title="t('i.page.prev5')" v-if="currentPage > 5" :class="[prefixCls + '-item-jump-prev']" @click.stop="fastPrev"><a><i class="ivu-icon ivu-icon-ios-arrow-back"></i></a></li>
<li :title="currentPage - 3" v-if="currentPage === 5" :class="[prefixCls + '-item']" @click.stop="changePage(currentPage - 3)"><a>{{ currentPage - 3 }}</a></li>
<li :title="currentPage - 2" v-if="currentPage - 2 > 1" :class="[prefixCls + '-item']" @click.stop="changePage(currentPage - 2)"><a>{{ currentPage - 2 }}</a></li>
<li :title="currentPage - 1" v-if="currentPage - 1 > 1" :class="[prefixCls + '-item']" @click.stop="changePage(currentPage - 1)"><a>{{ currentPage - 1 }}</a></li>
2017-03-07 10:32:46 +08:00
<li :title="currentPage" v-if="currentPage != 1 && currentPage != allPages" :class="[prefixCls + '-item',prefixCls + '-item-active']"><a>{{ currentPage }}</a></li>
2020-09-03 16:11:11 +08:00
<li :title="currentPage + 1" v-if="currentPage + 1 < allPages" :class="[prefixCls + '-item']" @click.stop="changePage(currentPage + 1)"><a>{{ currentPage + 1 }}</a></li>
<li :title="currentPage + 2" v-if="currentPage + 2 < allPages" :class="[prefixCls + '-item']" @click.stop="changePage(currentPage + 2)"><a>{{ currentPage + 2 }}</a></li>
<li :title="currentPage + 3" v-if="allPages - currentPage === 4" :class="[prefixCls + '-item']" @click.stop="changePage(currentPage + 3)"><a>{{ currentPage + 3 }}</a></li>
<li :title="t('i.page.next5')" v-if="allPages - currentPage >= 5" :class="[prefixCls + '-item-jump-next']" @click.stop="fastNext"><a><i class="ivu-icon ivu-icon-ios-arrow-forward"></i></a></li>
<li :title="allPages" v-if="allPages > 1" :class="lastPageClasses" @click.stop="changePage(allPages)"><a>{{ allPages }}</a></li>
2016-09-09 14:29:19 +08:00
<li
2017-01-12 10:21:47 +08:00
:title="t('i.page.next')"
2016-09-09 14:29:19 +08:00
:class="nextClasses"
2020-09-03 16:11:11 +08:00
@click.stop="next">
2018-06-25 14:42:59 +08:00
<a><template v-if="nextText !== ''">{{ nextText }}</template><i v-else class="ivu-icon ivu-icon-ios-arrow-forward"></i></a>
2016-09-09 14:29:19 +08:00
</li>
<Options
:show-sizer="showSizer"
2017-03-07 10:32:46 +08:00
:page-size="currentPageSize"
2016-09-09 14:29:19 +08:00
:page-size-opts="pageSizeOpts"
2017-04-27 17:35:47 +08:00
:placement="placement"
2018-03-12 16:42:48 +08:00
:transfer="transfer"
2016-09-09 14:29:19 +08:00
:show-elevator="showElevator"
2017-03-07 10:32:46 +08:00
:_current.once="currentPage"
:current="currentPage"
2020-09-03 16:15:54 +08:00
:disabled="disabled"
2016-09-09 14:29:19 +08:00
:all-pages="allPages"
:is-small="isSmall"
2016-09-09 14:29:19 +08:00
@on-size="onSize"
@on-page="onPage">
</Options>
</ul>
</template>
<script>
import { oneOf } from '../../utils/assist';
import Options from './options.vue';
2017-01-12 10:21:47 +08:00
import Locale from '../../mixins/locale';
2016-09-09 14:29:19 +08:00
const prefixCls = 'ivu-page';
export default {
2017-03-07 10:32:46 +08:00
name: 'Page',
2017-01-12 10:21:47 +08:00
mixins: [ Locale ],
2016-09-09 14:29:19 +08:00
components: { Options },
props: {
current: {
type: Number,
default: 1
},
total: {
type: Number,
default: 0
},
pageSize: {
type: Number,
default: 10
},
pageSizeOpts: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [10, 20, 30, 40];
2016-09-09 14:29:19 +08:00
}
},
2017-04-27 17:35:47 +08:00
placement: {
validator (value) {
return oneOf(value, ['top', 'bottom']);
},
default: 'bottom'
},
2018-03-12 16:42:48 +08:00
transfer: {
type: Boolean,
2018-06-28 15:09:02 +08:00
default () {
2018-08-07 16:35:27 +08:00
return !this.$IVIEW || this.$IVIEW.transfer === '' ? false : this.$IVIEW.transfer;
2018-06-28 15:09:02 +08:00
}
2018-03-12 16:42:48 +08:00
},
2016-09-09 14:29:19 +08:00
size: {
validator (value) {
return oneOf(value, ['small']);
}
},
simple: {
type: Boolean,
default: false
},
showTotal: {
type: Boolean,
default: false
},
showElevator: {
type: Boolean,
default: false
},
showSizer: {
type: Boolean,
default: false
2016-12-19 09:59:08 +08:00
},
2017-03-07 10:32:46 +08:00
className: {
2016-12-19 09:59:08 +08:00
type: String
},
2017-03-21 17:15:03 +08:00
styles: {
2016-12-19 09:59:08 +08:00
type: Object
2018-06-22 11:02:07 +08:00
},
prevText: {
type: String,
default: ''
},
nextText: {
type: String,
default: ''
},
2020-09-03 16:11:11 +08:00
cacheKey: {
type: String,
default: ''
},
cachePageSize: {
type: Boolean,
default: false
2020-09-03 16:15:54 +08:00
},
disabled: {
type: Boolean,
default: false
2016-09-09 14:29:19 +08:00
}
},
data () {
return {
2017-03-07 10:32:46 +08:00
prefixCls: prefixCls,
currentPage: this.current,
currentPageSize: this.pageSize
2016-12-25 22:49:42 +08:00
};
2016-09-09 14:29:19 +08:00
},
2017-03-07 10:32:46 +08:00
watch: {
2017-08-08 11:23:50 -05:00
total (val) {
let maxPage = Math.ceil(val / this.currentPageSize);
if (maxPage < this.currentPage ) {
this.currentPage = (maxPage === 0 ? 1 : maxPage);
2017-08-08 11:23:50 -05:00
}
},
2017-03-07 10:32:46 +08:00
current (val) {
this.currentPage = val;
},
pageSize (val) {
this.currentPageSize = val;
}
},
2016-09-09 14:29:19 +08:00
computed: {
isSmall () {
return !!this.size;
},
2016-09-09 14:29:19 +08:00
allPages () {
2017-03-07 10:32:46 +08:00
const allPage = Math.ceil(this.total / this.currentPageSize);
2016-10-10 09:08:20 +08:00
return (allPage === 0) ? 1 : allPage;
2016-09-09 14:29:19 +08:00
},
simpleWrapClasses () {
return [
`${prefixCls}`,
2016-12-19 09:59:08 +08:00
`${prefixCls}-simple`,
{
2017-03-07 10:32:46 +08:00
[`${this.className}`]: !!this.className
2016-12-19 09:59:08 +08:00
}
2016-12-25 22:49:42 +08:00
];
2016-09-09 14:29:19 +08:00
},
2016-09-23 10:57:56 +08:00
simplePagerClasses () {
return `${prefixCls}-simple-pager`;
},
2016-09-09 14:29:19 +08:00
wrapClasses () {
return [
`${prefixCls}`,
{
2017-03-07 10:32:46 +08:00
[`${this.className}`]: !!this.className,
2020-09-03 16:15:54 +08:00
[`${prefixCls}-with-disabled`]: this.disabled,
2016-09-09 14:29:19 +08:00
'mini': !!this.size
}
2016-12-25 22:49:42 +08:00
];
2016-09-09 14:29:19 +08:00
},
prevClasses () {
return [
`${prefixCls}-prev`,
{
2020-09-03 16:15:54 +08:00
[`${prefixCls}-disabled`]: this.currentPage === 1 || this.disabled,
2018-06-22 11:02:07 +08:00
[`${prefixCls}-custom-text`]: this.prevText !== ''
2016-09-09 14:29:19 +08:00
}
2016-12-25 22:49:42 +08:00
];
2016-09-09 14:29:19 +08:00
},
nextClasses () {
return [
`${prefixCls}-next`,
{
2020-09-03 16:15:54 +08:00
[`${prefixCls}-disabled`]: this.currentPage === this.allPages || this.disabled,
2018-06-22 11:02:07 +08:00
[`${prefixCls}-custom-text`]: this.nextText !== ''
2016-09-09 14:29:19 +08:00
}
2016-12-25 22:49:42 +08:00
];
2016-10-28 17:24:52 +08:00
},
firstPageClasses () {
return [
`${prefixCls}-item`,
{
2017-03-07 10:32:46 +08:00
[`${prefixCls}-item-active`]: this.currentPage === 1
2016-10-28 17:24:52 +08:00
}
2016-12-25 22:49:42 +08:00
];
2016-10-28 17:24:52 +08:00
},
lastPageClasses () {
return [
`${prefixCls}-item`,
{
2017-03-07 10:32:46 +08:00
[`${prefixCls}-item-active`]: this.currentPage === this.allPages
2016-10-28 17:24:52 +08:00
}
2016-12-25 22:49:42 +08:00
];
2020-09-03 16:11:11 +08:00
},
pageSizeKey() {
if (this.cachePageSize && this.cacheKey !== null) {
return `pageSize_${this.cacheKey}`;
} else {
return null;
}
2016-09-09 14:29:19 +08:00
}
},
methods: {
changePage (page) {
2020-09-03 16:15:54 +08:00
if (this.disabled) return;
2017-03-07 10:32:46 +08:00
if (this.currentPage != page) {
this.currentPage = page;
2017-08-10 11:28:44 -05:00
this.$emit('update:current', page);
2016-09-09 14:29:19 +08:00
this.$emit('on-change', page);
}
},
prev () {
2020-09-03 16:15:54 +08:00
if (this.disabled) return;
2017-03-07 10:32:46 +08:00
const current = this.currentPage;
2016-09-09 14:29:19 +08:00
if (current <= 1) {
return false;
}
this.changePage(current - 1);
},
next () {
2020-09-03 16:15:54 +08:00
if (this.disabled) return;
2017-03-07 10:32:46 +08:00
const current = this.currentPage;
2016-09-09 14:29:19 +08:00
if (current >= this.allPages) {
return false;
}
this.changePage(current + 1);
},
fastPrev () {
2020-09-03 16:15:54 +08:00
if (this.disabled) return;
2017-03-07 10:32:46 +08:00
const page = this.currentPage - 5;
2016-09-09 14:29:19 +08:00
if (page > 0) {
this.changePage(page);
} else {
this.changePage(1);
}
},
fastNext () {
2020-09-03 16:15:54 +08:00
if (this.disabled) return;
2017-03-07 10:32:46 +08:00
const page = this.currentPage + 5;
2016-09-09 14:29:19 +08:00
if (page > this.allPages) {
this.changePage(this.allPages);
} else {
this.changePage(page);
}
},
onSize (pageSize) {
2020-09-03 16:15:54 +08:00
if (this.disabled) return;
2017-03-07 10:32:46 +08:00
this.currentPageSize = pageSize;
this.$emit('on-page-size-change', pageSize);
2017-04-27 16:27:38 +08:00
this.changePage(1);
2020-09-03 16:11:11 +08:00
if (this.cachePageSize && this.cacheKey !== null) {
window.localStorage.setItem(this.pageSizeKey, pageSize);
}
2016-09-09 14:29:19 +08:00
},
onPage (page) {
2020-09-03 16:15:54 +08:00
if (this.disabled) return;
2016-09-09 14:29:19 +08:00
this.changePage(page);
},
keyDown (e) {
const key = e.keyCode;
2017-11-07 17:07:03 +08:00
const condition = (key >= 48 && key <= 57) || (key >= 96 && key <= 105) || key === 8 || key === 37 || key === 39;
2016-09-09 14:29:19 +08:00
if (!condition) {
e.preventDefault();
}
},
keyUp (e) {
const key = e.keyCode;
const val = parseInt(e.target.value);
if (key === 38) {
2016-12-25 22:49:42 +08:00
this.prev();
2016-09-09 14:29:19 +08:00
} else if (key === 40) {
2016-12-25 22:49:42 +08:00
this.next();
2017-11-07 17:07:03 +08:00
} else if (key === 13) {
2016-09-09 14:29:19 +08:00
let page = 1;
if (val > this.allPages) {
page = this.allPages;
2017-11-07 17:07:03 +08:00
} else if (val <= 0 || !val) {
2016-09-09 14:29:19 +08:00
page = 1;
} else {
page = val;
}
e.target.value = page;
this.changePage(page);
}
}
}
2016-12-25 22:49:42 +08:00
};
2016-10-28 17:24:52 +08:00
</script>