List列表

通用列表。

何时使用

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

单独引入此组件

想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

  1. import { NzListModule } from 'ng-zorro-antd/list';

代码演示

List列表 - 图1

简单列表

列表拥有大、中、小三种尺寸。

通过设置 sizelargesmall 分别把按钮设为大、小尺寸。若不设置 size,则尺寸为中。

可通过设置 nzHeadernzFooter,来自定义列表头部和尾部。

  1. import { Component } from '@angular/core';
  2. import { NzMessageService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-list-simple',
  5. template: `
  6. <h3 [ngStyle]="{ 'margin-bottom.px': 16 }">Default Size</h3>
  7. <nz-list [nzDataSource]="data" nzBordered [nzHeader]="'Header'" [nzFooter]="'Footer'" [nzRenderItem]="defaultItem">
  8. <ng-template #defaultItem let-item>
  9. <nz-list-item>
  10. <span class="ant-typography"><mark>[ITEM]</mark></span>
  11. {{ item }}
  12. </nz-list-item>
  13. </ng-template>
  14. </nz-list>
  15. <h3 [ngStyle]="{ margin: '16px 0' }">Small Size</h3>
  16. <nz-list
  17. [nzDataSource]="data"
  18. nzBordered
  19. nzSize="small"
  20. [nzHeader]="'Header'"
  21. [nzFooter]="'Footer'"
  22. [nzRenderItem]="smallItem"
  23. >
  24. <ng-template #smallItem let-item><nz-list-item [nzContent]="item"></nz-list-item></ng-template>
  25. </nz-list>
  26. <h3 [ngStyle]="{ margin: '16px 0' }">Large Size</h3>
  27. <ul
  28. nz-list
  29. [nzDataSource]="data"
  30. nzBordered
  31. nzSize="large"
  32. [nzHeader]="'Header'"
  33. [nzFooter]="'Footer'"
  34. [nzRenderItem]="largeItem"
  35. >
  36. <ng-template #largeItem let-item>
  37. <li nz-list-item [nzActions]="[opAction]" [nzContent]="item" nzNoFlex></li>
  38. <ng-template #opAction><a (click)="msg.info('edit')">edit</a></ng-template>
  39. </ng-template>
  40. </ul>
  41. `
  42. })
  43. export class NzDemoListSimpleComponent {
  44. data = [
  45. 'Racing car sprays burning fuel into crowd.',
  46. 'Japanese princess to wed commoner.',
  47. 'Australian walks 100km after outback crash.',
  48. 'Man charged over missing wedding girl.',
  49. 'Los Angeles battles huge wildfires.'
  50. ];
  51. constructor(public msg: NzMessageService) {}
  52. }

List列表 - 图2

基础列表

基础列表。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-list-basic',
  4. template: `
  5. <div style="margin-bottom: 8px;"><button nz-button (click)="change()">Switch Data</button></div>
  6. <nz-list [nzDataSource]="data" [nzRenderItem]="item" [nzItemLayout]="'horizontal'" [nzLoading]="loading">
  7. <ng-template #item let-item>
  8. <nz-list-item>
  9. <nz-list-item-meta
  10. [nzTitle]="nzTitle"
  11. nzAvatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  12. nzDescription="Ant Design, a design language for background applications, is refined by Ant UED Team"
  13. >
  14. <ng-template #nzTitle>
  15. <a href="https://ng.ant.design">{{ item.title }}</a>
  16. </ng-template>
  17. </nz-list-item-meta>
  18. </nz-list-item>
  19. </ng-template>
  20. </nz-list>
  21. `
  22. })
  23. export class NzDemoListBasicComponent {
  24. loading = false;
  25. data = [
  26. {
  27. title: 'Ant Design Title 1'
  28. },
  29. {
  30. title: 'Ant Design Title 2'
  31. },
  32. {
  33. title: 'Ant Design Title 3'
  34. },
  35. {
  36. title: 'Ant Design Title 4'
  37. }
  38. ];
  39. change(): void {
  40. this.loading = true;
  41. if (this.data.length > 0) {
  42. setTimeout(() => {
  43. this.data = [];
  44. this.loading = false;
  45. }, 1000);
  46. } else {
  47. setTimeout(() => {
  48. this.data = [
  49. {
  50. title: 'Ant Design Title 1'
  51. },
  52. {
  53. title: 'Ant Design Title 2'
  54. },
  55. {
  56. title: 'Ant Design Title 3'
  57. },
  58. {
  59. title: 'Ant Design Title 4'
  60. }
  61. ];
  62. this.loading = false;
  63. }, 1000);
  64. }
  65. }
  66. }

List列表 - 图3

加载更多

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

  1. // tslint:disable:no-any
  2. import { HttpClient } from '@angular/common/http';
  3. import { Component, OnInit } from '@angular/core';
  4. import { NzMessageService } from 'ng-zorro-antd';
  5. const count = 5;
  6. const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
  7. @Component({
  8. selector: 'nz-demo-list-loadmore',
  9. template: `
  10. <nz-list
  11. class="demo-loadmore-list"
  12. [nzDataSource]="list"
  13. [nzItemLayout]="'horizontal'"
  14. [nzLoading]="initLoading"
  15. [nzRenderItem]="item"
  16. [nzLoadMore]="loadMore"
  17. >
  18. <ng-template #item let-item>
  19. <nz-list-item
  20. [nzContent]="item.loading ? '' : 'content'"
  21. [nzActions]="item.loading ? [] : [editAction, moreAction]"
  22. >
  23. <nz-skeleton [nzAvatar]="true" [nzActive]="true" [nzTitle]="false" [nzLoading]="item.loading">
  24. <ng-template #editAction><a (click)="edit(item)">edit</a></ng-template>
  25. <ng-template #moreAction><a (click)="edit(item)">more</a></ng-template>
  26. <nz-list-item-meta
  27. [nzTitle]="nzTitle"
  28. nzAvatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  29. nzDescription="Ant Design, a design language for background applications, is refined by Ant UED Team"
  30. >
  31. <ng-template #nzTitle>
  32. <a href="https://ng.ant.design">{{ item.name.last }}</a>
  33. </ng-template>
  34. </nz-list-item-meta>
  35. </nz-skeleton>
  36. </nz-list-item>
  37. </ng-template>
  38. <ng-template #loadMore>
  39. <div class="loadmore">
  40. <button nz-button *ngIf="!loadingMore" (click)="onLoadMore()">loading more</button>
  41. </div>
  42. </ng-template>
  43. </nz-list>
  44. `,
  45. styles: [
  46. `
  47. .demo-loadmore-list {
  48. min-height: 350px;
  49. }
  50. .loadmore {
  51. text-align: center;
  52. margin-top: 12px;
  53. height: 32px;
  54. line-height: 32px;
  55. }
  56. `
  57. ]
  58. })
  59. export class NzDemoListLoadmoreComponent implements OnInit {
  60. initLoading = true; // bug
  61. loadingMore = false;
  62. data: any[] = [];
  63. list: Array<{ loading: boolean; name: any }> = [];
  64. constructor(private http: HttpClient, private msg: NzMessageService) {}
  65. ngOnInit(): void {
  66. this.getData((res: any) => {
  67. this.data = res.results;
  68. this.list = res.results;
  69. this.initLoading = false;
  70. });
  71. }
  72. getData(callback: (res: any) => void): void {
  73. this.http.get(fakeDataUrl).subscribe((res: any) => callback(res));
  74. }
  75. onLoadMore(): void {
  76. this.loadingMore = true;
  77. this.list = this.data.concat([...Array(count)].fill({}).map(() => ({ loading: true, name: {} })));
  78. this.http.get(fakeDataUrl).subscribe((res: any) => {
  79. this.data = this.data.concat(res.results);
  80. this.list = [...this.data];
  81. this.loadingMore = false;
  82. });
  83. }
  84. edit(item: any): void {
  85. this.msg.success(item.email);
  86. }
  87. }

List列表 - 图4

竖排列表样式

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

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-list-vertical',
  4. template: `
  5. <nz-list
  6. [nzDataSource]="data"
  7. [nzItemLayout]="'vertical'"
  8. [nzRenderItem]="item"
  9. [nzPagination]="pagination"
  10. [nzFooter]="footer"
  11. >
  12. <ng-template #item let-item>
  13. <nz-list-item [nzContent]="item.content" [nzActions]="[starAction, likeAction, msgAction]" [nzExtra]="extra">
  14. <ng-template #starAction><i nz-icon nzType="star-o" style="margin-right: 8px;"></i> 156</ng-template>
  15. <ng-template #likeAction><i nz-icon nzType="like-o" style="margin-right: 8px;"></i> 156</ng-template>
  16. <ng-template #msgAction><i nz-icon nzType="message" style="margin-right: 8px;"></i> 2</ng-template>
  17. <nz-list-item-meta [nzAvatar]="item.avatar" [nzTitle]="nzTitle" [nzDescription]="item.description">
  18. <ng-template #nzTitle
  19. ><a href="{{ item.href }}">{{ item.title }}</a></ng-template
  20. >
  21. </nz-list-item-meta>
  22. <ng-template #extra>
  23. <img width="272" alt="logo" src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png" />
  24. </ng-template>
  25. </nz-list-item>
  26. </ng-template>
  27. <ng-template #footer>
  28. <div><b>ant design</b> footer part</div>
  29. </ng-template>
  30. <ng-template #pagination>
  31. <nz-pagination [nzPageIndex]="1" [nzTotal]="50" (nzPageIndexChange)="loadData($event)"></nz-pagination>
  32. </ng-template>
  33. </nz-list>
  34. `
  35. })
  36. export class NzDemoListVerticalComponent implements OnInit {
  37. // tslint:disable-next-line:no-any
  38. data: any[] = [];
  39. ngOnInit(): void {
  40. this.loadData(1);
  41. }
  42. loadData(pi: number): void {
  43. this.data = new Array(5).fill({}).map((_, index) => {
  44. return {
  45. href: 'http://ant.design',
  46. title: `ant design part ${index} (page: ${pi})`,
  47. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  48. description: 'Ant Design, a design language for background applications, is refined by Ant UED Team.',
  49. content:
  50. '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.'
  51. };
  52. });
  53. }
  54. }

List列表 - 图5

栅格列表

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

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-list-grid',
  4. template: `
  5. <nz-list [nzDataSource]="data" [nzRenderItem]="item" [nzGrid]="{ gutter: 16, span: 6 }">
  6. <ng-template #item let-item>
  7. <nz-list-item [nzContent]="nzContent">
  8. <ng-template #nzContent>
  9. <nz-card [nzTitle]="item.title">
  10. Card content
  11. </nz-card>
  12. </ng-template>
  13. </nz-list-item>
  14. </ng-template>
  15. </nz-list>
  16. `
  17. })
  18. export class NzDemoListGridComponent {
  19. data = [
  20. {
  21. title: 'Title 1'
  22. },
  23. {
  24. title: 'Title 2'
  25. },
  26. {
  27. title: 'Title 3'
  28. },
  29. {
  30. title: 'Title 4'
  31. }
  32. ];
  33. }

List列表 - 图6

响应式的栅格列表

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

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-list-resposive',
  4. template: `
  5. <nz-list [nzDataSource]="data" [nzRenderItem]="item" [nzGrid]="{ gutter: 16, xs: 24, sm: 12, md: 6, lg: 6, xl: 4 }">
  6. <ng-template #item let-item>
  7. <nz-list-item [nzContent]="nzContent">
  8. <ng-template #nzContent>
  9. <nz-card [nzTitle]="item.title">
  10. Card content
  11. </nz-card>
  12. </ng-template>
  13. </nz-list-item>
  14. </ng-template>
  15. </nz-list>
  16. `
  17. })
  18. export class NzDemoListResposiveComponent {
  19. data = [
  20. {
  21. title: 'Title 1'
  22. },
  23. {
  24. title: 'Title 2'
  25. },
  26. {
  27. title: 'Title 3'
  28. },
  29. {
  30. title: 'Title 4'
  31. },
  32. {
  33. title: 'Title 5'
  34. },
  35. {
  36. title: 'Title 6'
  37. }
  38. ];
  39. }

List列表 - 图7

滚动加载无限长列表

结合 cdk-virtual-scroll 实现滚动加载无限长列表,带有虚拟化 virtualization 功能,能够提高数据量大时候长列表的性能。

Virtualized 是在大数据列表中应用的一种技术,主要是为了减少不可见区域不必要的渲染从而提高性能,特别是数据量在成千上万条效果尤为明显。

  1. // tslint:disable:no-any
  2. import { CollectionViewer, DataSource } from '@angular/cdk/collections';
  3. import { HttpClient } from '@angular/common/http';
  4. import { ChangeDetectionStrategy, Component } from '@angular/core';
  5. import { BehaviorSubject, Observable, Subscription } from 'rxjs';
  6. @Component({
  7. selector: 'nz-demo-list-infinite-load',
  8. template: `
  9. <div>
  10. <cdk-virtual-scroll-viewport itemSize="73" class="demo-infinite-container">
  11. <nz-list>
  12. <nz-list-item *cdkVirtualFor="let item of ds">
  13. <nz-skeleton *ngIf="!item" [nzAvatar]="true" [nzParagraph]="{ rows: 1 }"></nz-skeleton>
  14. <nz-list-item-meta
  15. *ngIf="item"
  16. [nzTitle]="nzTitle"
  17. nzAvatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  18. [nzDescription]="item.email"
  19. >
  20. <ng-template #nzTitle>
  21. <a href="https://ng.ant.design">{{ item.name.last }}</a>
  22. </ng-template>
  23. </nz-list-item-meta>
  24. </nz-list-item>
  25. </nz-list>
  26. </cdk-virtual-scroll-viewport>
  27. </div>
  28. `,
  29. styles: [
  30. `
  31. .demo-infinite-container {
  32. height: 300px;
  33. border: 1px solid #e8e8e8;
  34. border-radius: 4px;
  35. }
  36. nz-list {
  37. padding: 24px;
  38. }
  39. `
  40. ],
  41. changeDetection: ChangeDetectionStrategy.OnPush
  42. })
  43. export class NzDemoListInfiniteLoadComponent {
  44. ds = new MyDataSource(this.http);
  45. constructor(private http: HttpClient) {}
  46. }
  47. class MyDataSource extends DataSource<string | undefined> {
  48. private length = 100000;
  49. private pageSize = 10;
  50. private cachedData = Array.from<any>({ length: this.length });
  51. private fetchedPages = new Set<number>();
  52. private dataStream = new BehaviorSubject<any[]>(this.cachedData);
  53. private subscription = new Subscription();
  54. constructor(private http: HttpClient) {
  55. super();
  56. }
  57. connect(collectionViewer: CollectionViewer): Observable<any[]> {
  58. this.subscription.add(
  59. collectionViewer.viewChange.subscribe(range => {
  60. const startPage = this.getPageForIndex(range.start);
  61. const endPage = this.getPageForIndex(range.end - 1);
  62. for (let i = startPage; i <= endPage; i++) {
  63. this.fetchPage(i);
  64. }
  65. })
  66. );
  67. return this.dataStream;
  68. }
  69. disconnect(): void {
  70. this.subscription.unsubscribe();
  71. }
  72. private getPageForIndex(index: number): number {
  73. return Math.floor(index / this.pageSize);
  74. }
  75. private fetchPage(page: number): void {
  76. if (this.fetchedPages.has(page)) {
  77. return;
  78. }
  79. this.fetchedPages.add(page);
  80. this.http
  81. .get(`https://randomuser.me/api/?results=${this.pageSize}&inc=name,gender,email,nat&noinfo`)
  82. .subscribe((res: any) => {
  83. this.cachedData.splice(page * this.pageSize, this.pageSize, ...res.results);
  84. this.dataStream.next(this.cachedData);
  85. });
  86. }
  87. }

API

nz-listcomponent

参数说明类型默认值
[nzDataSource]列表数据源any[]-
[nzRenderItem]自定义列表项TemplateRef<void>-
[nzBordered]是否展示边框booleanfalse
[nzFooter]列表底部string | TemplateRef<void>-
[nzGrid]列表栅格配置object-
[nzHeader]列表头部string | TemplateRef<void>-
[nzItemLayout]设置 nz-list-item 布局, 设置成 vertical 则竖直样式显示, 默认横排'vertical' | 'horizontal''horizontal'
[nzLoading]当卡片内容还在加载中时,可以用 loading 展示一个占位booleanfalse
[nzLoadMore]加载更多TemplateRef<void>-
[nzNoResult]当列表为空时加载的内容string | TemplateRef<void>-
[nzPagination]对应的 pagination 配置TemplateRef<void>-
[nzSize]list 的尺寸'large' | 'small' | 'default''default'
[nzSplit]是否展示分割线booleantrue

nzGrid

参数说明类型默认值
column列数number-
gutter栅格间隔number0
xs<576px 展示的列数number-
sm≥576px 展示的列数number-
md≥768px 展示的列数number-
lg≥992px 展示的列数number-
xl≥1200px 展示的列数number-
xxl≥1600px 展示的列数number-

nz-list-itemcomponent

参数说明类型默认值
[nzContent]内容项string | TemplateRef<void>-
[nzActions]列表操作组,根据 nzItemLayout 的不同, 位置在卡片底部或者最右侧。Array<TemplateRef<void><void>>-
[nzExtra]额外内容, 通常用在 nzItemLayoutvertical 的情况下, 展示右侧内容; horizontal 展示在列表元素最右侧TemplateRef<void>-
[nzNoFlex]是否非 flex 布局渲染booleanfalse

nz-list-item-metacomponent

参数说明类型默认值
[nzAvatar]列表元素的图标string | TemplateRef<void>-
[nzDescription]列表元素的描述内容string | TemplateRef<void>-
[nzTitle]列表元素的标题string | TemplateRef<void>-