1
1
<template >
2
2
<h3 >Use with filter</h3 >
3
3
<p >On basis of the Hacker News case, we create a select as a filter into the header, the list will be reload when we changed the filter.</p >
4
+ <p v-show =" $parent.docVersion >= 2.2" >Before that, we need to set a <code >ref</code > attribute on <code >InfiniteLoading</code > component to <code >$emit</code > reset event for it</p >
4
5
5
6
<h4 >Template</h4 >
6
7
<pre v-highlightjs v-if =" $parent.docVersion < 2" >< ; div class=" ; hacker-news-list" ;> ;
40
41
< ; /span> ;
41
42
< ; /infinite-loading> ;
42
43
< ; /div> ; </pre >
43
- <pre v-highlightjs v-if =" $parent.docVersion >= 2" >< ; div class=" ; hacker-news-list" ;> ;
44
+ <pre v-highlightjs v-if =" $parent.docVersion >= 2 && $parent.docVersion < 2.2 " >< ; div class=" ; hacker-news-list" ;> ;
44
45
< ; div class=" ; hacker-news-header" ;> ;
45
46
< ; a target=" ; _blank" ; href=" ; http://www.ycombinator.com/" ;> ;
46
47
< ; img src=" ; https://news.ycombinator.com/y18.gif" ;> ;
76
77
There is no more Hacker News :(
77
78
< ; /span> ;
78
79
< ; /infinite-loading> ;
80
+ < ; /div> ; </pre >
81
+ <pre v-highlightjs v-if =" $parent.docVersion >= 2.2" >< ; div class=" ; hacker-news-list" ;> ;
82
+ < ; div class=" ; hacker-news-header" ;> ;
83
+ < ; a target=" ; _blank" ; href=" ; http://www.ycombinator.com/" ;> ;
84
+ < ; img src=" ; https://news.ycombinator.com/y18.gif" ;> ;
85
+ < ; /a> ;
86
+ < ; span> ; Hacker News< ; /span> ;
87
+ < ; select v-model=" ; tag" ; @change=" ; changeFilter()" ;> ;
88
+ < ; option value=" ; story" ;> ; Story< ; /option> ;
89
+ < ; option value=" ; poll" ;> ; Poll< ; /option> ;
90
+ < ; option value=" ; show_hn" ;> ; Show hn< ; /option> ;
91
+ < ; option value=" ; ask_hn" ;> ; Ask hn< ; /option> ;
92
+ < ; option value=" ; front_page" ;> ; Front page< ; /option> ;
93
+ < ; /select> ;
94
+ < ; /div> ;
95
+ < ; div class=" ; hacker-news-item" ; v-for=" ; (item, key) in list" ;> ;
96
+ < ; span class=" ; num" ; v-text=" ; key + 1" ;> ;< ; /span> ;
97
+ < ; p> ;
98
+ < ; a target=" ; _blank" ; :href=" ; item.url" ; v-text=" ; item.title" ;> ;< ; /a> ;
99
+ < ; /p> ;
100
+ < ; p> ;
101
+ < ; small> ;
102
+ < ; span v-text=" ; item.points" ;> ;< ; /span> ;
103
+ points by
104
+ < ; a target=" ; _blank" ; :href=" ; 'https://news.ycombinator.com/user?id=' + item.author" ;
105
+ v-text=" ; item.author" ;> ;< ; /a> ;
106
+ |
107
+ < ; a target=" ; _blank" ; :href=" ; 'https://news.ycombinator.com/item?id=' + item.objectID" ;
108
+ v-text=" ; item.num_comments + ' comments'" ;> ;< ; /a> ;
109
+ < ; /small> ;
110
+ < ; /p> ;
111
+ < ; /div> ;
112
+ < ; infinite-loading @infinite=" ; infiniteHandler" ; ref=" ; infiniteLoading" ;> ;
113
+ < ; span slot=" ; no-more" ;> ;
114
+ There is no more Hacker News :(
115
+ < ; /span> ;
116
+ < ; /infinite-loading> ;
79
117
< ; /div> ; </pre >
80
118
<h4 >Script</h4 >
81
119
<pre v-highlightjs v-if =" $parent.docVersion < 1" >import InfiniteLoading from 'vue-infinite-loading';
@@ -156,7 +194,8 @@ export default {
156
194
InfiniteLoading,
157
195
},
158
196
};</pre >
159
- <pre v-highlightjs v-if =" $parent.docVersion >= 2" >import InfiniteLoading from 'vue-infinite-loading';
197
+ <pre v-highlightjs v-if =" $parent.docVersion >= 2 && $parent.docVersion < 2.2" >import InfiniteLoading from 'vue-infinite-loading';
198
+ import axios from 'axios';
160
199
161
200
const api = 'http://hn.algolia.com/api/v1/search_by_date';
162
201
@@ -169,7 +208,7 @@ export default {
169
208
},
170
209
methods: {
171
210
onInfinite() {
172
- this.$http .get(api, {
211
+ axios .get(api, {
173
212
params: {
174
213
tags: this.tag,
175
214
page: this.list.length / 20 + 1,
@@ -196,6 +235,48 @@ export default {
196
235
components: {
197
236
InfiniteLoading,
198
237
},
238
+ };</pre >
239
+ <pre v-highlightjs v-if =" $parent.docVersion >= 2.2" >import InfiniteLoading from 'vue-infinite-loading';
240
+ import axios from 'axios';
241
+
242
+ const api = 'http://hn.algolia.com/api/v1/search_by_date';
243
+
244
+ export default {
245
+ data() {
246
+ return {
247
+ list: [],
248
+ tag: 'story',
249
+ };
250
+ },
251
+ methods: {
252
+ infiniteHandler($state) {
253
+ axios.get(api, {
254
+ params: {
255
+ tags: this.tag,
256
+ page: this.list.length / 20 + 1,
257
+ },
258
+ }).then(res => ; res.json()).then((data) => ; {
259
+ if (data.hits.length) {
260
+ this.list = this.list.concat(data.hits);
261
+ $state.loaded();
262
+ if (this.list.length / 20 === 10) {
263
+ $state.complete();
264
+ }
265
+ } else {
266
+ $state.complete();
267
+ }
268
+ });
269
+ },
270
+ changeFilter() {
271
+ this.list = [];
272
+ this.$nextTick(() => ; {
273
+ this.$refs.infiniteLoading.$emit('$InfiniteLoading:reset');
274
+ });
275
+ },
276
+ },
277
+ components: {
278
+ InfiniteLoading,
279
+ },
199
280
};</pre >
200
281
<p >
201
282
In the <code >changeFilter</code > function, we clear the list and wait for the DOM to update, then we send an <code >$InfiniteLoading:reset</code > event to let the <code >InfiniteLoading</code > component back to the original state, it will request new data immediately.
0 commit comments