-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathDataTablesServer.vue
100 lines (96 loc) · 2 KB
/
DataTablesServer.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
<style lang='scss'>
@import "../style/index.scss";
</style>
<script>
import ShareMixin from '../mixins/ShareMixin'
export default {
name: 'DataTablesServer',
mixins: [ShareMixin],
props: {
loadingStr: {
type: String,
default: ''
},
total: {
type: Number
},
loading: {
type: Boolean,
default: false
}
},
data() {
return {
innerTotal: 0
}
},
created() {
this._server = true
this.queryChange('init')
// fix https://github.com/njleonzhang/vue-data-tables/issues/172
let totalPage = this.total / this.pageSize
let ceilTotalPage = Math.ceil(totalPage)
this.innerTotal = ceilTotalPage >= this.currentPage
? this.total
: this.pageSize * this.currentPage
},
computed: {
curTableData() {
return this.data
},
queryInfo() {
return {
page: this.innerCurrentPage,
pageSize: this.innerPageSize,
sort: this.sortData,
filters: this.filters
}
},
},
methods: {
queryChange(type) {
let info = {
type,
...this.queryInfo
}
this.$emit('query-change', info)
},
handleSort(obj) {
let { prop, order } = obj
// avoid event emit, if both prop and order are not change, special scenario 'multi-columns share the same prop'
if (this.sortData.prop !== prop || this.sortData.order !== order) {
this.sortData = {
prop,
order
}
this.queryChange('sort')
}
},
handleSizeChange(size) {
this.innerPageSize = size
this.queryChange('size')
this.$emit('size-change', size)
},
},
watch: {
total(val) {
this.innerTotal = val
},
filters: {
handler() {
this.queryChange('filter')
},
deep: true
},
'tableProps.defaultSort': {
immediate: true,
handler(val) {
this.sortData = val || {}
}
},
innerCurrentPage(val) {
this.queryChange('page')
}
}
}
</script>