-
Notifications
You must be signed in to change notification settings - Fork 6
/
Pagination.vue
131 lines (110 loc) · 3 KB
/
Pagination.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<template>
<div class="pagination__container">
<el-pagination
v-if="!(total <= 0 || total <= limit )"
layout="prev, pager, next"
:total="total"
:page-size="limit"
:current-page="page"
@current-change="onPageChange"
:disabled="(!enable || loading)"
/>
</div>
</template>
<script>
export default {
name: "Pagination",
model: { prop: 'resolved', event: 'changed' },
props: {
unique: { default: 'default' },
limit: { type: Number, default: 40 },
total: { type: Number, default: 0 },
index: { type: Boolean, default: false },
enable: { type: Boolean, default: true },
filling: {
type: Function,
default: (...args) => { console.warn( 'expect a "filling" func' ); return {}}
},
resolved: null
},
data() {
return {
loading: false,
datas: {},
offset: 0,
page: 1
}
},
methods: {
onPageChange: function (val) {
this.page = val
this.offset = (val - 1) * this.limit
this.resolve()
},
resolve(first) {
if (!this.existPage(this.page)) {
this.load(first)
}else {
this.commit()
}
},
load(first) {
if (!this.enable) return
this.$emit('loading'); this.loading = true
this.fillingData(first).then(res => {
this.commit();
this.$emit('loaded'); this.loading = false
})
},
async fillingData(first) {
let result = this.filling(this.offset, this.limit, first)
if (result.then && (typeof result.then) === 'function') {
return new Promise((resolve, reject) => {
result.then(res => {
if (Array.isArray(res)) { this.loadinfo(this.page, res); resolve(res) }
else console.error('res" no array')
})
})
}else console.error(' expect a Promise ')
},
loadinfo(page, data) {
if (!this.datas[this.unique]) this.datas[this.unique] = {}
this.datas[this.unique][page] = []
this.datas[this.unique][page].push(...data)
},
discharge(page) { return this.datas[this.unique][page] },
existPage(page) {
return this.datas[this.unique] && this.datas[this.unique][page]
},
commit() {
let res = this.discharge(this.page).map(( value, index ) => {
if (this.index) value.__index = index + this.offset + 1
return value
})
this.$emit('changed', res)
},
reload() {
this.$emit('changed', [])
this.offset = 0
this.page = 1
this.resolve(true)
},
},
created() {
this.reload()
},
watch: {
unique: function (newVal, oldVal) {
if (newVal !== oldVal) this.reload()
},
enable: function () {
this.resolve()
}
}
}
</script>
<style scoped>
.pagination__container .el-pagination{
text-align: center;
}
</style>