55
55
</v-row >
56
56
<v-row justify =" center" v-else class =" card-wrapper" >
57
57
<BallotCard
58
- v-for =" (item, i) in pageData.items"
58
+ v-for =" (item, i) in pageData.items[pageData.page - 1] "
59
59
:key =" i"
60
60
v-bind =" item"
61
61
:is-voting-disabled =" isVotingDisabled"
66
66
<v-pagination
67
67
v-model =" pageData.page"
68
68
:length =" pageData.totalPages"
69
+ @input =" nextPageOnClick"
69
70
circle
70
71
></v-pagination >
71
72
</v-row >
@@ -106,13 +107,13 @@ export default {
106
107
item: null ,
107
108
per: 10 ,
108
109
pageData: {
109
- hasNext: false ,
110
- hasPrev: false ,
111
- nextNum: false ,
110
+ // hasNext: false,
111
+ // hasPrev: false,
112
+ // nextNum: false,
112
113
page: - 1 ,
113
114
items: [],
114
- prevNum: null ,
115
- totalItems: 0 ,
115
+ // prevNum: null,
116
+ // totalItems: 0,
116
117
totalPages: 0
117
118
}
118
119
};
@@ -122,32 +123,53 @@ export default {
122
123
this .item = item;
123
124
this .showModal = true ;
124
125
},
126
+ nextPageOnClick (page ) {
127
+ Vue .set (this .pageData , " page" , page);
128
+ this .updateQueryParams ();
129
+ },
125
130
async setResult (result ) {
126
- await new Promise (resolve =>
131
+ await new Promise (resolve => {
127
132
setTimeout (async () => {
128
- for (const [key , value ] of Object .entries (result)) {
129
- Vue .set (this .pageData , key, value);
133
+ // shuffle the results if no search string given
134
+ let shuffled;
135
+ if (this .searchText .length > 0 ) {
136
+ shuffled = result .items ;
137
+ } else {
138
+ shuffled = this .shuffle (result .items );
139
+ }
140
+
141
+ // push into sub arrays
142
+ let postShuffled = [];
143
+ while (shuffled .length > 0 ) {
144
+ postShuffled .push (shuffled .splice (0 , 10 ));
130
145
}
146
+
147
+ // set data
148
+ Vue .set (this .pageData , " items" , postShuffled);
149
+ Vue .set (this .pageData , " totalPages" , this .pageData .items .length );
150
+
151
+ // for (const [key, value] of Object.entries(result)) {
152
+ // if (key !== "items") {
153
+ // Vue.set(this.pageData, key, value);
154
+ // }
155
+ // }
156
+ // Vue.set(this.pageData, "items", shuffled);
131
157
await this .updateQueryParams ();
132
158
resolve ();
133
- }, 1000 )
134
- );
159
+ }, 1000 );
160
+ } );
135
161
},
136
162
async search () {
137
163
if (this .searchText === " " ) {
138
164
return this .loadPage ();
139
165
}
140
- this .pageData .page = 1 ;
166
+ // this.pageData.page = 1;
141
167
this .requestIndex ++ ;
142
168
this .requestCount ++ ;
143
169
const requestIndex = this .requestIndex ;
144
170
const searchText = this .searchText ;
145
171
try {
146
- const results = await voting .search (
147
- this .searchText ,
148
- this .pageData .page ,
149
- this .per
150
- );
172
+ const results = await voting .search (this .searchText , 1 , 10000 );
151
173
if (
152
174
this .searchText === searchText &&
153
175
this .requestIndex === requestIndex
@@ -172,7 +194,7 @@ export default {
172
194
async loadPage () {
173
195
this .requestCount ++ ;
174
196
try {
175
- const results = await voting .getBallot (this . pageData . page , this . per );
197
+ const results = await voting .getBallot (1 , 10000 );
176
198
await this .setResult (results);
177
199
} catch (err) {
178
200
if (err .status === 404 ) {
@@ -207,6 +229,20 @@ export default {
207
229
} else {
208
230
this .search ();
209
231
}
232
+ },
233
+ /**
234
+ * Shuffles array in place.
235
+ * @param {Array} a items An array containing the items.
236
+ */
237
+ shuffle (a ) {
238
+ var j, x, i;
239
+ for (i = a .length - 1 ; i > 0 ; i-- ) {
240
+ j = Math .floor (Math .random () * (i + 1 ));
241
+ x = a[i];
242
+ a[i] = a[j];
243
+ a[j] = x;
244
+ }
245
+ return a;
210
246
}
211
247
},
212
248
computed: {
@@ -217,18 +253,18 @@ export default {
217
253
watch: {
218
254
searchText () {
219
255
this .search ();
220
- },
221
- [" pageData.page" ]() {
222
- this .refresh ();
223
- },
224
- [" $route.query.page" ](val ) {
225
- const page = parseInt (val);
226
- if (this .pageData .page === page) {
227
- return ;
228
- }
229
- this .pageData .page = page;
230
- this .refresh ();
231
256
}
257
+ // ["pageData.page"]() {
258
+ // this.refresh();
259
+ // },
260
+ // ["$route.query.page"](val) {
261
+ // const page = parseInt(val);
262
+ // if (this.pageData.page === page) {
263
+ // return;
264
+ // }
265
+ // this.pageData.page = page;
266
+ // this.refresh();
267
+ // }
232
268
},
233
269
async mounted () {
234
270
this .searchText =
@@ -237,6 +273,8 @@ export default {
237
273
this .$route .query .page === undefined
238
274
? 1
239
275
: parseInt (this .$route .query .page );
276
+
277
+ this .refresh ();
240
278
}
241
279
};
242
280
</script >
0 commit comments