List列表

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

    简单列表

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

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

    List列表 - 图4

    竖排列表样式

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

    栅格列表

    可以通过设置 nz-listnzGrid 属性来实现栅格列表。

    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

    响应式的栅格列表

    响应式的栅格列表。

    滚动加载无限长列表

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

    nz-list-emptycomponent

    列表空内容组件

    nz-list[nzGrid]component

    使用栅格布局

    nz-list-headercomponent

    列表头部位置组件

    nz-list-footercomponent

    列表脚部位置组件

    列表分页位置组件

    nz-list-load-morecomponent

    列表加载更多位置组件


    nz-list-itemcomponent

    ul[nz-list-item-actions]

    列表项操作项容器组件

    nz-list-item-actioncomponent

    列表项操作项组件


    列表项扩展内容位置组件

    nz-list-item-metacomponent

    nz-list-item-meta-titlecomponent

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

    nz-list-item-meta-descriptioncomponent

    nz-list-item-meta-avatarcomponent

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