List列表
最基础的列表展示,可承载文字、列表、图片、段落,常用于后台数据展示页面。
简单列表
列表拥有大、中、小三种尺寸。
通过设置 为 large``small
分别把按钮设为大、小尺寸。若不设置 size
,则尺寸为中。
可通过设置 nzHeader
和 nzFooter
,来自定义列表头部和尾部。
import { Component } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
selector: 'nz-demo-list-simple',
template: `
<h3 [ngStyle]="{ 'margin-bottom.px': 16 }">Default Size</h3>
<nz-list nzBordered nzHeader="Header" nzFooter="Footer">
<nz-list-item *ngFor="let item of data">
<span nz-typography><mark>[ITEM]</mark></span>
{{ item }}
</nz-list-item>
</nz-list>
<h3 [ngStyle]="{ margin: '16px 0' }">Small Size</h3>
<nz-list nzBordered nzSize="small">
<nz-list-header>Header</nz-list-header>
<nz-list-item *ngFor="let item of data">item</nz-list-item>
<nz-list-footer>Footer</nz-list-footer>
</nz-list>
<h3 [ngStyle]="{ margin: '16px 0' }">Large Size</h3>
<ul nz-list [nzDataSource]="data" nzBordered nzSize="large">
<nz-list-header>Header</nz-list-header>
<li nz-list-item *ngFor="let item of data" nzNoFlex>
<ul nz-list-item-actions>
<nz-list-item-action>
<a (click)="msg.info('edit')">edit</a>
</nz-list-item-action>
</ul>
{{ item }}
</li>
<nz-list-footer>Footer</nz-list-footer>
</ul>
`
})
export class NzDemoListSimpleComponent {
data = [
'Racing car sprays burning fuel into crowd.',
'Japanese princess to wed commoner.',
'Australian walks 100km after outback crash.',
'Man charged over missing wedding girl.',
'Los Angeles battles huge wildfires.'
];
constructor(public msg: NzMessageService) {}
}
基础列表
基础列表。
加载更多
// tslint:disable:no-any
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
const count = 5;
const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
@Component({
selector: 'nz-demo-list-loadmore',
template: `
<nz-list class="demo-loadmore-list" [nzLoading]="initLoading">
<nz-list-item *ngFor="let item of list">
<ng-container *ngIf="!item.loading">
<nz-list-item-meta
nzAvatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
nzDescription="Ant Design, a design language for background applications, is refined by Ant UED Team"
>
<nz-list-item-meta-title>
<a href="https://ng.ant.design">{{ item.name.last }}</a>
</nz-list-item-meta-title>
</nz-list-item-meta>
content
<ul nz-list-item-actions>
<nz-list-item-action><a (click)="edit(item)">edit</a></nz-list-item-action>
<nz-list-item-action><a (click)="edit(item)">more</a></nz-list-item-action>
</ul>
</ng-container>
<nz-skeleton *ngIf="item.loading" [nzAvatar]="true" [nzActive]="true" [nzTitle]="false" [nzLoading]="true"> </nz-skeleton>
</nz-list-item>
<button nz-button *ngIf="!loadingMore" (click)="onLoadMore()">loading more</button>
</div>
</nz-list>
`,
styles: [
`
.demo-loadmore-list {
min-height: 350px;
}
.loadmore {
text-align: center;
margin-top: 12px;
height: 32px;
line-height: 32px;
}
`
]
})
export class NzDemoListLoadmoreComponent implements OnInit {
initLoading = true; // bug
loadingMore = false;
list: Array<{ loading: boolean; name: any }> = [];
constructor(private http: HttpClient, private msg: NzMessageService) {}
ngOnInit(): void {
this.getData((res: any) => {
this.data = res.results;
this.list = res.results;
this.initLoading = false;
});
}
getData(callback: (res: any) => void): void {
this.http.get(fakeDataUrl).subscribe((res: any) => callback(res));
}
onLoadMore(): void {
this.loadingMore = true;
this.list = this.data.concat([...Array(count)].fill({}).map(() => ({ loading: true, name: {} })));
this.http.get(fakeDataUrl).subscribe((res: any) => {
this.data = this.data.concat(res.results);
this.list = [...this.data];
this.loadingMore = false;
});
}
edit(item: any): void {
this.msg.success(item.email);
}
}
竖排列表样式
通过设置 nzItemLayout
属性为 vertical
可实现竖排列表样式。
栅格列表
可以通过设置 nz-list
的 nzGrid
属性来实现栅格列表。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-list-grid',
template: `
<nz-list nzGrid>
<div nz-row [nzGutter]="16">
<div nz-col [nzSpan]="6" *ngFor="let item of data">
<nz-list-item>
<nz-card [nzTitle]="item.title">
Card content
</nz-card>
</nz-list-item>
</div>
</div>
</nz-list>
`
})
export class NzDemoListGridComponent {
data = [
{
title: 'Title 1'
},
{
title: 'Title 2'
},
{
title: 'Title 3'
},
{
title: 'Title 4'
}
];
}
响应式的栅格列表
响应式的栅格列表。
滚动加载无限长列表
Virtualized
是在大数据列表中应用的一种技术,主要是为了减少不可见区域不必要的渲染从而提高性能,特别是数据量在成千上万条效果尤为明显。
import { CollectionViewer, DataSource } from '@angular/cdk/collections';
import { HttpClient } from '@angular/common/http';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
interface ItemData {
gender: string;
name: Name;
email: string;
interface Name {
title: string;
first: string;
last: string;
}
@Component({
selector: 'nz-demo-list-infinite-load',
template: `
<div>
<cdk-virtual-scroll-viewport itemSize="73" class="demo-infinite-container">
<nz-list>
<nz-list-item *cdkVirtualFor="let item of ds">
<nz-skeleton *ngIf="!item" [nzAvatar]="true" [nzParagraph]="{ rows: 1 }"></nz-skeleton>
<nz-list-item-meta
*ngIf="item"
nzAvatar="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
[nzDescription]="item.email"
>
<nz-list-item-meta-title>
<a href="https://ng.ant.design">{{ item.name.last }}</a>
</nz-list-item-meta-title>
</nz-list-item-meta>
</nz-list-item>
</cdk-virtual-scroll-viewport>
</div>
`,
styles: [
`
.demo-infinite-container {
height: 300px;
border: 1px solid #e8e8e8;
border-radius: 4px;
}
nz-list {
padding: 24px;
}
`
],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class NzDemoListInfiniteLoadComponent {
ds = new MyDataSource(this.http);
constructor(private http: HttpClient) {}
}
class MyDataSource extends DataSource<ItemData> {
private length = 100000;
private pageSize = 10;
private cachedData = Array.from<ItemData>({ length: this.length });
private fetchedPages = new Set<number>();
private dataStream = new BehaviorSubject<ItemData[]>(this.cachedData);
private subscription = new Subscription();
constructor(private http: HttpClient) {
super();
}
connect(collectionViewer: CollectionViewer): Observable<ItemData[]> {
this.subscription.add(
collectionViewer.viewChange.subscribe(range => {
const startPage = this.getPageForIndex(range.start);
const endPage = this.getPageForIndex(range.end - 1);
for (let i = startPage; i <= endPage; i++) {
this.fetchPage(i);
}
})
);
return this.dataStream;
}
disconnect(): void {
this.subscription.unsubscribe();
}
private getPageForIndex(index: number): number {
return Math.floor(index / this.pageSize);
}
private fetchPage(page: number): void {
if (this.fetchedPages.has(page)) {
return;
}
this.fetchedPages.add(page);
this.http
.get<{ results: ItemData[] }>(`https://randomuser.me/api/?results=${this.pageSize}&inc=name,gender,email,nat&noinfo`)
.subscribe(res => {
this.cachedData.splice(page * this.pageSize, this.pageSize, ...res.results);
this.dataStream.next(this.cachedData);
});
}
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
列表项元信息头像部分组件