List 列表
适用于加载更多数据,支持上下滚动加载数据
基本使用
使用load
来加载数据,使用finished
属性来终止加载
使用load
函数需要返回promise
实例,且promise
的状态决定是loading
还是error
,使用finished
属性来决定已全部加载完毕的状态。
Row 0
你是自己故事的作者。Row 1
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
Row 2
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
Row 3
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
积极面对,挑战不难。
Row 4
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
积极面对,挑战不难。
你的未来取决于你现在的选择。
Row 5
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
Row 6
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
Row 7
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
Row 8
你是自己故事的作者。你的生活是你的作品。
Row 9
你是自己故事的作者。你的生活是你的作品。
vue
<template>
<el-list
class="list-load"
:list="list"
style="height: 500px"
:load="handleLoad"
:finished="finished"
>
<template #default="{ index, data }">
<div class="item-content">
<div>Row {{ index }}</div>
<el-text v-html="data.text" />
</div>
</template>
</el-list>
</template>
<script setup>
import { ref } from 'vue'
const textArray = [
'你是自己故事的作者。<br>',
'你的生活是你的作品。<br>',
'挑战是成长的阶梯。<br>',
'积极面对,挑战不难。<br>',
'你的未来取决于你现在的选择。<br>',
]
const generateArray = () =>
Array.from(Array.from({ length: 10 }).keys()).map((i) => ({
text: textArray
.slice(0, Math.ceil(Math.random() * textArray.length))
.join(''),
}))
const list = ref(generateArray())
const finished = ref(false)
const handleLoad = () => {
console.log('load more')
return new Promise((resolve, reject) => {
setTimeout(() => {
const success = Math.random() > 0.5
if (success) {
list.value.push(...generateArray())
if (list.value.length >= 30) {
finished.value = true
}
}
return success ? resolve() : reject()
}, 1000 * 1)
})
}
</script>
<style lang="scss" scoped>
.item-content {
box-sizing: border-box;
padding: 6px 12px;
margin: 8px;
border-radius: 4px;
border: 1px solid #e4e7ed;
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.12);
}
.text {
text-align: center;
margin: 6px;
}
</style>
隐藏源代码
向上滚动加载
使用pre-load
来加载顶部数据
Row 9 -
Row 8 -
Row 7 - Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!
Row 6 - Good morning, how are you?Good morning, how are you?Good morning, how are you?
Row 5 - Hi, good morning, I'm fine!Hi, good morning, I'm fine!
Row 4 -
Row 3 -
Row 2 - Good morning, how are you?Good morning, how are you?Good morning, how are you?
Row 1 - Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!Hi, good morning, I'm fine!
Row 0 - Good morning, how are you?Good morning, how are you?Good morning, how are you?Good morning, how are you?Good morning, how are you?Good morning, how are you?
vue
<template>
<el-list
ref="listRef"
class="list-load"
:list="list"
style="height: 680px"
:pre-load="handlePreLoad"
:finished="finished"
no-more-text="没有更多聊天内容啦"
>
<template #default="{ data }">
<div class="item-content">
<el-bubble
:placement="data.placement"
:content="data.content"
:content-style="data.contentStyle"
:avatar="{ style: data.avatar }"
/>
</div>
</template>
</el-list>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
const listRef = ref()
const generateArray = (len = 0) =>
Array.from(Array.from({ length: 10 }).keys()).map((i) => {
const index = len + i
return {
placement: index % 2 === 0 ? 'start' : 'end',
content:
index % 2 === 0
? `Row ${index} - ${`Good morning, how are you?`.repeat(
Math.floor(Math.random() * 10)
)}`
: `Row ${index} - ${`Hi, good morning, I'm fine!`.repeat(
Math.floor(Math.random() * 10)
)}`,
avatar:
index % 2 === 0
? {
color: '#f56a00',
backgroundColor: '#fde3cf',
}
: {
color: '#fff',
backgroundColor: '#87d068',
},
contentStyle: { maxWidth: '500px' },
}
})
const list = ref([...generateArray().reverse()])
const finished = ref(false)
const handlePreLoad = () => {
console.log('load more')
return new Promise((resolve, reject) => {
setTimeout(() => {
const success = Math.random() > 0.5
if (success) {
list.value.unshift(...generateArray(list.value.length).reverse())
if (list.value.length >= 50) {
finished.value = true
}
}
return success ? resolve(list.value) : reject()
}, 1000 * 1)
})
}
onMounted(() => {
listRef.value.scrollToBottom()
})
</script>
<style lang="scss" scoped>
.item-content {
box-sizing: border-box;
padding: 6px 12px;
margin: 8px;
}
.text {
text-align: center;
margin: 6px;
}
</style>
隐藏源代码
原生滚动条
使用native-scrollbar
属性切换原生滚动条
Row 0
你是自己故事的作者。你的生活是你的作品。
Row 1
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
Row 2
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
Row 3
你是自己故事的作者。Row 4
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
Row 5
你是自己故事的作者。Row 6
你是自己故事的作者。你的生活是你的作品。
Row 7
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
Row 8
你是自己故事的作者。Row 9
你是自己故事的作者。你的生活是你的作品。
挑战是成长的阶梯。
vue
<template>
<el-list
class="list-load"
:list="list"
style="height: 500px"
:load="handleLoad"
:finished="finished"
native-scrollbar
>
<template #default="{ index, data }">
<div class="item-content">
<div>Row {{ index }}</div>
<el-text v-html="data.text" />
</div>
</template>
</el-list>
</template>
<script setup>
import { ref } from 'vue'
const textArray = [
'你是自己故事的作者。<br>',
'你的生活是你的作品。<br>',
'挑战是成长的阶梯。<br>',
'积极面对,挑战不难。<br>',
'你的未来取决于你现在的选择。<br>',
]
const generateArray = () =>
Array.from(Array.from({ length: 10 }).keys()).map((i) => ({
text: textArray
.slice(0, Math.ceil(Math.random() * textArray.length))
.join(''),
}))
const list = ref(generateArray())
const finished = ref(false)
const handleLoad = () => {
console.log('load more')
return new Promise((resolve, reject) => {
setTimeout(() => {
const success = Math.random() > 0.5
if (success) {
list.value.push(...generateArray())
if (list.value.length >= 30) {
finished.value = true
}
}
return success ? resolve() : reject()
}, 1000 * 1)
})
}
</script>
<style lang="scss" scoped>
.item-content {
box-sizing: border-box;
padding: 6px 12px;
margin: 8px;
border-radius: 4px;
border: 1px solid #e4e7ed;
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.12);
}
.text {
text-align: center;
margin: 6px;
}
</style>
隐藏源代码
API
属性
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
list | 列表项 | Array | - |
native-scrollbar | 是否使用原生滚动条 | Boolean | false |
infinite-scroll-delay | 节流时延,单位为 ms | Number | 16 |
infinite-scroll-distance | 触发加载的距离阈值,单位为 px | Number | 20 |
load | 内容到达底部时触发加载的函数 | Function | - |
pre-load | 内容到达顶部时触发加载的函数 | Function | - |
finished | 配合 load 使用,当数据加载完毕时,可以设置 | Boolean | false |
loading-text | 加载时的文案 | String | 加载中... |
error-text | 加载异常的文案 | String | 请求失败,点击重新加载 |
no-more-text | 加载完的文案 | String | 没有更多了 |
Events
事件名 | 说明 | 类型 |
---|---|---|
scroll | 容器滚动时触发 | Function |
Slots
插槽名 | 说明 |
---|---|
default | 列表项内容 |
Exposes
名称 | 详情 | 类型 |
---|---|---|
scrollToBottom | 滚动到底部 | Function |
target | 滚动容器 | Object |