List列表

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

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

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

    通过设置 为 largesmall 分别把按钮设为大、小尺寸。若不设置 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 [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

    基础列表

    基础列表。

    可通过 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
    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. </ng-template>
    33. </nz-list-item-meta>
    34. </nz-skeleton>
    35. </nz-list-item>
    36. </ng-template>
    37. <ng-template #loadMore>
    38. <div class="loadmore">
    39. <button nz-button *ngIf="!loadingMore" (click)="onLoadMore()">loading more</button>
    40. </div>
    41. </ng-template>
    42. </nz-list>
    43. `,
    44. styles: [
    45. `
    46. .demo-loadmore-list {
    47. min-height: 350px;
    48. }
    49. .loadmore {
    50. text-align: center;
    51. margin-top: 12px;
    52. height: 32px;
    53. line-height: 32px;
    54. }
    55. `
    56. ]
    57. })
    58. initLoading = true; // bug
    59. loadingMore = false;
    60. data: any[] = [];
    61. list: Array<{ loading: boolean; name: any }> = [];
    62. constructor(private http: HttpClient, private msg: NzMessageService) {}
    63. ngOnInit(): void {
    64. this.getData((res: any) => {
    65. this.data = res.results;
    66. this.list = res.results;
    67. this.initLoading = false;
    68. });
    69. }
    70. getData(callback: (res: any) => void): void {
    71. this.http.get(fakeDataUrl).subscribe((res: any) => callback(res));
    72. }
    73. onLoadMore(): void {
    74. this.loadingMore = true;
    75. this.list = this.data.concat([...Array(count)].fill({}).map(() => ({ loading: true, name: {} })));
    76. this.http.get(fakeDataUrl).subscribe((res: any) => {
    77. this.data = this.data.concat(res.results);
    78. this.list = [...this.data];
    79. this.loadingMore = false;
    80. });
    81. }
    82. edit(item: any): void {
    83. this.msg.success(item.email);
    84. }
    85. }

    List列表 - 图4

    竖排列表样式

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

    可以通过设置 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 保持一致。

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

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

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

    nzGrid