st 表格·续2

import { Component } from '@angular/core';import { STColumn } from '@delon/abc/st';import { NzMessageService } from 'ng-zorro-antd/message';import { DemoModalComponent, DemoDrawerComponent } from '@shared';@Component({  selector: 'components-st-buttons',  template: `    <​st [data]="users" [columns]="columns"><​/st>  `,})export class ComponentsStButtonsComponent {  constructor(private message: NzMessageService) {}  users: any[] = Array(10)    .fill({})    .map((_item: any, idx: number) => {      return {        id: idx + 1,        name: `name ${idx + 1}`,        age: Math.ceil(Math.random() * 10) + 20,      };    });  columns: STColumn[] = [    { title: '序号', type: 'no' },    { title: '编号', index: 'id' },    { title: '姓名', index: 'name' },    { title: '年龄', index: 'age' },    {      title: '操作区',      buttons: [        {          text: 'Edit',          icon: 'edit',          type: 'modal',          modal: {            component: DemoModalComponent,          },          click: (_record, modal) => this.message.success(`重新加载页面,回传值:${JSON.stringify(modal)}`),        },        {          text: 'Drawer',          type: 'drawer',          drawer: {            title: '编辑',            component: DemoDrawerComponent,          },          click: (_record, modal) => this.message.success(`重新加载页面,回传值:${JSON.stringify(modal)}`),        },        {          icon: 'check-circle',          click: record => this.message.info(`check-${record.name}`),          iif: record => record.id % 2 === 0,          iifBehavior: 'disabled',          tooltip: `Is disabled button`,        },        {          icon: 'delete',          type: 'del',          pop: {            title: 'Yar you sure?',            okType: 'danger',            icon: 'star',          },          click: (record, _modal, comp) => {            this.message.success(`成功删除【${record.name}】`);            comp!.removeRow(record);          },          iif: record => record.id % 2 === 0,        },        {          text: '更多',          children: [            {              text: record => (record.id === 1 ? `过期` : `正常`),              click: record => this.message.error(`${record.id === 1 ? `过期` : `正常`}【${record.name}】`),            },            {              text: `审核`,              click: record => this.message.info(`check-${record.name}`),              iif: record => record.id % 2 === 0,              iifBehavior: 'disabled',              tooltip: 'This is tooltip',            },            {              type: 'divider',            },            {              text: `重新开始`,              icon: 'edit',              click: record => this.message.success(`重新开始【${record.name}】`),            },          ],        },      ],    },  ];}

自定义小部件

类型为 widget 自定义小部件,例如点击头像处理。

import { Component, ViewChild } from '@angular/core';import { STColumn, STComponent } from '@delon/abc/st';@Component({  selector: 'components-st-widget',  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>      <​button (click)="changeImg()" nz-button>Change Img Data<​/button>    <​/div>    <​st #st [data]="url" [columns]="columns"><​/st>  `,})export class ComponentsStWidgetComponent {  url = `/users?total=100`;  @ViewChild('st', { static: false }) private st: STComponent;  columns: STColumn[] = [    { title: '编号', index: 'id', width: 80 },    {      title: '自定义头像',      type: 'widget',      widget: { type: 'img', params: ({ record }) => ({ img: record.picture.thumbnail }) },      width: 150,    },    { title: '邮箱', index: 'email' },  ];  changeImg(): void {    this.st.setRow(0, { picture: { thumbnail: 'https://ng-alain.com/assets/img/logo-color.svg' } }, { refreshSchema: true, emitReload: false });  }}

固定列

对于列数很多的数据,可以使用 left 和 right 固定前后的列,横向滚动查看其它数据,需要和 scroll.x 配合使用。

固定列使用了 sticky 属性,浏览器支持情况可以参考这里

- 若列头与内容不对齐或出现列重复,请指定列的宽度 width

- 建议指定 scroll.x 为大于表格宽度的固定值或百分比。注意,且非固定列宽度之和不要超过 scroll.x

import { Component } from '@angular/core';import { STColumn } from '@delon/abc/st';@Component({  selector: 'components-st-fixed',  template: `    <​st [data]="users" [columns]="columns" [scroll]="{x: '1300px'}"><​/st>  `,})export class ComponentsStFixedComponent {  users: any[] = Array(10)    .fill({})    .map((_item: any, idx: number) => {      return {        id: idx + 1,        name: `name ${idx + 1}`,        age: Math.ceil(Math.random() * 10) + 20,      };    });  columns: STColumn[] = [    { title: '编号1', index: 'id', fixed: 'left', width: 100 },    { title: '编号2', index: 'id', fixed: 'left', width: 100 },    { title: '编号3', index: 'id', fixed: 'left', width: 100 },    { title: '编号4', index: 'id' },    { title: '编号5', index: 'id' },    { title: '编号6', index: 'id' },    { title: '编号7', index: 'id' },    { title: '编号8', index: 'id' },    { title: '编号8', index: 'id' },    { title: '编号8', index: 'id' },    { title: '编号8', index: 'id' },    { title: '编号8', index: 'id' },    { title: '姓名10', index: 'name', fixed: 'right', width: 100 },    { title: '姓名11', index: 'name', fixed: 'right', width: 100 },    { title: '年龄12', index: 'age', fixed: 'right', width: 100 },  ];}

表头分组

columns[n] 可以内嵌 children,以渲染分组表头。

import { Component } from '@angular/core';import { STColumn } from '@delon/abc/st';@Component({  selector: 'components-st-grouping-columns',  template: ` <​st #st [data]="url" [req]="{ params: params }" [columns]="columns" bordered size="middle"> <​/st>`,})export class ComponentsStGroupingColumnsComponent {  url = `/users?total=2&field=list`;  params = { a: 1, b: 2 };  columns: STColumn[] = [    { title: '编号', index: 'id', sort: true },    {      title: 'Other',      children: [        { title: '头像', type: 'img', width: 60, index: 'picture.thumbnail' },        { title: '邮箱', index: 'email' },        {          title: '姓名',          sort: true,          children: [            { title: 'first', index: 'name.first', sort: true },            { title: 'last', index: 'name.last' },          ],        },      ],    },  ];}

响应式

小屏幕下将以响应模式堆叠显示,responsiveHideHeaderFooter 属性可以使大屏幕不显示头和尾,反之。

import { Component } from '@angular/core';import { STColumn } from '@delon/abc/st';@Component({  selector: 'components-st-responsive',  template: `  <​st [data]="url" [req]="{params: params}" [columns]="columns"    header="The header" footer="The footer" responsiveHideHeaderFooter>  <​/st>`,})export class ComponentsStResponsiveComponent {  url = `/users?total=100`;  params = { a: 1, b: 2 };  columns: STColumn[] = [    { title: '编号', index: 'id' },    { title: '头像', type: 'img', width: 60, index: 'picture.thumbnail' },    { title: '邮箱', index: 'email' },    { title: '电话', index: 'phone' },    { title: '注册时间', type: 'date', index: 'registered' },  ];}

统计

支持 count、distinctCount、sum、average、max、min、自定义统计方法。