forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable-pagination.js
More file actions
134 lines (127 loc) · 3.15 KB
/
table-pagination.js
File metadata and controls
134 lines (127 loc) · 3.15 KB
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
132
133
134
import extend from '../../utils/extend'
function samePagination (oldPag, newPag) {
for (let prop in newPag) {
if (newPag[prop] !== oldPag[prop]) {
return false
}
}
return true
}
function fixPagination (p) {
if (p.page < 1) {
p.page = 1
}
if (p.rowsPerPage !== void 0 && p.rowsPerPage < 1) {
p.rowsPerPage = 0
}
return p
}
export default {
props: {
pagination: Object,
rowsPerPageOptions: {
type: Array,
default: () => [3, 5, 7, 10, 15, 20, 25, 50, 0]
}
},
data () {
return {
innerPagination: {
sortBy: null,
descending: false,
page: 1,
rowsPerPage: 5
}
}
},
computed: {
computedPagination () {
return fixPagination(extend({}, this.innerPagination, this.pagination))
},
firstRowIndex () {
const { page, rowsPerPage } = this.computedPagination
return (page - 1) * rowsPerPage
},
lastRowIndex () {
const { page, rowsPerPage } = this.computedPagination
return page * rowsPerPage
},
isFirstPage () {
return this.computedPagination.page === 1
},
pagesNumber () {
return Math.max(
1,
Math.ceil(this.computedRowsNumber / this.computedPagination.rowsPerPage)
)
},
isLastPage () {
if (this.lastRowIndex === 0) {
return true
}
return this.computedPagination.page >= this.pagesNumber
},
computedRowsPerPageOptions () {
return this.rowsPerPageOptions.map(count => ({
label: count === 0 ? this.$q.i18n.table.allRows : '' + count,
value: count
}))
}
},
watch: {
pagesNumber (lastPage, oldLastPage) {
if (lastPage === oldLastPage) {
return
}
const currentPage = this.computedPagination.page
if (lastPage && !currentPage) {
this.setPagination({ page: 1 })
}
else if (lastPage < currentPage) {
this.setPagination({ page: lastPage })
}
}
},
methods: {
__sendServerRequest (pagination) {
this.requestServerInteraction({
pagination,
filter: this.filter
})
},
setPagination (val, forceServerRequest) {
const newPagination = fixPagination(extend({}, this.computedPagination, val))
if (samePagination(this.computedPagination, newPagination)) {
if (this.isServerSide && forceServerRequest) {
this.__sendServerRequest(newPagination)
}
return
}
if (this.isServerSide) {
this.__sendServerRequest(newPagination)
return
}
if (this.pagination) {
this.$emit('update:pagination', newPagination)
}
else {
this.innerPagination = newPagination
}
},
prevPage () {
const { page } = this.computedPagination
if (page > 1) {
this.setPagination({page: page - 1})
}
},
nextPage () {
const { page, rowsPerPage } = this.computedPagination
if (this.lastRowIndex > 0 && page * rowsPerPage < this.computedRowsNumber) {
this.setPagination({page: page + 1})
}
}
},
created () {
this.$emit('update:pagination', extend({}, this.computedPagination))
}
}