Skip to content

Commit 835b29a

Browse files
committed
Update with filter use for the v2.2.x
1 parent 698f805 commit 835b29a

File tree

1 file changed

+84
-3
lines changed

1 file changed

+84
-3
lines changed

src/components/WithFilter.vue

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<h3>Use with filter</h3>
33
<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>
45

56
<h4>Template</h4>
67
<pre v-highlightjs v-if="$parent.docVersion < 2">&lt;div class=&quot;hacker-news-list&quot;&gt;
@@ -40,7 +41,7 @@
4041
&lt;/span&gt;
4142
&lt;/infinite-loading&gt;
4243
&lt;/div&gt;</pre>
43-
<pre v-highlightjs v-if="$parent.docVersion >= 2">&lt;div class=&quot;hacker-news-list&quot;&gt;
44+
<pre v-highlightjs v-if="$parent.docVersion >= 2 && $parent.docVersion < 2.2">&lt;div class=&quot;hacker-news-list&quot;&gt;
4445
&lt;div class=&quot;hacker-news-header&quot;&gt;
4546
&lt;a target=&quot;_blank&quot; href=&quot;http://www.ycombinator.com/&quot;&gt;
4647
&lt;img src=&quot;https://news.ycombinator.com/y18.gif&quot;&gt;
@@ -76,6 +77,43 @@
7677
There is no more Hacker News :(
7778
&lt;/span&gt;
7879
&lt;/infinite-loading&gt;
80+
&lt;/div&gt;</pre>
81+
<pre v-highlightjs v-if="$parent.docVersion >= 2.2">&lt;div class=&quot;hacker-news-list&quot;&gt;
82+
&lt;div class=&quot;hacker-news-header&quot;&gt;
83+
&lt;a target=&quot;_blank&quot; href=&quot;http://www.ycombinator.com/&quot;&gt;
84+
&lt;img src=&quot;https://news.ycombinator.com/y18.gif&quot;&gt;
85+
&lt;/a&gt;
86+
&lt;span&gt;Hacker News&lt;/span&gt;
87+
&lt;select v-model=&quot;tag&quot; @change=&quot;changeFilter()&quot;&gt;
88+
&lt;option value=&quot;story&quot;&gt;Story&lt;/option&gt;
89+
&lt;option value=&quot;poll&quot;&gt;Poll&lt;/option&gt;
90+
&lt;option value=&quot;show_hn&quot;&gt;Show hn&lt;/option&gt;
91+
&lt;option value=&quot;ask_hn&quot;&gt;Ask hn&lt;/option&gt;
92+
&lt;option value=&quot;front_page&quot;&gt;Front page&lt;/option&gt;
93+
&lt;/select&gt;
94+
&lt;/div&gt;
95+
&lt;div class=&quot;hacker-news-item&quot; v-for=&quot;(item, key) in list&quot;&gt;
96+
&lt;span class=&quot;num&quot; v-text=&quot;key + 1&quot;&gt;&lt;/span&gt;
97+
&lt;p&gt;
98+
&lt;a target=&quot;_blank&quot; :href=&quot;item.url&quot; v-text=&quot;item.title&quot;&gt;&lt;/a&gt;
99+
&lt;/p&gt;
100+
&lt;p&gt;
101+
&lt;small&gt;
102+
&lt;span v-text=&quot;item.points&quot;&gt;&lt;/span&gt;
103+
points by
104+
&lt;a target=&quot;_blank&quot; :href=&quot;'https://news.ycombinator.com/user?id=' + item.author&quot;
105+
v-text=&quot;item.author&quot;&gt;&lt;/a&gt;
106+
|
107+
&lt;a target=&quot;_blank&quot; :href=&quot;'https://news.ycombinator.com/item?id=' + item.objectID&quot;
108+
v-text=&quot;item.num_comments + ' comments'&quot;&gt;&lt;/a&gt;
109+
&lt;/small&gt;
110+
&lt;/p&gt;
111+
&lt;/div&gt;
112+
&lt;infinite-loading @infinite=&quot;infiniteHandler&quot; ref=&quot;infiniteLoading&quot;&gt;
113+
&lt;span slot=&quot;no-more&quot;&gt;
114+
There is no more Hacker News :(
115+
&lt;/span&gt;
116+
&lt;/infinite-loading&gt;
79117
&lt;/div&gt;</pre>
80118
<h4>Script</h4>
81119
<pre v-highlightjs v-if="$parent.docVersion < 1">import InfiniteLoading from 'vue-infinite-loading';
@@ -156,7 +194,8 @@ export default {
156194
InfiniteLoading,
157195
},
158196
};</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';
160199

161200
const api = 'http://hn.algolia.com/api/v1/search_by_date';
162201

@@ -169,7 +208,7 @@ export default {
169208
},
170209
methods: {
171210
onInfinite() {
172-
this.$http.get(api, {
211+
axios.get(api, {
173212
params: {
174213
tags: this.tag,
175214
page: this.list.length / 20 + 1,
@@ -196,6 +235,48 @@ export default {
196235
components: {
197236
InfiniteLoading,
198237
},
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 =&gt; res.json()).then((data) =&gt; {
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(() =&gt; {
273+
this.$refs.infiniteLoading.$emit('$InfiniteLoading:reset');
274+
});
275+
},
276+
},
277+
components: {
278+
InfiniteLoading,
279+
},
199280
};</pre>
200281
<p>
201282
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

Comments
 (0)