st 表格
st 表格
st 并不是在创造另一个表格组件,而是在 nz-table 基础上以可配置形式渲染表格,在中后台里这种方式可以满足绝大多数场景,但又可以更易地管理表格渲染动作。
关于数据源
data 支持三种不同格式数据源,整体分为:URL和静态数据两类;但可以透过参数的配置达到不同的效果,同时有非常多参数可通过 AlainSTConfig 重置默认值,使整个 st 组件模板达到极简。
URL
指的是通过一个 URL 字符串来获取数据。
- 通过 req.params、req.method 等参数解决请求数据格式问题
- 通过 res.reName 重置数据 key 而无须担心后端数据格式是否满足 st 需求
- 通过 res.process 可以对表格渲染前对数据进一步优化
- 通过 page.zeroIndexed 可以调整 http 请求时 pi 参数是否遵循 0 基索引,默认情况下为 1 基索引
- 若返回体的值是数组类型,则强制不分页
- 使用 _HttpClient 发起请求,因此满足 AlainThemeConfig 的配置也适用
静态数据
指的是通过指定值为 STData[] 或 Observable<STData[]>,二者都遵循以下规则:
- page.front 前端分页,默认:true
- true 由 st 根据 data 长度受控分页,包括:排序、过滤等
- false 由用户通过 total 和 data 参数受控分页,并维护 (change) 当分页变更时重新加载数据
- page.show 是否显示分页器;当未指定时若 ps>total 情况下自动不显示
常见问题
Cannot read property ‘text’ of undefined
若组件已经加载完毕,此时如果再次改变 columns 时可能会出现该错误,这是因为 st 每次只会根据 columns 对数据处理,当列定义发生改变后可能会因为列定义与现有数据无法配对,可能需要使用 this.st.resetColumns({ columns: [], emitReload: true }) 来更新列定义并重新加载数据。
import { STModule } from '@delon/abc/st';
代码演示
基本
快速生成表格;利用 res 可以适配后端数据格式。
import { Component, ViewChild } from '@angular/core';import { STColumn, STComponent } from '@delon/abc/st';@Component({ selector: 'components-st-basic', template: ` <button nz-button nzType="primary" (click)="setRow()">setRow Method</button> <st #st [widthMode]="{ type: 'strict' }" [data]="url" [req]="{ params: params }" [columns]="columns"></st> `,})export class ComponentsStBasicComponent { url = `/users?total=2&field=list`; params = { a: 1, b: 2 }; @ViewChild('st', { static: false }) private st: STComponent; columns: STColumn[] = [ { title: '编号', index: 'id', width: 80 }, { title: '头像', type: 'img', width: 60, index: 'picture.thumbnail' }, { title: '邮箱', index: 'email', width: 80 }, { title: '电话', index: 'phone' }, { title: { text: '佣金', optional: '(单位:元)', optionalHelp: '计算公式=订单金额 * 0.6%' }, index: 'price', type: 'currency' }, { title: '注册时间', type: 'date', index: 'registered' }, ]; setRow(): void { this.st.setRow(0, { price: 100000000 }) }}
自定义数据
data 属性支持 STData[]、Observable 数据类型。
import { Component, OnInit } from '@angular/core';import { STColumn, STChange } from '@delon/abc/st';import { of } from 'rxjs';import { delay } from 'rxjs/operators';@Component({ selector: 'components-st-custom-data', template: ` <div class="mb-md"> <button (click)="st.clear()" nz-button>Clear Data</button> <button (click)="st.reload()" nz-button>Reload Data</button> <button (click)="st.clearStatus(); st.reload()" nz-button>Clear Status</button> </div> <st #st [data]="users" [columns]="columns" (change)="change($event)"></st> `,})export class ComponentsStCustomDataComponent implements OnInit { users: any[] = []; columns: STColumn[] = [ { title: '编号', index: 'id', type: 'checkbox', selections: [ { text: '小于25岁', select: data => data.forEach(item => (item.checked = item.age < 25)), }, { text: '大于25岁', select: data => data.forEach(item => (item.checked = item.age >= 25)), }, ], }, { title: '姓名', index: 'name', sort: { compare: (a, b) => a.name.length - b.name.length, }, filter: { type: 'keyword', fn: (filter, record) => !filter.value || record.name.indexOf(filter.value) !== -1, }, }, { title: '年龄', index: 'age', sort: { compare: (a, b) => a.age - b.age, }, filter: { menus: [{ text: '20岁以下', value: [0, 20] }, { text: '20-25岁', value: [20, 25] }, { text: '25岁以上', value: [25, 100] }], fn: (filter, record) => record.age >= filter.value[0] && record.age <= filter.value[1], multiple: false, }, }, { title: '状态', type: 'badge', index: 'status', badge: { 1: { text: 'Success', color: 'success' }, 2: { text: 'Error', color: 'error' }, 3: { text: 'Processing', color: 'processing' }, 4: { text: 'Default', color: 'default' }, 5: { text: 'Warning', color: 'warning' }, }, }, ]; ngOnInit(): void { of( Array(100) .fill({}) .map((_item: any, idx: number) => { return { id: idx + 1, name: `name ${idx + 1}`, age: Math.ceil(Math.random() * 10) + 20, status: Math.floor(Math.random() * 5) + 1, }; }), ) .pipe(delay(500)) .subscribe(res => (this.users = res)); } change(e: STChange) { console.log(e); }}
列类型
支持十种不同列类型:行号、多选、单选、徽标、标签、图片、数字、货币、日期、布尔徽章、枚举。也可以使用自定义列完成更复杂渲染。
import { Component } from '@angular/core';import { STColumn, STColumnBadge, STColumnTag } from '@delon/abc/st';const BADGE: STColumnBadge = { 1: { text: '成功', color: 'success' }, 2: { text: '错误', color: 'error' }, 3: { text: '进行中', color: 'processing' }, 4: { text: '默认', color: 'default' }, 5: { text: '警告', color: 'warning' },};const TAG: STColumnTag = { 1: { text: '成功', color: 'green' }, 2: { text: '错误', color: 'red' }, 3: { text: '进行中', color: 'blue' }, 4: { text: '默认', color: '' }, 5: { text: '警告', color: 'orange' },};const r = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1) + min);@Component({ selector: 'components-st-type', template: ` <button nz-button (click)="reload()">Reload</button> <st #st [data]="users" [columns]="columns" [page]="{ position: 'both' }"></st> `,})export class ComponentsStTypeComponent { users: any[] = []; columns: STColumn[] = [ { title: '行号', type: 'no' }, { title: '姓名', index: 'name' }, { title: '年龄', index: 'age', type: 'number' }, { title: 'tag', index: 'tag', type: 'tag', tag: TAG }, { title: 'badge', index: 'badge', type: 'badge', badge: BADGE }, { title: 'Enum', index: 'enum', type: 'enum', enum: { 1: '壹', 2: '贰', 3: '叁' } }, { title: 'yn', index: 'yn', type: 'yn' }, ]; reload() { this.users = Array(10) .fill({}) .map((_item: any, idx: number) => { return { id: idx + 1, name: `name ${idx + 1}`, age: r(10, 50), tag: r(1, 5), badge: r(1, 5), enum: r(1, 3), yn: [true, false][r(1, 5) % 2], }; }); } constructor() { this.reload(); }}
可选择
利用 change 监听所选的数据。
import { Component } from '@angular/core';import { STColumn, STData, STChange } from '@delon/abc/st';@Component({ selector: 'components-st-checkbox', template: `<st [data]="url" [columns]="columns" [req]="{params: params}" [res]="{process: dataProcess}" (change)="change($event)"></st>`,})export class ComponentsStCheckboxComponent { url = `/users?total=100`; params = { a: 1, b: 2 }; columns: STColumn[] = [ { title: '编号', index: 'id.value', type: 'checkbox' }, { title: '头像', type: 'img', width: 60, index: 'picture.thumbnail' }, { title: '邮箱', index: 'email' }, { title: '电话', index: 'phone' }, { title: '注册时间', type: 'date', index: 'registered' }, ]; change(e: STChange) { console.log('change', e); } dataProcess(data: STData[]) { return data.map((i: STData, index: number) => { i.disabled = index === 0; return i; }); }}
搜索表单
配合 load() & reset() 实现搜索表单。
