2018-06-16 13:30:41 +00:00
|
|
|
<cn>
|
|
|
|
#### 滚动加载
|
|
|
|
结合 [vue-infinite-scroll](https://github.com/ElemeFE/vue-infinite-scroll) 实现滚动自动加载列表。
|
|
|
|
</cn>
|
|
|
|
|
|
|
|
<us>
|
|
|
|
#### Scrolling loaded
|
|
|
|
The example of infinite load with [vue-infinite-scroll](https://github.com/ElemeFE/vue-infinite-scroll).
|
|
|
|
</us>
|
|
|
|
|
|
|
|
```html
|
|
|
|
<template>
|
2018-09-05 13:28:54 +00:00
|
|
|
<div
|
|
|
|
class="demo-infinite-container"
|
|
|
|
v-infinite-scroll="handleInfiniteOnLoad"
|
2018-06-16 13:30:41 +00:00
|
|
|
:infinite-scroll-disabled="busy"
|
|
|
|
:infinite-scroll-distance="10"
|
|
|
|
>
|
|
|
|
<a-list
|
|
|
|
:dataSource="data"
|
|
|
|
>
|
|
|
|
<a-list-item slot="renderItem" slot-scope="item, index">
|
|
|
|
<a-list-item-meta :description="item.email">
|
|
|
|
<a slot="title" :href="item.href">{{item.name.last}}</a>
|
|
|
|
<a-avatar slot="avatar" src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
|
|
|
|
</a-list-item-meta>
|
|
|
|
<div>Content</div>
|
|
|
|
</a-list-item>
|
2018-09-05 13:28:54 +00:00
|
|
|
<div v-if="loading && !busy" class="demo-loading-container">
|
|
|
|
<a-spin />
|
|
|
|
</div>
|
2018-06-16 13:30:41 +00:00
|
|
|
</a-list>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
import reqwest from 'reqwest'
|
|
|
|
import infiniteScroll from 'vue-infinite-scroll'
|
|
|
|
const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo'
|
|
|
|
export default {
|
|
|
|
directives: { infiniteScroll },
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
data: [],
|
|
|
|
loading: false,
|
|
|
|
busy: false,
|
|
|
|
}
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
beforeMount () {
|
2019-01-05 02:53:40 +00:00
|
|
|
this.fetchData((res) => {
|
2018-06-16 13:30:41 +00:00
|
|
|
this.data = res.results
|
|
|
|
})
|
|
|
|
},
|
|
|
|
methods: {
|
2019-01-05 02:53:40 +00:00
|
|
|
fetchData (callback) {
|
2018-06-16 13:30:41 +00:00
|
|
|
reqwest({
|
|
|
|
url: fakeDataUrl,
|
|
|
|
type: 'json',
|
|
|
|
method: 'get',
|
|
|
|
contentType: 'application/json',
|
|
|
|
success: (res) => {
|
|
|
|
callback(res)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
handleInfiniteOnLoad () {
|
|
|
|
const data = this.data
|
|
|
|
this.loading = true
|
|
|
|
if (data.length > 14) {
|
|
|
|
this.$message.warning('Infinite List loaded all')
|
|
|
|
this.busy = true
|
|
|
|
this.loading = false
|
|
|
|
return
|
|
|
|
}
|
2019-01-10 13:43:01 +00:00
|
|
|
this.fetchData((res) => {
|
2018-06-16 13:30:41 +00:00
|
|
|
this.data = data.concat(res.results)
|
|
|
|
this.loading = false
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
<style>
|
|
|
|
.demo-infinite-container {
|
|
|
|
border: 1px solid #e8e8e8;
|
|
|
|
border-radius: 4px;
|
|
|
|
overflow: auto;
|
|
|
|
padding: 8px 24px;
|
|
|
|
height: 300px;
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
.demo-loading-container {
|
2018-06-16 13:30:41 +00:00
|
|
|
position: absolute;
|
|
|
|
bottom: 40px;
|
|
|
|
width: 100%;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
```
|