st 表格·续1
import { Component } from '@angular/core';import { STColumn } from '@delon/abc/st';@Component({ selector: 'components-st-form', template: ` <div class="mb-md"> <input nz-input [(ngModel)]="params.name" name="name" nzPlaceHolder="请输入姓名" style="width: 100px;" class="mr-sm"> <button nz-button (click)="st.load(1)" [nzType]="'primary'">搜索</button> <button nz-button (click)="params = {}; st.reset()">重置</button> </div> <st #st [data]="url" [req]="{params: params}" [columns]="columns"></st> `,})export class ComponentsStFormComponent { url = `/users?total=100`; params: any = { name: 'asdf' }; // mock columns: STColumn[] = [ { title: '编号', index: 'id', default: '-' }, { title: '头像', type: 'img', width: 60, index: 'picture.thumbnail' }, { title: '邮箱', index: 'email' }, { title: '电话', index: 'phone' }, { title: '注册时间', type: 'date', index: 'registered' }, ];}
行事件
利用 (change) 实现点击行回调,因于DOM事件在同一元素上无法区分单或双击,若明确不需要双击事件,可以设定 rowClickTime 值为 0 以防止 200ms 迟延。
打开控制面板了解打印明细。
import { Component } from '@angular/core';import { STColumn, STChange } from '@delon/abc/st';@Component({ selector: 'components-st-row-click', template: ` <st [data]="url" [req]="{params: params}" [columns]="columns" (change)="_click($event)"></st>`,})export class ComponentsStRowClickComponent { url = `/users?results=3`; params = { a: 1, b: 2 }; // mock columns: STColumn[] = [ { title: '编号', index: 'id' }, { title: '邮箱', index: 'email' }, { title: '电话', index: 'phone' }, { title: '', buttons: [ { text: 'btn', type: 'link', click: (e: any) => console.log('btn click', e), }, ], }, ]; _click(e: STChange) { console.log(e); }}
单选框
利用 change 监听所选的数据。
import { Component } from '@angular/core';import { STColumn, STData, STChange } from '@delon/abc/st';@Component({ selector: 'components-st-radio', template: `<st [data]="url" [columns]="columns" [req]="{params: params}" [res]="{process: dataChange}" (change)="change($event)"></st>`,})export class ComponentsStRadioComponent { url = `/users?total=300`; params = { a: 1, b: 2 }; columns: STColumn[] = [ { title: '编号', index: 'id', type: 'radio' }, { title: '头像', type: 'img', width: 60, index: 'picture.thumbnail' }, { title: '邮箱', index: 'email' }, { title: '电话', index: 'phone' }, { title: '注册时间', type: 'date', index: 'registered' }, ]; change(ret: STChange) { console.log('change', ret); } dataChange(data: STData[]) { return data.map((i: STData, index: number) => { i.disabled = index === 0; i.hidden = index === 1; return i; }); }}
后端筛选和排序
利用 multiSort 支持多字段排序。
import { Component } from '@angular/core';import { STColumn } from '@delon/abc/st';@Component({ selector: 'components-st-sort', template: ` <button nz-button (click)="st.reset()">重置</button> <st #st [data]="url" [req]="{ params: params }" [columns]="columns" multiSort></st> `,})export class ComponentsStSortComponent { url = `/users?total=200`; params = { a: 1, b: 2 }; columns: STColumn[] = [ { title: '编号', index: 'id' }, { title: '头像', type: 'img', width: 60, index: 'picture.thumbnail' }, { title: '姓名', index: 'name.last', format: (item, _col, index) => `${index + 1}: ${item.name.last} ${item.name.first}`, sort: true, }, { title: '国家', index: 'nat', filter: { menus: [{ text: '中国', value: 'CH' }, { text: '美国', value: 'US' }, { text: '德国', value: 'DE' }], }, sort: true, }, { title: '性别', index: 'gender', filter: { menus: [{ text: 'male', value: 'male' }, { text: 'female', value: 'female' }], multiple: false, }, sort: true, }, { title: '注册时间', type: 'date', index: 'registered' }, ];}
可展开
使用 #expand 模板实现可展开,允许接收 item、index、column 三个值。附加可实现:嵌套子表格。
import { Component } from '@angular/core';import { STColumn } from '@delon/abc/st';@Component({ selector: 'components-st-expand', template: ` <st [data]="users" [columns]="columns" [expand]="expand" expandRowByClick expandAccordion> <ng-template #expand let-item let-index="index" let-column="column"> {{ item.description }} </ng-template> </st> `,})export class ComponentsStExpandComponent { 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, // 是否显示展开按钮 showExpand: idx !== 0, description: `${idx + 1}. My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park.`, }; }); columns: STColumn[] = [ { title: '编号', index: 'id' }, { title: '姓名', index: 'name' }, { title: '年龄', index: 'age' }, { buttons: [ { text: 'Button', }, ], }, ];}
自定义列
创建一个带有 st-row="custom-name" 的 ng-template,并在列描述 render: 'custom-name' 指定名称;模板允许接收 item、index、column 三个值。
如果指定 type="title" 表示是对标题自定义列。附加可实现:表头分组。
import { Component } from '@angular/core';import { STColumn } from '@delon/abc/st';@Component({ selector: 'components-st-render', template: ` <div class="mb-md"> <nz-checkbox-group [(ngModel)]="customColumns" name="customColumns" (ngModelChange)="st.resetColumns({ emitReload: true })"></nz-checkbox-group> </div> <st #st [data]="users" [columns]="columns"> <ng-template st-row="customTitle" type="title" let-c> {{ c.title.text }} <span nz-dropdown [nzDropdownMenu]="menuTpl" nzTrigger="click" [nzClickHide]="false" nzPlacement="bottomRight"> <i nz-icon nzType="down"></i> </span> <nz-dropdown-menu #menuTpl="nzDropdownMenu"> <div class="ant-table-filter-dropdown p-sm"> <input type="text" nz-input placeholder="Search name" [(ngModel)]="searchValue" name="searchValue" class="width-sm mr-sm"> <button nz-button [nzType]="'primary'" (click)="st.load(2)">Search</button> </div> </nz-dropdown-menu> </ng-template> <ng-template st-row="custom" let-item let-index="index"> <span nz-tooltip [nzTooltipTitle]="'年龄:' + item.age">tooltip: {{item.age}}-{{index}}</span> </ng-template> </st> `,})export class ComponentsStRenderComponent { searchValue: string; 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: '编号', index: 'id' }, { title: '姓名', index: 'name', iif: () => this.isChoose('name') }, { title: '年龄', index: 'age', iif: () => this.isChoose('age') }, { title: '自定义', renderTitle: 'customTitle', render: 'custom', iif: () => this.isChoose('custom'), }, ]; customColumns = [ { label: '姓名', value: 'name', checked: true }, { label: '年龄', value: 'age', checked: true }, { label: '自定义', value: 'custom', checked: true }, ]; isChoose(key: string): boolean { return !!this.customColumns.find(w => w.value === key && w.checked); }}
自定义按钮组
透过简单的配置产生一组日常按钮组(目标组件示例:DemoModalComponent、DemoDrawerComponent)。
对话框由ModalHelper处理,抽屉由DrawerHelper处理。
