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
217 lines (182 loc) · 5.56 KB
/
table-pagination.js
File metadata and controls
217 lines (182 loc) · 5.56 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { ref, computed, watch, nextTick } from 'vue'
function samePagination (oldPag, newPag) {
for (const 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 const useTablePaginationProps = {
pagination: Object,
rowsPerPageOptions: {
type: Array,
default: () => [ 5, 7, 10, 15, 20, 25, 50, 0 ]
},
'onUpdate:pagination': [ Function, Array ]
}
export function useTablePaginationState (vm, getCellValue) {
const { props, emit } = vm
const innerPagination = ref(
Object.assign({
sortBy: null,
descending: false,
page: 1,
rowsPerPage: props.rowsPerPageOptions.length > 0
? props.rowsPerPageOptions[ 0 ]
: 5
}, props.pagination)
)
const computedPagination = computed(() => {
const pag = props[ 'onUpdate:pagination' ] !== void 0
? { ...innerPagination.value, ...props.pagination }
: innerPagination.value
return fixPagination(pag)
})
const isServerSide = computed(() => computedPagination.value.rowsNumber !== void 0)
function sendServerRequest (pagination) {
requestServerInteraction({
pagination,
filter: props.filter
})
}
function requestServerInteraction (prop = {}) {
nextTick(() => {
emit('request', {
pagination: prop.pagination || computedPagination.value,
// FIXME: 'props.filter' is string/object, but 'prop.filter' can be controlled by the user, and the docs are suggesting 'prop.filter' is a function
// So, value of 'filter' becomes function/string/object, which makes a lot of things unpredictable and can break things
// Either update the docs to say 'prop.filter' should be a string/object, or use 'prop.filter || props.filterMethod' or maybe get 'computedFilterFunction' here and use that instead of 'props.filterMethod'
// The examples on our docs are using 'filter' as a string in onRequest handler, but the JSON API is saying 'filter' is a function
filter: prop.filter || props.filter,
getCellValue
})
})
}
function setPagination (val, forceServerRequest) {
const newPagination = fixPagination({
...computedPagination.value,
...val
})
if (samePagination(computedPagination.value, newPagination) === true) {
if (isServerSide.value === true && forceServerRequest === true) {
sendServerRequest(newPagination)
}
return
}
if (isServerSide.value === true) {
sendServerRequest(newPagination)
return
}
if (
props.pagination !== void 0
&& props[ 'onUpdate:pagination' ] !== void 0
) {
emit('update:pagination', newPagination)
}
else {
innerPagination.value = newPagination
}
}
return {
innerPagination,
computedPagination,
isServerSide,
requestServerInteraction,
setPagination
}
}
export function useTablePagination (vm, innerPagination, computedPagination, isServerSide, setPagination, filteredSortedRowsNumber) {
const { props, emit, proxy: { $q } } = vm
const computedRowsNumber = computed(() => (
isServerSide.value === true
? computedPagination.value.rowsNumber || 0
: filteredSortedRowsNumber.value
))
const firstRowIndex = computed(() => {
const { page, rowsPerPage } = computedPagination.value
return (page - 1) * rowsPerPage
})
const lastRowIndex = computed(() => {
const { page, rowsPerPage } = computedPagination.value
return page * rowsPerPage
})
const isFirstPage = computed(() => computedPagination.value.page === 1)
const pagesNumber = computed(() => (
computedPagination.value.rowsPerPage === 0
? 1
: Math.max(
1,
Math.ceil(computedRowsNumber.value / computedPagination.value.rowsPerPage)
)
))
const isLastPage = computed(() => (
lastRowIndex.value === 0
? true
: computedPagination.value.page >= pagesNumber.value
))
const computedRowsPerPageOptions = computed(() => {
const opts = props.rowsPerPageOptions.includes(innerPagination.value.rowsPerPage)
? props.rowsPerPageOptions
: [ innerPagination.value.rowsPerPage ].concat(props.rowsPerPageOptions)
return opts.map(count => ({
label: count === 0 ? $q.lang.table.allRows : '' + count,
value: count
}))
})
watch(pagesNumber, (lastPage, oldLastPage) => {
if (lastPage === oldLastPage) {
return
}
const currentPage = computedPagination.value.page
if (lastPage && !currentPage) {
setPagination({ page: 1 })
}
else if (lastPage < currentPage) {
setPagination({ page: lastPage })
}
})
function firstPage () {
setPagination({ page: 1 })
}
function prevPage () {
const { page } = computedPagination.value
if (page > 1) {
setPagination({ page: page - 1 })
}
}
function nextPage () {
const { page, rowsPerPage } = computedPagination.value
if (lastRowIndex.value > 0 && page * rowsPerPage < computedRowsNumber.value) {
setPagination({ page: page + 1 })
}
}
function lastPage () {
setPagination({ page: pagesNumber.value })
}
if (props[ 'onUpdate:pagination' ] !== void 0) {
emit('update:pagination', { ...computedPagination.value })
}
return {
firstRowIndex,
lastRowIndex,
isFirstPage,
isLastPage,
pagesNumber,
computedRowsPerPageOptions,
computedRowsNumber,
firstPage,
prevPage,
nextPage,
lastPage
}
}