列表

通用列表。

何时使用

最基础的列表展示,可承载文字、列表、图片、段落,常用于后台数据展示页面。

代码演示

List列表 - 图1

基础列表

基础列表。

  1. <template>
  2. <a-list
  3. itemLayout="horizontal"
  4. :dataSource="data"
  5. >
  6. <a-list-item slot="renderItem" slot-scope="item, index">
  7. <a-list-item-meta
  8. description="Ant Design, a design language for background applications, is refined by Ant UED Team"
  9. >
  10. <a slot="title" href="https://vue.ant.design/">{{item.title}}</a>
  11. <a-avatar slot="avatar" src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
  12. </a-list-item-meta>
  13. </a-list-item>
  14. </a-list>
  15. </template>
  16. <script>
  17. const data = [
  18. {
  19. title: 'Ant Design Title 1',
  20. },
  21. {
  22. title: 'Ant Design Title 2',
  23. },
  24. {
  25. title: 'Ant Design Title 3',
  26. },
  27. {
  28. title: 'Ant Design Title 4',
  29. },
  30. ]
  31. export default {
  32. data () {
  33. return {
  34. data,
  35. }
  36. },
  37. }
  38. </script>
  39. <style>
  40. </style>

List列表 - 图2

栅格列表

可以通过设置 Listgrid 属性来实现栅格列表,column 可设置期望显示的列数。

  1. <template>
  2. <a-list
  3. :grid="{ gutter: 16, column: 4 }"
  4. :dataSource="data"
  5. >
  6. <a-list-item slot="renderItem" slot-scope="item, index">
  7. <a-card :title="item.title">Card content</a-card>
  8. </a-list-item>
  9. </a-list>
  10. </template>
  11. <script>
  12. const data = [
  13. {
  14. title: 'Title 1',
  15. },
  16. {
  17. title: 'Title 2',
  18. },
  19. {
  20. title: 'Title 3',
  21. },
  22. {
  23. title: 'Title 4',
  24. },
  25. ]
  26. export default {
  27. data () {
  28. return {
  29. data,
  30. }
  31. },
  32. }
  33. </script>
  34. <style>
  35. </style>

List列表 - 图3

加载更多

可通过 loadMore 属性实现加载更多功能。

  1. <template>
  2. <a-list
  3. class="demo-loadmore-list"
  4. :loading="loading"
  5. itemLayout="horizontal"
  6. :dataSource="data"
  7. >
  8. <div v-if="showLoadingMore" slot="loadMore" :style="{ textAlign: 'center', marginTop: '12px', height: '32px', lineHeight: '32px' }">
  9. <a-spin v-if="loadingMore" />
  10. <a-button v-else @click="onLoadMore">loading more</a-button>
  11. </div>
  12. <a-list-item slot="renderItem" slot-scope="item, index">
  13. <a slot="actions">edit</a>
  14. <a slot="actions">more</a>
  15. <a-list-item-meta
  16. description="Ant Design, a design language for background applications, is refined by Ant UED Team"
  17. >
  18. <a slot="title" href="https://vue.ant.design/">{{item.name.last}}</a>
  19. <a-avatar slot="avatar" src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
  20. </a-list-item-meta>
  21. <div>content</div>
  22. </a-list-item>
  23. </a-list>
  24. </template>
  25. <script>
  26. import reqwest from 'reqwest'
  27. const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo'
  28. export default {
  29. data () {
  30. return {
  31. loading: true,
  32. loadingMore: false,
  33. showLoadingMore: true,
  34. data: [],
  35. }
  36. },
  37. mounted () {
  38. this.getData((res) => {
  39. this.loading = false
  40. this.data = res.results
  41. })
  42. },
  43. methods: {
  44. getData (callback) {
  45. reqwest({
  46. url: fakeDataUrl,
  47. type: 'json',
  48. method: 'get',
  49. contentType: 'application/json',
  50. success: (res) => {
  51. callback(res)
  52. },
  53. })
  54. },
  55. onLoadMore () {
  56. this.loadingMore = true
  57. this.getData((res) => {
  58. this.data = this.data.concat(res.results)
  59. this.loadingMore = false
  60. this.$nextTick(() => {
  61. window.dispatchEvent(new Event('resize'))
  62. })
  63. })
  64. },
  65. },
  66. }
  67. </script>
  68. <style>
  69. .demo-loadmore-list {
  70. min-height: 350px;
  71. }
  72. </style>

List列表 - 图4

响应式的栅格列表

响应式的栅格列表。尺寸与 Layout Grid 保持一致。

  1. <template>
  2. <a-list
  3. :grid="{ gutter: 16, xs: 1, sm: 2, md: 4, lg: 4, xl: 6, xxl: 3 }"
  4. :dataSource="data"
  5. >
  6. <a-list-item slot="renderItem" slot-scope="item, index">
  7. <a-card :title="item.title">Card content</a-card>
  8. </a-list-item>
  9. </a-list>
  10. </template>
  11. <script>
  12. const data = [
  13. {
  14. title: 'Title 1',
  15. },
  16. {
  17. title: 'Title 2',
  18. },
  19. {
  20. title: 'Title 3',
  21. },
  22. {
  23. title: 'Title 4',
  24. },
  25. {
  26. title: 'Title 5',
  27. },
  28. {
  29. title: 'Title 6',
  30. },
  31. ]
  32. export default {
  33. data () {
  34. return {
  35. data,
  36. }
  37. },
  38. }
  39. </script>
  40. <style>
  41. </style>

List列表 - 图5

简单列表

列表拥有大、中、小三种尺寸。通过设置 sizelarge small 分别把按钮设为大、小尺寸。若不设置 size,则尺寸为中。可通过设置 headerfooter,来自定义列表头部和尾部。

  1. <template>
  2. <div>
  3. <h3 :style="{ marginBottom: '16px' }">Default Size</h3>
  4. <a-list
  5. bordered
  6. :dataSource="data"
  7. >
  8. <a-list-item slot="renderItem" slot-scope="item, index">{{item}}</a-list-item>
  9. <div slot="header">Header</div>
  10. <div slot="footer">Footer</div>
  11. </a-list>
  12. <h3 :style="{ margin: '16px 0' }">Small Size</h3>
  13. <a-list
  14. size="small"
  15. bordered
  16. :dataSource="data"
  17. >
  18. <a-list-item slot="renderItem" slot-scope="item, index">{{item}}</a-list-item>
  19. <div slot="header">Header</div>
  20. <div slot="footer">Footer</div>
  21. </a-list>
  22. <h3 :style="{ margin: '16px 0' }">Large Size</h3>
  23. <a-list
  24. size="large"
  25. bordered
  26. :dataSource="data"
  27. >
  28. <a-list-item slot="renderItem" slot-scope="item, index">{{item}}</a-list-item>
  29. <div slot="header">Header</div>
  30. <div slot="footer">Footer</div>
  31. </a-list>
  32. </div>
  33. </template>
  34. <script>
  35. const data = [
  36. 'Racing car sprays burning fuel into crowd.',
  37. 'Japanese princess to wed commoner.',
  38. 'Australian walks 100km after outback crash.',
  39. 'Man charged over missing wedding girl.',
  40. 'Los Angeles battles huge wildfires.',
  41. ]
  42. export default {
  43. data () {
  44. return {
  45. data,
  46. }
  47. },
  48. }
  49. </script>
  50. <style>
  51. </style>

List列表 - 图6

竖排列表样式

通过设置 itemLayout 属性为 vertical 可实现竖排列表样式。

  1. <template>
  2. <a-list
  3. itemLayout="vertical"
  4. size="large"
  5. :pagination="pagination"
  6. :dataSource="listData"
  7. >
  8. <div slot="footer"><b>ant design vue</b> footer part</div>
  9. <a-list-item slot="renderItem" slot-scope="item, index" key="item.title">
  10. <template slot="actions" v-for="{type, text} in actions">
  11. <span :key="type">
  12. <a-icon :type="type" style="margin-right: 8px" />
  13. {{text}}
  14. </span>
  15. </template>
  16. <img slot="extra" width="272" alt="logo" src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png" />
  17. <a-list-item-meta
  18. :description="item.description"
  19. >
  20. <a slot="title" :href="item.href">{{item.title}}</a>
  21. <a-avatar slot="avatar" :src="item.avatar" />
  22. </a-list-item-meta>
  23. {{item.content}}
  24. </a-list-item>
  25. </a-list>
  26. </template>
  27. <script>
  28. const listData = []
  29. for (let i = 0; i < 23; i++) {
  30. listData.push({
  31. href: 'https://vue.ant.design/',
  32. title: `ant design vue part ${i}`,
  33. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  34. description: 'Ant Design, a design language for background applications, is refined by Ant UED Team.',
  35. content: 'We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  36. })
  37. }
  38. export default {
  39. data () {
  40. return {
  41. listData,
  42. pagination: {
  43. onChange: (page) => {
  44. console.log(page);
  45. },
  46. pageSize: 3,
  47. },
  48. actions: [
  49. { type: 'star-o', text: '156' },
  50. { type: 'like-o', text: '156' },
  51. { type: 'message', text: '2' },
  52. ],
  53. }
  54. },
  55. }
  56. </script>
  57. <style>
  58. </style>

List列表 - 图7

滚动加载

结合 vue-infinite-scroll 实现滚动自动加载列表。

  1. <template>
  2. <div
  3. class="demo-infinite-container"
  4. v-infinite-scroll="handleInfiniteOnLoad"
  5. :infinite-scroll-disabled="busy"
  6. :infinite-scroll-distance="10"
  7. >
  8. <a-list
  9. :dataSource="data"
  10. >
  11. <a-list-item slot="renderItem" slot-scope="item, index">
  12. <a-list-item-meta :description="item.email">
  13. <a slot="title" :href="item.href">{{item.name.last}}</a>
  14. <a-avatar slot="avatar" src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
  15. </a-list-item-meta>
  16. <div>Content</div>
  17. </a-list-item>
  18. <div v-if="loading && !busy" class="demo-loading-container">
  19. <a-spin />
  20. </div>
  21. </a-list>
  22. </div>
  23. </template>
  24. <script>
  25. import reqwest from 'reqwest'
  26. import infiniteScroll from 'vue-infinite-scroll'
  27. const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo'
  28. export default {
  29. directives: { infiniteScroll },
  30. data () {
  31. return {
  32. data: [],
  33. loading: false,
  34. busy: false,
  35. }
  36. },
  37. beforeMount () {
  38. this.fetchData((res) => {
  39. this.data = res.results
  40. })
  41. },
  42. methods: {
  43. fetchData (callback) {
  44. reqwest({
  45. url: fakeDataUrl,
  46. type: 'json',
  47. method: 'get',
  48. contentType: 'application/json',
  49. success: (res) => {
  50. callback(res)
  51. },
  52. })
  53. },
  54. handleInfiniteOnLoad () {
  55. const data = this.data
  56. this.loading = true
  57. if (data.length > 14) {
  58. this.$message.warning('Infinite List loaded all')
  59. this.busy = true
  60. this.loading = false
  61. return
  62. }
  63. this.fetchData((res) => {
  64. this.data = data.concat(res.results)
  65. this.loading = false
  66. })
  67. },
  68. },
  69. }
  70. </script>
  71. <style>
  72. .demo-infinite-container {
  73. border: 1px solid #e8e8e8;
  74. border-radius: 4px;
  75. overflow: auto;
  76. padding: 8px 24px;
  77. height: 300px;
  78. }
  79. .demo-loading-container {
  80. position: absolute;
  81. bottom: 40px;
  82. width: 100%;
  83. text-align: center;
  84. }
  85. </style>

List列表 - 图8

滚动加载无限长列表

结合 vue-virtual-scroller 实现滚动加载无限长列表,带有虚拟化(virtualization)功能,能够提高数据量大时候长列表的性能。可以结合 vue-infinite-scroll 实现滚动自动加载无限长列表。 virtualized 是在大数据列表中应用的一种技术,主要是为了减少不可见区域不必要的渲染从而提高性能,特别是数据量在成千上万条效果尤为明显。

  1. <template>
  2. <a-list>
  3. <virtual-scroller
  4. style="height: 400px"
  5. :items="data"
  6. item-height="73"
  7. v-infinite-scroll="handleInfiniteOnLoad"
  8. :infinite-scroll-disabled="busy"
  9. :infinite-scroll-distance="10"
  10. >
  11. <a-list-item slot-scope="{item}">
  12. <a-list-item-meta :description="item.email">
  13. <a slot="title" :href="item.href">{{item.name.last}}</a>
  14. <a-avatar slot="avatar" src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
  15. </a-list-item-meta>
  16. <div>Content {{item.index}}</div>
  17. </a-list-item>
  18. </virtual-scroller>
  19. <a-spin v-if="loading" class="demo-loading" />
  20. </a-list>
  21. </template>
  22. <script>
  23. import reqwest from 'reqwest'
  24. import infiniteScroll from 'vue-infinite-scroll'
  25. import { VirtualScroller } from 'vue-virtual-scroller'
  26. const fakeDataUrl = 'https://randomuser.me/api/?results=10&inc=name,gender,email,nat&noinfo'
  27. export default {
  28. directives: { infiniteScroll },
  29. data () {
  30. return {
  31. data: [],
  32. loading: false,
  33. busy: false,
  34. }
  35. },
  36. beforeMount () {
  37. this.fetchData((res) => {
  38. this.data = res.results.map((item, index) => ({ ...item, index }))
  39. })
  40. },
  41. methods: {
  42. fetchData (callback) {
  43. reqwest({
  44. url: fakeDataUrl,
  45. type: 'json',
  46. method: 'get',
  47. contentType: 'application/json',
  48. success: (res) => {
  49. callback(res)
  50. },
  51. })
  52. },
  53. handleInfiniteOnLoad () {
  54. const data = this.data
  55. this.loading = true
  56. if (data.length > 100) {
  57. this.$message.warning('Infinite List loaded all')
  58. this.busy = true
  59. this.loading = false
  60. return
  61. }
  62. this.fetchData((res) => {
  63. this.data = data.concat(res.results).map((item, index) => ({ ...item, index }))
  64. this.loading = false
  65. })
  66. },
  67. },
  68. components: {
  69. 'virtual-scroller': VirtualScroller,
  70. },
  71. }
  72. </script>
  73. <style>
  74. .demo-loading {
  75. position: absolute;
  76. bottom: 40px;
  77. width: 100%;
  78. text-align: center;
  79. }
  80. </style>

API

List

参数说明类型默认值
bordered是否展示边框booleanfalse
footer列表底部string|slot-
grid列表栅格配置object-
header列表头部string|slot-
itemLayout设置 List.Item 布局, 设置成 vertical 则竖直样式显示, 默认横排string-
loading当卡片内容还在加载中时,可以用 loading 展示一个占位boolean|objectfalse
loadMore加载更多string|slot-
locale默认文案设置,目前包括空数据文案objectemptyText: '暂无数据'
pagination对应的 pagination 配置, 设置 false 不显示boolean|objectfalse
sizelist 的尺寸default | middle | smalldefault
split是否展示分割线booleantrue
renderItem自定义Item函数,也可使用slot="renderItem" 和 slot-scope="item, index"(item, index) => vNode
rowKey各项 key 的取值,可以是字符串或一个函数item => string|number

pagination

分页的配置项。

参数说明类型默认值
position指定分页显示的位置'top' | 'bottom' | 'both''bottom'

更多配置项,请查看 Pagination

List grid props

参数说明类型默认值
column列数number oneOf [ 1, 2, 3, 4, 6, 8, 12, 24]-
gutter栅格间隔number0
xs<576px 展示的列数number-
sm≥576px 展示的列数number-
md≥768px 展示的列数number-
lg≥992px 展示的列数number-
xl≥1200px 展示的列数number-
xxl≥1600px 展示的列数number-

List.Item

参数说明类型默认值
actions列表操作组,根据 itemLayout 的不同, 位置在卡片底部或者最右侧Array<vNode>/slot
extra额外内容, 通常用在 itemLayoutvertical 的情况下, 展示右侧内容; horizontal 展示在列表元素最右侧string|slot-

List.Item.Meta

参数说明类型默认值
avatar列表元素的图标slot-
description列表元素的描述内容string|slot-
title列表元素的标题string|slot-