forked from rancher/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaging.js
More file actions
147 lines (114 loc) · 2.94 KB
/
paging.js
File metadata and controls
147 lines (114 loc) · 2.94 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
import { ROWS_PER_PAGE } from '@shell/store/prefs';
export default {
computed: {
totalRows() {
if (this.externalPaginationEnabled) {
return this.externalPaginationResult?.count || 0;
}
return this.filteredRows.length;
},
indexFrom() {
return Math.max(0, 1 + this.perPage * (this.page - 1));
},
indexTo() {
return Math.min(this.totalRows, this.indexFrom + this.perPage - 1);
},
totalPages() {
return Math.ceil(this.totalRows / this.perPage );
},
showPaging() {
if (!this.paging) {
return false;
}
const havePages = this.totalPages > 1;
if (this.altLoading) {
return havePages;
}
return !this.loading && havePages;
},
pagingDisplay() {
const opt = {
...(this.pagingParams || {}),
count: this.totalRows,
pages: this.totalPages,
from: this.indexFrom,
to: this.indexTo,
};
return this.$store.getters['i18n/t'](this.pagingLabel, opt);
},
pagedRows() {
if (this.externalPaginationEnabled) {
return this.rows;
} else if ( this.paging ) {
return this.filteredRows.slice(this.indexFrom - 1, this.indexTo);
} else {
return this.filteredRows;
}
}
},
data() {
const perPage = this.getPerPage();
return { page: 1, perPage };
},
watch: {
pagedRows() {
// Go to the last page if we end up "past" the last page because the table changed
const from = this.indexFrom;
const last = this.totalRows;
if ( this.totalPages > 0 && this.page > 1 && from > last ) {
this.setPage(this.totalPages);
}
},
page() {
this.debouncedPaginationChanged();
},
perPage() {
this.debouncedPaginationChanged();
},
},
methods: {
getPerPage() {
// perPage can not change while the list is displayed
let out = this.rowsPerPage || 0;
if ( out <= 0 ) {
out = parseInt(this.$store.getters['prefs/get'](ROWS_PER_PAGE), 10) || 0;
}
// This should ideally never happen, but the preference value could be invalid, so return something...
if ( out <= 0 ) {
out = 10;
}
return out;
},
setPage(num) {
if (this.page === num) {
return;
}
this.page = num;
},
goToPage(which) {
let page;
switch (which) {
case 'first':
page = 1;
break;
case 'prev':
page = Math.max(1, this.page - 1 );
break;
case 'next':
page = Math.min(this.totalPages, this.page + 1 );
break;
case 'last':
page = this.totalPages;
break;
}
this.setPage(page);
},
getPageByRow(rowId, getRowId = (x) => x) {
const pos = this.filteredRows.map(getRowId).indexOf(rowId);
if (pos === -1) {
return null;
}
return Math.ceil(pos / this.perPage);
}
}
};