st 表格·续3
import { Component } from '@angular/core';import { STColumn } from '@delon/abc/st';@Component({ selector: 'components-st-statistical', template: ` <button nz-button (click)="data = []">Clean Data</button> <st #st [data]="data" [columns]="columns" [body]="bodyTpl"> <ng-template #bodyTpl let-s> <ng-container *ngIf="st.count > 0"> <tr> <td>合计</td> <td>{{ s.len.text }} 个</td> <td>{{ s.dc.text }}</td> <td class="text-right">{{ s.sum.text }}</td> <td class="text-right">{{ s.avg.text }}</td> <td class="text-right">{{ s.min.text }}</td> <td class="text-right">{{ s.max.text }}</td> <td class="text-right">{{ s.custom.text }}</td> </tr> <tr class="bg-grey-lighter"> <td colspan="3">性别平均值</td> <td class="text-right">{{ s.sum.value / s.dc.value | _currency }}</td> <td colspan="4"></td> </tr> </ng-container> </ng-template> </st> `,})export class ComponentsStStatisticalComponent { data: any[] = Array(100) .fill({}) .map((_item: any, idx: number) => { return { id: idx + 1, price: ~~(Math.random() * 100), age: ~~(Math.random() * 100) > 50 ? '女' : '男', }; }); columns: STColumn[] = [ { title: '行号', type: 'no' }, { title: '编号', index: 'id', statistical: 'count', key: 'len' }, { title: '性别', index: 'age', statistical: 'distinctCount', key: 'dc' }, { title: 'Sum', index: 'price', type: 'currency', statistical: 'sum', key: 'sum' }, { title: 'Average', index: 'price', type: 'currency', statistical: 'average', key: 'avg' }, { title: 'Min', index: 'price', type: 'currency', statistical: 'min', key: 'min' }, { title: 'Max', index: 'price', type: 'currency', statistical: 'max', key: 'max' }, { title: 'Custom', index: 'price', type: 'currency', statistical: { type: values => ({ value: values[0], text: `**${values[0]}` }), currency: false }, key: 'custom', }, ];}
虚拟滚动
虚拟滚动,结合 cdk scrolling 的虚拟滚动,用于巨量数据加载。可以通过获得 cdkVirtualScrollViewport 进行进一步操作,见本示例及 API。
import { Component, ViewChild, OnDestroy, AfterViewInit } from '@angular/core';import { STColumn, STPage, STComponent } from '@delon/abc/st';import { Subject } from 'rxjs';import { takeUntil } from 'rxjs/operators';@Component({ selector: 'components-st-virtual', template: ` <button nz-button (click)="scrollToIndex(200)">Scroll To Index 200</button> <st #st [data]="data" [columns]="columns" [page]="page" virtualScroll [scroll]="{ x: '1300px', y: '240px' }"></st> `,})export class ComponentsStVirtualComponent implements AfterViewInit, OnDestroy { private destroy$ = new Subject(); @ViewChild('st', { static: false }) st: STComponent; page: STPage = { front: false, show: false, }; data: any[] = Array(2000) .fill({}) .map((_item: any, idx: number) => { return { id: idx + 1, price: ~~(Math.random() * 100), }; }); columns: STColumn[] = [ { title: '编号', index: 'id', width: 100 }, { title: '价格1', index: 'price', width: 100 }, { title: '价格2', index: 'price', width: 100 }, { title: '价格3', index: 'price', width: 100 }, { title: '价格4', index: 'price', width: 100 }, { title: '价格5', index: 'price', width: 100 }, { title: '价格6', index: 'price', width: 100 }, { title: '价格7', index: 'price', width: 100 }, { title: '价格8', index: 'price', width: 100 }, { title: '价格9', index: 'price', width: 100 }, { title: '价格10', index: 'price', width: 100 }, ]; scrollToIndex(index: number): void { this.st.cdkVirtualScrollViewport.scrollToIndex(index); } ngAfterViewInit(): void { this.st.cdkVirtualScrollViewport.scrolledIndexChange.pipe(takeUntil(this.destroy$)).subscribe(data => { console.log('scroll index to', data); }); } ngOnDestroy(): void { this.destroy$.next(); this.destroy$.complete(); }}
导出Excel
将表格数据保存为 Excel。
import { Component, ViewChild } from '@angular/core';import { STColumn, STComponent } from '@delon/abc/st';@Component({ selector: 'components-st-export', template: ` <button nz-button (click)="st.export()">Export</button> <button nz-button (click)="st.export(true)">Export Filtered Data</button> <button nz-button (click)="st.export(data, { filename: 'via-data.xlsx', sheetname: 'user' })">Export via data</button> <st #st [data]="data" [columns]="columns" class="mt-sm"></st> `,})export class ComponentsStExportComponent { @ViewChild('st', { static: false }) st: STComponent; data: any[] = Array(10000) .fill({}) .map((_item: any, index: number) => { return { id: index + 1, picture: { thumbnail: `https://randomuser.me/api/portraits/thumb/women/${Math.min(index + 1, 30)}.jpg`, }, email: `e${index + 1}@qq.com`, phone: `phone - ${index + 1}`, price: Math.ceil(Math.random() * 10000000) + 10000000, registered: new Date(), }; }); columns: STColumn[] = [ { title: '编号', index: 'id' }, { title: '头像', type: 'img', width: 60, index: 'picture.thumbnail', exported: false, }, { title: '邮箱', index: 'email' }, { title: '电话', index: 'phone' }, { title: '数字', index: 'price', type: 'number', sort: { compare: (a, b) => a.price - b.price, }, }, { title: '货币', index: 'price', type: 'currency' }, { title: '注册时间', type: 'date', index: 'registered' }, ];}
API
st
| [columns] |
列描述 |
STColumn[] |
- |
|
|
|
|
| [data] |
数据源 |
string, STData[], Observable<STData[]> |
- |
|
|
|
|
| [ps] |
每页数量,当设置为 0 表示不分页,默认:10 |
number |
10 |
|
|
|
|
| [total] |
当前总数据,在服务器渲染时需要传入,默认:0 |
number |
0 |
|
|
|
|
| [noResult] |
无数据时显示内容 |
string,TemplateRef<void> |
- |
|
|
|
|
| [bordered] |
是否显示边框 |
boolean |
false |
|
|
|
|
| [size] |
table大小 |
‘small’,’middle’,’default’ |
‘default’ |
|
|
|
|
| [widthMode] |
设置表格宽度模式 |
STWidthMode |
- |
|
|
|
|
| [rowClassName] |
表格行的类名 |
(record: STData, index: number) => string |
- |
|
|
|
|
| [loading] | 页面是否加载中,当指定 null 由 st 受控 | boolean | null | null |
|---|---|---|---|
| [loadingIndicator] |
加载指示符 |
TemplateRef<void> |
- |
|
|
|
|
| [loadingDelay] |
延迟显示加载效果的时间(防止闪烁) |
number |
0 |
|
|
|
|
| [scroll] |
横向或纵向支持滚动,也可用于指定滚动区域的宽高度:{ x: “300px”, y: “300px” } |
{ y?: string; x?: string } |
- |
|
|
|
|
| [virtualScroll] |
是否启用虚拟滚动模式,与 [nzScroll] 配合使用 |
boolean |
false |
|
|
|
|
| [virtualItemSize] |
虚拟滚动时每一列的高度,与 cdk itemSize 相同 |
number |
54 |
|
|
|
|
| [virtualMaxBufferPx] |
缓冲区最大像素高度,与 cdk maxBufferPx 相同 |
number |
200 |
|
|
|
|
| [virtualMinBufferPx] |
缓冲区最小像素高度,低于该值时将加载新结构,与 cdk minBufferPx 相同 |
number |
100 |
|
|
|
|
| [virtualForTrackBy] |
虚拟滚动数据 TrackByFunction 函数 |
TrackByFunction<T> |
- |
|
|
|
|
| [singleSort] | 单排序规则若不指定,则返回:columnName=ascend|descend若指定,则返回:sort=columnName.(ascend|descend) | STSingleSort | null |
|---|---|---|---|
| [multiSort] |
是否多排序,当 sort 多个相同值时自动合并,建议后端支持时使用 |
boolean, STMultiSort |
false |
|
|
|
|
| [rowClickTime] |
行单击多少时长之类为双击(单位:毫秒) |
number |
200 |
|
|
|
|
| [header] |
表格标题 |
string,TemplateRef<void> |
- |
|
|
|
|
| [footer] |
表格底部 |
string,TemplateRef<void> |
- |
|
|
|
|
| [bodyHeader] |
表格顶部额外内容,一般用于添加合计行 |
TemplateRef<STStatisticalResults> |
- |
|
|
|
|
| [body] |
表格额外内容,一般用于添加合计行 |
TemplateRef<STStatisticalResults> |
- |
|
|
|
|
| [widthConfig] |
表头分组时指定每列宽度,与 STColumn 的 width 不可混用 |
string[] |
- |
|
|
|
|