2016-07-29 09:46:16 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import Pager from './pager.vue';
|
2016-10-13 03:12:24 +00:00
|
|
|
import ElSelect from 'element-ui/packages/select/index.js';
|
|
|
|
import ElOption from 'element-ui/packages/option/index.js';
|
2016-07-29 09:46:16 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'ElPagination',
|
|
|
|
|
|
|
|
props: {
|
|
|
|
pageSize: {
|
|
|
|
type: Number,
|
|
|
|
default: 10
|
|
|
|
},
|
|
|
|
|
|
|
|
small: Boolean,
|
|
|
|
|
|
|
|
total: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
currentPage: {
|
|
|
|
type: Number,
|
|
|
|
default: 1
|
|
|
|
},
|
|
|
|
|
|
|
|
layout: {
|
2016-08-19 10:54:18 +00:00
|
|
|
default: 'prev, pager, next, jumper, ->, total'
|
2016-07-29 09:46:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
pageSizes: {
|
|
|
|
type: Array,
|
|
|
|
default() {
|
|
|
|
return [10, 20, 30, 40, 50, 100];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
internalCurrentPage: 1,
|
|
|
|
internalPageSize: 0
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
render(h) {
|
|
|
|
let template = <div class='el-pagination'></div>;
|
2016-10-21 08:14:14 +00:00
|
|
|
const layout = this.layout || '';
|
|
|
|
if (!layout) return;
|
2016-07-29 09:46:16 +00:00
|
|
|
const TEMPLATE_MAP = {
|
|
|
|
prev: <prev></prev>,
|
|
|
|
jumper: <jumper></jumper>,
|
2016-08-15 05:35:16 +00:00
|
|
|
pager: <pager currentPage={ this.internalCurrentPage } pageCount={ this.pageCount } on-currentchange={ this.handleCurrentChange }></pager>,
|
2016-07-29 09:46:16 +00:00
|
|
|
next: <next></next>,
|
|
|
|
sizes: <sizes></sizes>,
|
|
|
|
slot: <slot></slot>,
|
|
|
|
total: <total></total>
|
|
|
|
};
|
|
|
|
const components = layout.split(',').map((item) => item.trim());
|
|
|
|
const rightWrapper = <div class="el-pagination__rightwrapper"></div>;
|
|
|
|
let haveRightWrapper = false;
|
|
|
|
|
|
|
|
if (this.small) {
|
|
|
|
template.data.class += ' el-pagination--small';
|
|
|
|
}
|
|
|
|
|
|
|
|
components.forEach(compo => {
|
|
|
|
if (compo === '->') {
|
|
|
|
haveRightWrapper = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!haveRightWrapper) {
|
|
|
|
template.children.push(TEMPLATE_MAP[compo]);
|
|
|
|
} else {
|
|
|
|
rightWrapper.children.push(TEMPLATE_MAP[compo]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (haveRightWrapper) {
|
|
|
|
template.children.push(rightWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
return template;
|
|
|
|
},
|
|
|
|
|
|
|
|
components: {
|
|
|
|
Prev: {
|
|
|
|
render(h) {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
class={['btn-prev', { disabled: this.$parent.internalCurrentPage <= 1 }]}
|
|
|
|
on-click={ this.$parent.prev }>
|
|
|
|
<i class="el-icon el-icon-arrow-left"></i>
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Next: {
|
|
|
|
render(h) {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
class={
|
|
|
|
[
|
|
|
|
'btn-next',
|
|
|
|
{ disabled: this.$parent.internalCurrentPage === this.$parent.pageCount }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
on-click={ this.$parent.next }>
|
|
|
|
<i class="el-icon el-icon-arrow-right"></i>
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Sizes: {
|
2016-08-15 05:35:16 +00:00
|
|
|
created() {
|
|
|
|
if (Array.isArray(this.$parent.pageSizes)) {
|
2016-10-21 08:14:14 +00:00
|
|
|
this.$parent.internalPageSize = this.$parent.pageSizes.indexOf(this.$parent.pageSize) > -1
|
|
|
|
? this.$parent.pageSize
|
|
|
|
: this.$parent.pageSizes[0];
|
2016-08-15 05:35:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-29 09:46:16 +00:00
|
|
|
render(h) {
|
|
|
|
return (
|
|
|
|
<span class="el-pagination__sizes">
|
|
|
|
<el-select
|
2016-08-11 04:20:29 +00:00
|
|
|
size="small"
|
|
|
|
value={ this.$parent.internalPageSize }
|
2016-07-29 09:46:16 +00:00
|
|
|
on-change={ this.handleChange }
|
2016-08-11 04:20:29 +00:00
|
|
|
width={ 110 }>
|
2016-07-29 09:46:16 +00:00
|
|
|
{
|
|
|
|
this.$parent.pageSizes.map(item =>
|
|
|
|
<el-option
|
2016-08-11 04:20:29 +00:00
|
|
|
value={ item }
|
|
|
|
label={ item + ' 条/页' }>
|
2016-07-29 09:46:16 +00:00
|
|
|
</el-option>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</el-select>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
components: {
|
|
|
|
ElSelect,
|
|
|
|
ElOption
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
handleChange(val) {
|
|
|
|
if (val !== this.$parent.internalPageSize) {
|
|
|
|
this.$parent.internalPageSize = val = parseInt(val, 10);
|
2016-08-15 05:35:16 +00:00
|
|
|
this.$parent.$emit('sizechange', val);
|
2016-07-29 09:46:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Jumper: {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
oldValue: null
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
handleFocus(event) {
|
|
|
|
this.oldValue = event.target.value;
|
|
|
|
},
|
|
|
|
|
2016-10-12 05:40:35 +00:00
|
|
|
handleChange({ target }) {
|
2016-07-29 09:46:16 +00:00
|
|
|
this.$parent.internalCurrentPage = this.$parent.getValidCurrentPage(target.value);
|
2016-10-12 05:40:35 +00:00
|
|
|
this.$parent.$emit('currentchange', this.$parent.internalCurrentPage);
|
2016-07-29 09:46:16 +00:00
|
|
|
this.oldValue = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render(h) {
|
|
|
|
return (
|
|
|
|
<span class="el-pagination__jump">
|
|
|
|
前往
|
|
|
|
<input
|
|
|
|
class="el-pagination__editor"
|
|
|
|
type="number"
|
|
|
|
min={ 1 }
|
|
|
|
max={ this.pageCount }
|
2016-10-12 05:40:35 +00:00
|
|
|
domProps-value={ this.$parent.internalCurrentPage }
|
2016-07-29 09:46:16 +00:00
|
|
|
on-change={ this.handleChange }
|
|
|
|
on-focus={ this.handleFocus }
|
|
|
|
style={{ width: '30px' }}
|
2016-10-12 05:40:35 +00:00
|
|
|
number/>
|
2016-07-29 09:46:16 +00:00
|
|
|
页
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Total: {
|
|
|
|
render(h) {
|
|
|
|
return (
|
|
|
|
<span class="el-pagination__total">共 { this.$parent.total } 条</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Pager
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
handleCurrentChange(val) {
|
|
|
|
this.internalCurrentPage = this.getValidCurrentPage(val);
|
2016-08-15 05:35:16 +00:00
|
|
|
this.$emit('currentchange', this.internalCurrentPage);
|
2016-07-29 09:46:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
prev() {
|
|
|
|
const oldPage = this.internalCurrentPage;
|
|
|
|
const newVal = this.internalCurrentPage - 1;
|
|
|
|
this.internalCurrentPage = this.getValidCurrentPage(newVal);
|
|
|
|
|
|
|
|
if (this.internalCurrentPage !== oldPage) {
|
2016-08-15 05:35:16 +00:00
|
|
|
this.$emit('currentchange', this.internalCurrentPage);
|
2016-07-29 09:46:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
next() {
|
|
|
|
const oldPage = this.internalCurrentPage;
|
|
|
|
const newVal = this.internalCurrentPage + 1;
|
|
|
|
this.internalCurrentPage = this.getValidCurrentPage(newVal);
|
|
|
|
|
|
|
|
if (this.internalCurrentPage !== oldPage) {
|
2016-08-15 05:35:16 +00:00
|
|
|
this.$emit('currentchange', this.internalCurrentPage);
|
2016-07-29 09:46:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-21 08:14:14 +00:00
|
|
|
// XXX: 暂时没有到第一页和最后一页的交互
|
|
|
|
// first() {
|
|
|
|
// const oldPage = this.internalCurrentPage;
|
|
|
|
// const newVal = 1;
|
|
|
|
// this.internalCurrentPage = this.getValidCurrentPage(newVal);
|
2016-07-29 09:46:16 +00:00
|
|
|
|
2016-10-21 08:14:14 +00:00
|
|
|
// if (this.internalCurrentPage !== oldPage) {
|
|
|
|
// this.$emit('currentchange', this.internalCurrentPage);
|
|
|
|
// }
|
|
|
|
// },
|
2016-07-29 09:46:16 +00:00
|
|
|
|
2016-10-21 08:14:14 +00:00
|
|
|
// last() {
|
|
|
|
// const oldPage = this.internalCurrentPage;
|
|
|
|
// const newVal = this.pageCount;
|
|
|
|
// this.internalCurrentPage = this.getValidCurrentPage(newVal);
|
2016-07-29 09:46:16 +00:00
|
|
|
|
2016-10-21 08:14:14 +00:00
|
|
|
// if (this.internalCurrentPage !== oldPage) {
|
|
|
|
// this.$emit('currentchange', this.internalCurrentPage);
|
|
|
|
// }
|
|
|
|
// },
|
2016-07-29 09:46:16 +00:00
|
|
|
|
|
|
|
getValidCurrentPage(value) {
|
|
|
|
value = parseInt(value, 10);
|
|
|
|
|
|
|
|
var resetValue;
|
|
|
|
if (value < 1) {
|
|
|
|
resetValue = this.pageCount > 0 ? 1 : 0;
|
|
|
|
} else if (value > this.pageCount) {
|
|
|
|
resetValue = this.pageCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resetValue === undefined && isNaN(value)) {
|
|
|
|
value = this.pageCount > 0 ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return resetValue === undefined ? value : resetValue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
pageCount() {
|
|
|
|
return Math.ceil(this.total / this.internalPageSize);
|
2016-10-21 08:14:14 +00:00
|
|
|
}
|
2016-07-29 09:46:16 +00:00
|
|
|
|
2016-10-21 08:14:14 +00:00
|
|
|
// XXX: 暂时没用到
|
|
|
|
// startRecordIndex() {
|
|
|
|
// const result = (this.internalCurrentPage - 1) * this.internalPageSize + 1;
|
|
|
|
// return result > 0 ? result : 0;
|
|
|
|
// },
|
2016-07-29 09:46:16 +00:00
|
|
|
|
2016-10-21 08:14:14 +00:00
|
|
|
// endRecordIndex() {
|
|
|
|
// const result = this.internalCurrentPage * this.internalPageSize;
|
|
|
|
// return result > this.total ? this.total : result;
|
|
|
|
// }
|
2016-07-29 09:46:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
pageCount(newVal) {
|
2016-10-21 08:14:14 +00:00
|
|
|
/* istanbul ignore if */
|
2016-07-29 09:46:16 +00:00
|
|
|
if (newVal > 0 && this.internalCurrentPage === 0) {
|
|
|
|
this.internalCurrentPage = 1;
|
|
|
|
} else if (this.internalCurrentPage > newVal) {
|
|
|
|
this.internalCurrentPage = newVal;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
currentPage: {
|
|
|
|
immediate: true,
|
|
|
|
handler(val) {
|
|
|
|
this.internalCurrentPage = val;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
pageSize: {
|
|
|
|
immediate: true,
|
|
|
|
handler(val) {
|
|
|
|
this.internalPageSize = val;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
internalCurrentPage(newVal, oldVal) {
|
|
|
|
newVal = parseInt(newVal, 10);
|
|
|
|
|
2016-10-21 08:14:14 +00:00
|
|
|
/* istanbul ignore if */
|
2016-07-29 09:46:16 +00:00
|
|
|
if (isNaN(newVal)) {
|
|
|
|
newVal = oldVal || 1;
|
|
|
|
} else {
|
|
|
|
newVal = this.getValidCurrentPage(newVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newVal !== undefined) {
|
|
|
|
Vue.nextTick(() => {
|
|
|
|
this.internalCurrentPage = newVal;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|