List列表

通用列表。

何时使用

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

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

代码演示

List列表 - 图1

简单列表

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

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

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

  1. import { Component } from '@angular/core';
  2. import { NzMessageService } from 'ng-zorro-antd/message';
  3. @Component({
  4. selector: 'nz-demo-list-simple',
  5. template: `
  6. <h3 [ngStyle]="{ 'margin-bottom.px': 16 }">Default Size</h3>
  7. <nz-list nzBordered nzHeader="Header" nzFooter="Footer">
  8. <nz-list-item *ngFor="let item of data">
  9. <span nz-typography><mark>[ITEM]</mark></span>
  10. {{ item }}
  11. </nz-list-item>
  12. </nz-list>
  13. <h3 [ngStyle]="{ margin: '16px 0' }">Small Size</h3>
  14. <nz-list nzBordered nzSize="small">
  15. <nz-list-header>Header</nz-list-header>
  16. <nz-list-item *ngFor="let item of data">item</nz-list-item>
  17. <nz-list-footer>Footer</nz-list-footer>
  18. </nz-list>
  19. <h3 [ngStyle]="{ margin: '16px 0' }">Large Size</h3>
  20. <ul nz-list [nzDataSource]="data" nzBordered nzSize="large">
  21. <nz-list-header>Header</nz-list-header>
  22. <li nz-list-item *ngFor="let item of data" nzNoFlex>
  23. <ul nz-list-item-actions>
  24. <nz-list-item-action>
  25. <a (click)="msg.info('edit')">edit</a>
  26. </nz-list-item-action>
  27. </ul>
  28. {{ item }}
  29. </li>
  30. <nz-list-footer>Footer</nz-list-footer>
  31. </ul>
  32. `
  33. })
  34. export class NzDemoListSimpleComponent {
  35. 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. constructor(public msg: NzMessageService) {}
  43. }

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 nzItemLayout="horizontal" [nzLoading]="loading">
  7. <nz-list-item *ngFor="let item of data">
  8. <nz-list-item-meta
  9. nzAvatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  10. nzDescription="Ant Design, a design language for background applications, is refined by Ant UED Team"
  11. >
  12. <nz-list-item-meta-title>
  13. <a href="https://ng.ant.design">{{ item.title }}</a>
  14. </nz-list-item-meta-title>
  15. </nz-list-item-meta>
  16. </nz-list-item>
  17. <nz-list-empty *ngIf="data.length === 0"></nz-list-empty>
  18. </nz-list>
  19. `
  20. })
  21. export class NzDemoListBasicComponent {
  22. loading = false;
  23. data = [
  24. {
  25. title: 'Ant Design Title 1'
  26. },
  27. {
  28. title: 'Ant Design Title 2'
  29. },
  30. {
  31. title: 'Ant Design Title 3'
  32. },
  33. {
  34. title: 'Ant Design Title 4'
  35. }
  36. ];
  37. change(): void {
  38. this.loading = true;
  39. if (this.data.length > 0) {
  40. setTimeout(() => {
  41. this.data = [];
  42. this.loading = false;
  43. }, 1000);
  44. } else {
  45. setTimeout(() => {
  46. this.data = [
  47. {
  48. title: 'Ant Design Title 1'
  49. },
  50. {
  51. title: 'Ant Design Title 2'
  52. },
  53. {
  54. title: 'Ant Design Title 3'
  55. },
  56. {
  57. title: 'Ant Design Title 4'
  58. }
  59. ];
  60. this.loading = false;
  61. }, 1000);
  62. }
  63. }
  64. }

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/message';
  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 class="demo-loadmore-list" [nzLoading]="initLoading">
  11. <nz-list-item *ngFor="let item of list">
  12. <ng-container *ngIf="!item.loading">
  13. <nz-list-item-meta
  14. nzAvatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  15. nzDescription="Ant Design, a design language for background applications, is refined by Ant UED Team"
  16. >
  17. <nz-list-item-meta-title>
  18. <a href="https://ng.ant.design">{{ item.name.last }}</a>
  19. </nz-list-item-meta-title>
  20. </nz-list-item-meta>
  21. content
  22. <ul nz-list-item-actions>
  23. <nz-list-item-action><a (click)="edit(item)">edit</a></nz-list-item-action>
  24. <nz-list-item-action><a (click)="edit(item)">more</a></nz-list-item-action>
  25. </ul>
  26. </ng-container>
  27. <nz-skeleton *ngIf="item.loading" [nzAvatar]="true" [nzActive]="true" [nzTitle]="false" [nzLoading]="true"> </nz-skeleton>
  28. </nz-list-item>
  29. <div class="loadmore" nz-list-load-more>
  30. <button nz-button *ngIf="!loadingMore" (click)="onLoadMore()">loading more</button>
  31. </div>
  32. </nz-list>
  33. `,
  34. styles: [
  35. `
  36. .demo-loadmore-list {
  37. min-height: 350px;
  38. }
  39. .loadmore {
  40. text-align: center;
  41. margin-top: 12px;
  42. height: 32px;
  43. line-height: 32px;
  44. }
  45. `
  46. ]
  47. })
  48. export class NzDemoListLoadmoreComponent implements OnInit {
  49. initLoading = true; // bug
  50. loadingMore = false;
  51. data: any[] = [];
  52. list: Array<{ loading: boolean; name: any }> = [];
  53. constructor(private http: HttpClient, private msg: NzMessageService) {}
  54. ngOnInit(): void {
  55. this.getData((res: any) => {
  56. this.data = res.results;
  57. this.list = res.results;
  58. this.initLoading = false;
  59. });
  60. }
  61. getData(callback: (res: any) => void): void {
  62. this.http.get(fakeDataUrl).subscribe((res: any) => callback(res));
  63. }
  64. onLoadMore(): void {
  65. this.loadingMore = true;
  66. this.list = this.data.concat([...Array(count)].fill({}).map(() => ({ loading: true, name: {} })));
  67. this.http.get(fakeDataUrl).subscribe((res: any) => {
  68. this.data = this.data.concat(res.results);
  69. this.list = [...this.data];
  70. this.loadingMore = false;
  71. });
  72. }
  73. edit(item: any): void {
  74. this.msg.success(item.email);
  75. }
  76. }

List列表 - 图4

竖排列表样式

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

  1. import { Component, OnInit } from '@angular/core';
  2. interface ItemData {
  3. href: string;
  4. title: string;
  5. avatar: string;
  6. description: string;
  7. content: string;
  8. }
  9. @Component({
  10. selector: 'nz-demo-list-vertical',
  11. template: `
  12. <nz-list nzItemLayout="vertical">
  13. <nz-list-item *ngFor="let item of data">
  14. <nz-list-item-meta>
  15. <nz-list-item-meta-avatar [nzSrc]="item.avatar"> </nz-list-item-meta-avatar>
  16. <nz-list-item-meta-title>
  17. <a href="{{ item.href }}">{{ item.title }}</a>
  18. </nz-list-item-meta-title>
  19. <nz-list-item-meta-description>
  20. {{ item.description }}
  21. </nz-list-item-meta-description>
  22. </nz-list-item-meta>
  23. {{ item.content }}
  24. <ul nz-list-item-actions>
  25. <nz-list-item-action><i nz-icon nzType="star-o" style="margin-right: 8px;"></i> 156</nz-list-item-action>
  26. <nz-list-item-action><i nz-icon nzType="star-o" style="margin-right: 8px;"></i> 156</nz-list-item-action>
  27. <nz-list-item-action><i nz-icon nzType="star-o" style="margin-right: 8px;"></i> 2</nz-list-item-action>
  28. </ul>
  29. <nz-list-item-extra>
  30. <img width="272" alt="logo" src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png" />
  31. </nz-list-item-extra>
  32. </nz-list-item>
  33. </nz-list>
  34. `
  35. })
  36. export class NzDemoListVerticalComponent implements OnInit {
  37. data: ItemData[] = [];
  38. ngOnInit(): void {
  39. this.loadData(1);
  40. }
  41. loadData(pi: number): void {
  42. this.data = new Array(5).fill({}).map((_, index) => {
  43. return {
  44. href: 'http://ant.design',
  45. title: `ant design part ${index} (page: ${pi})`,
  46. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  47. description: 'Ant Design, a design language for background applications, is refined by Ant UED Team.',
  48. content:
  49. 'We supply a series of design principles, practical patterns and high quality design resources ' +
  50. '(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 nzGrid>
  6. <div nz-row [nzGutter]="16">
  7. <div nz-col [nzSpan]="6" *ngFor="let item of data">
  8. <nz-list-item>
  9. <nz-card [nzTitle]="item.title">
  10. Card content
  11. </nz-card>
  12. </nz-list-item>
  13. </div>
  14. </div>
  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 nzGrid>
  6. <div nz-row [nzGutter]="16">
  7. <div nz-col [nzXl]="4" [nzLg]="6" [nzMd]="6" [nzSm]="12" [nzXs]="24" *ngFor="let item of data">
  8. <nz-list-item>
  9. <nz-card [nzTitle]="item.title">
  10. Card content
  11. </nz-card>
  12. </nz-list-item>
  13. </div>
  14. </div>
  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. import { CollectionViewer, DataSource } from '@angular/cdk/collections';
  2. import { HttpClient } from '@angular/common/http';
  3. import { ChangeDetectionStrategy, Component } from '@angular/core';
  4. import { BehaviorSubject, Observable, Subscription } from 'rxjs';
  5. interface ItemData {
  6. gender: string;
  7. name: Name;
  8. email: string;
  9. }
  10. interface Name {
  11. title: string;
  12. first: string;
  13. last: string;
  14. }
  15. @Component({
  16. selector: 'nz-demo-list-infinite-load',
  17. template: `
  18. <div>
  19. <cdk-virtual-scroll-viewport itemSize="73" class="demo-infinite-container">
  20. <nz-list>
  21. <nz-list-item *cdkVirtualFor="let item of ds">
  22. <nz-skeleton *ngIf="!item" [nzAvatar]="true" [nzParagraph]="{ rows: 1 }"></nz-skeleton>
  23. <nz-list-item-meta
  24. *ngIf="item"
  25. nzAvatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
  26. [nzDescription]="item.email"
  27. >
  28. <nz-list-item-meta-title>
  29. <a href="https://ng.ant.design">{{ item.name.last }}</a>
  30. </nz-list-item-meta-title>
  31. </nz-list-item-meta>
  32. </nz-list-item>
  33. </nz-list>
  34. </cdk-virtual-scroll-viewport>
  35. </div>
  36. `,
  37. styles: [
  38. `
  39. .demo-infinite-container {
  40. height: 300px;
  41. border: 1px solid #e8e8e8;
  42. border-radius: 4px;
  43. }
  44. nz-list {
  45. padding: 24px;
  46. }
  47. `
  48. ],
  49. changeDetection: ChangeDetectionStrategy.OnPush
  50. })
  51. export class NzDemoListInfiniteLoadComponent {
  52. ds = new MyDataSource(this.http);
  53. constructor(private http: HttpClient) {}
  54. }
  55. class MyDataSource extends DataSource<ItemData> {
  56. private length = 100000;
  57. private pageSize = 10;
  58. private cachedData = Array.from<ItemData>({ length: this.length });
  59. private fetchedPages = new Set<number>();
  60. private dataStream = new BehaviorSubject<ItemData[]>(this.cachedData);
  61. private subscription = new Subscription();
  62. constructor(private http: HttpClient) {
  63. super();
  64. }
  65. connect(collectionViewer: CollectionViewer): Observable<ItemData[]> {
  66. this.subscription.add(
  67. collectionViewer.viewChange.subscribe(range => {
  68. const startPage = this.getPageForIndex(range.start);
  69. const endPage = this.getPageForIndex(range.end - 1);
  70. for (let i = startPage; i <= endPage; i++) {
  71. this.fetchPage(i);
  72. }
  73. })
  74. );
  75. return this.dataStream;
  76. }
  77. disconnect(): void {
  78. this.subscription.unsubscribe();
  79. }
  80. private getPageForIndex(index: number): number {
  81. return Math.floor(index / this.pageSize);
  82. }
  83. private fetchPage(page: number): void {
  84. if (this.fetchedPages.has(page)) {
  85. return;
  86. }
  87. this.fetchedPages.add(page);
  88. this.http
  89. .get<{ results: ItemData[] }>(`https://randomuser.me/api/?results=${this.pageSize}&inc=name,gender,email,nat&noinfo`)
  90. .subscribe(res => {
  91. this.cachedData.splice(page * this.pageSize, this.pageSize, ...res.results);
  92. this.dataStream.next(this.cachedData);
  93. });
  94. }
  95. }

API

nz-listcomponent

参数说明类型默认值
[nzBordered]是否展示边框booleanfalse
[nzFooter]列表底部string | TemplateRef<void>-
[nzHeader]列表头部string | TemplateRef<void>-
[nzItemLayout]设置 nz-list-item 布局, 设置成 vertical 则竖直样式显示, 默认横排‘vertical’ | ‘horizontal’‘horizontal’
[nzLoading]当卡片内容还在加载中时,可以用 loading 展示一个占位booleanfalse
[nzSize]list 的尺寸‘large’ | ‘small’ | ‘default’‘default’
[nzSplit]是否展示分割线booleantrue

nz-list-emptycomponent

列表空内容组件

参数说明类型默认值
[nzNoResult]空内容显示内容string | TemplateRef<void>-

nz-list[nzGrid]component

使用栅格布局

nz-list-headercomponent

列表头部位置组件

nz-list-footercomponent

列表脚部位置组件

nz-list-paginationcomponent

列表分页位置组件

nz-list-load-morecomponent

列表加载更多位置组件


nz-list-itemcomponent

参数说明类型默认值
[nzNoFlex]是否非 flex 布局渲染booleanfalse

ul[nz-list-item-actions]

列表项操作项容器组件

nz-list-item-actioncomponent

列表项操作项组件

nz-list-item-extracomponent


列表项扩展内容位置组件

nz-list-item-metacomponent

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

nz-list-item-meta-titlecomponent

列表项元信息标题部分组件

nz-list-item-meta-descriptioncomponent

列表项元信息描述部分组件

nz-list-item-meta-avatarcomponent

列表项元信息头像部分组件