|
| 1 | +<template> |
| 2 | + <h3>Trigger manually</h3> |
| 3 | + <p>In most cases, we hope load data immediately for the empty list, it is also the default behavior of this component.</p> |
| 4 | + <p>But in some special cases, you may want to trigger load action through a click or other manual behavior, there has a workaround to implement it, for example, we need to load data manually every three pages:</p> |
| 5 | + |
| 6 | + <h4>Template</h4> |
| 7 | + <pre v-highlightjs v-if="$parent.docVersion < 2"><div class="hacker-news-list"> |
| 8 | + <div class="hacker-news-header"> |
| 9 | + <a target="_blank" href="http://www.ycombinator.com/"> |
| 10 | + <img src="https://news.ycombinator.com/y18.gif"> |
| 11 | + </a> |
| 12 | + <span>Hacker News</span> |
| 13 | + </div> |
| 14 | + <div class="hacker-news-item" v-for="item in list"> |
| 15 | + <span class="num" v-text="$index + 1"></span> |
| 16 | + <p> |
| 17 | + <a target="_blank" :href="item.url" v-text="item.title"></a> |
| 18 | + </p> |
| 19 | + <p> |
| 20 | + <small> |
| 21 | + <span v-text="item.points"></span> |
| 22 | + points by |
| 23 | + <a target="_blank" :href="'https://news.ycombinator.com/user?id=' + item.author" |
| 24 | + v-text="item.author"></a> |
| 25 | + | |
| 26 | + <a target="_blank" :href="'https://news.ycombinator.com/item?id=' + item.objectID" |
| 27 | + v-text="item.num_comments + ' comments'"></a> |
| 28 | + </small> |
| 29 | + </p> |
| 30 | + </div> |
| 31 | + <button class="btn-load-more" v-show="distance < 0" @click="manualLoad">Load more</button> |
| 32 | + <infinite-loading :on-infinite="onInfinite" v-ref:infinite-loading> |
| 33 | + <span slot="no-more"> |
| 34 | + There is no more Hacker News :( |
| 35 | + </span> |
| 36 | + </infinite-loading> |
| 37 | +</div></pre> |
| 38 | + <pre v-highlightjs v-if="$parent.docVersion >= 2 && $parent.docVersion < 2.2"><div class="hacker-news-list"> |
| 39 | + <div class="hacker-news-header"> |
| 40 | + <a target="_blank" href="http://www.ycombinator.com/"> |
| 41 | + <img src="https://news.ycombinator.com/y18.gif"> |
| 42 | + </a> |
| 43 | + <span>Hacker News</span> |
| 44 | + </div> |
| 45 | + <div class="hacker-news-item" v-for="(item, key) in list"> |
| 46 | + <span class="num" v-text="key + 1"></span> |
| 47 | + <p> |
| 48 | + <a target="_blank" :href="item.url" v-text="item.title"></a> |
| 49 | + </p> |
| 50 | + <p> |
| 51 | + <small> |
| 52 | + <span v-text="item.points"></span> |
| 53 | + points by |
| 54 | + <a target="_blank" :href="'https://news.ycombinator.com/user?id=' + item.author" |
| 55 | + v-text="item.author"></a> |
| 56 | + | |
| 57 | + <a target="_blank" :href="'https://news.ycombinator.com/item?id=' + item.objectID" |
| 58 | + v-text="item.num_comments + ' comments'"></a> |
| 59 | + </small> |
| 60 | + </p> |
| 61 | + </div> |
| 62 | + <button class="btn-load-more" v-show="distance < 0" @click="manualLoad">Load more</button> |
| 63 | + <infinite-loading :on-infinite="onInfinite" ref="infiniteLoading"> |
| 64 | + <span slot="no-more"> |
| 65 | + There is no more Hacker News :( |
| 66 | + </span> |
| 67 | + </infinite-loading> |
| 68 | +</div></pre> |
| 69 | + <pre v-highlightjs v-if="$parent.docVersion >= 2.2"><div class="hacker-news-list"> |
| 70 | + <div class="hacker-news-header"> |
| 71 | + <a target="_blank" href="http://www.ycombinator.com/"> |
| 72 | + <img src="https://news.ycombinator.com/y18.gif"> |
| 73 | + </a> |
| 74 | + <span>Hacker News</span> |
| 75 | + </div> |
| 76 | + <div class="hacker-news-item" v-for="(item, key) in list"> |
| 77 | + <span class="num" v-text="key + 1"></span> |
| 78 | + <p> |
| 79 | + <a target="_blank" :href="item.url" v-text="item.title"></a> |
| 80 | + </p> |
| 81 | + <p> |
| 82 | + <small> |
| 83 | + <span v-text="item.points"></span> |
| 84 | + points by |
| 85 | + <a target="_blank" :href="'https://news.ycombinator.com/user?id=' + item.author" |
| 86 | + v-text="item.author"></a> |
| 87 | + | |
| 88 | + <a target="_blank" :href="'https://news.ycombinator.com/item?id=' + item.objectID" |
| 89 | + v-text="item.num_comments + ' comments'"></a> |
| 90 | + </small> |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + <button class="btn-load-more" v-show="distance < 0" @click="manualLoad">Load more</button> |
| 94 | + <infinite-loading @infinite="infiniteHandler" ref="infiniteLoading"> |
| 95 | + <span slot="no-more"> |
| 96 | + There is no more Hacker News :( |
| 97 | + </span> |
| 98 | + </infinite-loading> |
| 99 | +</div></pre> |
| 100 | + <p> |
| 101 | + In the template, we add a button to load data manually, set <code>v-show</code> directive to control hide or show, and bind a click event listener for it. |
| 102 | + <span v-if="$parent.docVersion < 2 || $parent.docVersion >=2.2">Then set <code v-if="$parent.docVersion < 2">v-ref</code><code v-else>ref</code> directive and bind <code>distance</code> property for the <code>infinite-loading</code> component.</span> |
| 103 | + </p> |
| 104 | + <h4>Script</h4> |
| 105 | + <pre v-highlightjs v-if="$parent.docVersion >= 1 && $parent.docVersion < 2">import InfiniteLoading from 'vue-infinite-loading'; |
| 106 | + |
| 107 | +const api = 'http://hn.algolia.com/api/v1/search_by_date?tags=story'; |
| 108 | + |
| 109 | +export default { |
| 110 | + data() { |
| 111 | + return { |
| 112 | + list: [], |
| 113 | + distance: -Infinity, |
| 114 | + }; |
| 115 | + }, |
| 116 | + methods: { |
| 117 | + onInfinite() { |
| 118 | + this.$http.get(api, { |
| 119 | + params: { |
| 120 | + page: this.list.length / 20 + 1, |
| 121 | + }, |
| 122 | + }).then((res) => { |
| 123 | + if (res.data.hits.length) { |
| 124 | + this.list = this.list.concat(res.data.hits); |
| 125 | + this.$broadcast('$InfiniteLoading:loaded'); |
| 126 | + if (this.list.length / 20 === 10) { |
| 127 | + this.$broadcast('$InfiniteLoading:complete'); |
| 128 | + } else if (res.data.page % 3 === 0) { |
| 129 | + this.distance = -Infinity; |
| 130 | + } |
| 131 | + } else { |
| 132 | + this.$broadcast('$InfiniteLoading:complete'); |
| 133 | + } |
| 134 | + }); |
| 135 | + }, |
| 136 | + manualLoad() { |
| 137 | + this.distance = 100; |
| 138 | + this.$nextTick(() => { |
| 139 | + this.$refs.infiniteLoading.attemptLoad(); |
| 140 | + }); |
| 141 | + }, |
| 142 | + }, |
| 143 | + components: { |
| 144 | + InfiniteLoading, |
| 145 | + }, |
| 146 | +};</pre> |
| 147 | + <pre v-highlightjs v-if="$parent.docVersion >= 2 && $parent.docVersion < 2.2">import InfiniteLoading from 'vue-infinite-loading'; |
| 148 | +import axios from 'axios'; |
| 149 | + |
| 150 | +const api = 'http://hn.algolia.com/api/v1/search_by_date?tags=story'; |
| 151 | + |
| 152 | +export default { |
| 153 | + data() { |
| 154 | + return { |
| 155 | + list: [], |
| 156 | + distance: -Infinity, |
| 157 | + }; |
| 158 | + }, |
| 159 | + methods: { |
| 160 | + onInfinite() { |
| 161 | + axios.get(api, { |
| 162 | + params: { |
| 163 | + page: this.list.length / 20 + 1, |
| 164 | + }, |
| 165 | + }).then(({ data }) => { |
| 166 | + if (data.hits.length) { |
| 167 | + this.list = this.list.concat(data.hits); |
| 168 | + this.$refs.infiniteLoading.$emit('$InfiniteLoading:loaded'); |
| 169 | + if (this.list.length / 20 === 10) { |
| 170 | + this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete'); |
| 171 | + } else if (data.page % 3 === 0) { |
| 172 | + this.distance = -Infinity; |
| 173 | + } |
| 174 | + } else { |
| 175 | + this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete'); |
| 176 | + } |
| 177 | + }); |
| 178 | + }, |
| 179 | + manualLoad() { |
| 180 | + this.distance = 100; |
| 181 | + this.$nextTick(() => { |
| 182 | + this.$refs.infiniteLoading.attemptLoad(); |
| 183 | + }); |
| 184 | + }, |
| 185 | + }, |
| 186 | + components: { |
| 187 | + InfiniteLoading, |
| 188 | + }, |
| 189 | +};</pre> |
| 190 | + <pre v-highlightjs v-if="$parent.docVersion >= 2.2">import InfiniteLoading from 'vue-infinite-loading'; |
| 191 | +import axios from 'axios'; |
| 192 | + |
| 193 | +const api = 'http://hn.algolia.com/api/v1/search_by_date?tags=story'; |
| 194 | + |
| 195 | +export default { |
| 196 | + data() { |
| 197 | + return { |
| 198 | + list: [], |
| 199 | + distance: -Infinity, |
| 200 | + }; |
| 201 | + }, |
| 202 | + methods: { |
| 203 | + infiniteHandler($state) { |
| 204 | + axios.get(api, { |
| 205 | + params: { |
| 206 | + page: this.list.length / 20 + 1, |
| 207 | + }, |
| 208 | + }).then(({ data }) => { |
| 209 | + if (data.hits.length) { |
| 210 | + this.list = this.list.concat(data.hits); |
| 211 | + $state.loaded(); |
| 212 | + if (this.list.length / 20 === 10) { |
| 213 | + $state.complete(); |
| 214 | + } else if (data.page % 3 === 0) { |
| 215 | + this.distance = -Infinity; |
| 216 | + } |
| 217 | + } else { |
| 218 | + $state.complete(); |
| 219 | + } |
| 220 | + }); |
| 221 | + }, |
| 222 | + manualLoad() { |
| 223 | + this.distance = 100; |
| 224 | + this.$nextTick(() => { |
| 225 | + this.$refs.infiniteLoading.attemptLoad(); |
| 226 | + }); |
| 227 | + }, |
| 228 | + }, |
| 229 | + components: { |
| 230 | + InfiniteLoading, |
| 231 | + }, |
| 232 | +};</pre> |
| 233 | + <p> |
| 234 | + In the script, we set the <code>distance</code> to <code>-Infinite</code> by default, then we create a <code>manualLoad</code> function to change the <code>distance</code> to a positive value and trigger load manually, lastly, in the <code>onInfinite</code> function, we reset the <code>distance</code> to <code>-Infinite</code> every three pages. |
| 235 | + </p> |
| 236 | + <p>That's all, you can preview it on the right, hope it is useful!</p> |
| 237 | +</template> |
0 commit comments