列表

通用列表。

何时使用

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

代码演示

List列表 - 图1

基础列表

基础列表。

  1. <template>
  2. <a-list item-layout="horizontal" :data-source="data">
  3. <a-list-item slot="renderItem" slot-scope="item, index">
  4. <a-list-item-meta
  5. description="Ant Design, a design language for background applications, is refined by Ant UED Team"
  6. >
  7. <a slot="title" href="https://www.antdv.com/">{{ item.title }}</a>
  8. <a-avatar
  9. slot="avatar"
  10. src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  11. />
  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></style>

List列表 - 图2

栅格列表

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

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

List列表 - 图3

加载更多

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

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

List列表 - 图4

响应式的栅格列表

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

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

List列表 - 图5

简单列表

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

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

List列表 - 图6

竖排列表样式

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

  1. <template>
  2. <a-list item-layout="vertical" size="large" :pagination="pagination" :data-source="listData">
  3. <div slot="footer"><b>ant design vue</b> footer part</div>
  4. <a-list-item slot="renderItem" key="item.title" slot-scope="item, index">
  5. <template v-for="{ type, text } in actions" slot="actions">
  6. <span :key="type">
  7. <a-icon :type="type" style="margin-right: 8px" />
  8. {{ text }}
  9. </span>
  10. </template>
  11. <img
  12. slot="extra"
  13. width="272"
  14. alt="logo"
  15. src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png"
  16. />
  17. <a-list-item-meta :description="item.description">
  18. <a slot="title" :href="item.href">{{ item.title }}</a>
  19. <a-avatar slot="avatar" :src="item.avatar" />
  20. </a-list-item-meta>
  21. {{ item.content }}
  22. </a-list-item>
  23. </a-list>
  24. </template>
  25. <script>
  26. const listData = [];
  27. for (let i = 0; i < 23; i++) {
  28. listData.push({
  29. href: 'https://www.antdv.com/',
  30. title: `ant design vue part ${i}`,
  31. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  32. description:
  33. 'Ant Design, a design language for background applications, is refined by Ant UED Team.',
  34. content:
  35. '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></style>

List列表 - 图7

滚动加载

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

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

List列表 - 图8

滚动加载无限长列表

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

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