tree-sel​ect 树选择

tree-sel​ect 树选择

树型选择控件。

注意:

  • tree-sel​ect 的数据源必须包含 title、key 键名

代码演示

基础样例

最简单的用法。

多选:重置时无法刷新默认值 #2085

import { Component } from '@angular/core';import { SFSchema, SFTreeSelectWidgetSchema } from '@delon/form';import { NzMessageService } from 'ng-zorro-antd/message';import { of } from 'rxjs';import { delay } from 'rxjs/operators';@Component({  selector: 'form-tree-sel​ect-simple',  template: `    <​sf [schema]="schema" (formSubmit)="submit($event)"><​/sf>  `,})export class FormTreeSelectSimpleComponent {  schema: SFSchema = {    properties: {      status1: {        type: 'string',        title: '基本',        enum: [          { title: '待支付', key: 'WAIT_BUYER_PAY' },          { title: '已支付', key: 'TRADE_SUCCESS' },          { title: '交易完成', key: 'TRADE_FINISHED' },        ],        default: 'WAIT_BUYER_PAY',        ui: {          widget: 'tree-sel​ect',        } as SFTreeSelectWidgetSchema,      },      status2: {        type: 'string',        title: '多选',        enum: [          { title: '待支付', key: 'WAIT_BUYER_PAY' },          { title: '已支付', key: 'TRADE_SUCCESS' },          { title: '交易完成', key: 'TRADE_FINISHED' },        ],        default: ['WAIT_BUYER_PAY', 'TRADE_SUCCESS'],        ui: {          widget: 'tree-sel​ect',          multiple: true,        } as SFTreeSelectWidgetSchema,      },      status3: {        type: 'string',        title: '可勾选',        default: ['WAIT_BUYER_PAY', 'TRADE_FINISHED'],        ui: {          widget: 'tree-sel​ect',          checkable: true,          asyncData: () =>            of([              { title: '待支付', key: 'WAIT_BUYER_PAY' },              { title: '已支付', key: 'TRADE_SUCCESS' },              { title: '交易完成', key: 'TRADE_FINISHED' },            ]).pipe(delay(10)),        } as SFTreeSelectWidgetSchema,      },      // 异步数据      async: {        type: 'string',        title: 'Async',        enum: [          { title: '待支付', key: 'WAIT_BUYER_PAY' },          { title: '已支付', key: 'TRADE_SUCCESS' },          { title: '交易完成', key: 'TRADE_FINISHED' },        ],        ui: {          widget: 'tree-sel​ect',          expandChange: () => {            return of([              { title: '待支付', key: 'WAIT_BUYER_PAY' },              { title: '已支付', key: 'TRADE_SUCCESS' },              { title: '交易完成', key: 'TRADE_FINISHED' },            ]).pipe(delay(10));          },        } as SFTreeSelectWidgetSchema,      },    },  };  constructor(public msg: NzMessageService) {}  submit(value: any) {    this.msg.success(JSON.stringify(value));  }}

自定义图标

可以针对不同节点采用样式覆盖的方式定制图标。

import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';import { SFSchema, SFTreeSelectWidgetSchema } from '@delon/form';import { NzTreeNode } from 'ng-zorro-antd/tree';@Component({  selector: 'form-tree-sel​ect-customized-icon',  template: `    <​sf *ngIf="schema" [schema]="schema"><​/sf>    <​ng-template #customTpl let-node>      <​span class="ant-tree-node-content-wrapper" [class.ant-tree-node-selected]="node.isSelected">        <​span> <​i nz-icon [nzType]="node.isExpanded ? 'folder-open' : 'folder'"><​/i> {{ node.title }} <​/span>      <​/span>    <​/ng-template>  `,})export class FormTreeSelectCustomizedIconComponent implements OnInit {  @ViewChild('customTpl', { static: true }) private customTpl!: TemplateRef<{ $implicit: NzTreeNode }>;  schema: SFSchema;  ngOnInit(): void {    this.schema = {      properties: {        status: {          type: 'string',          title: '基本',          enum: [            {              title: 'parent 1',              key: '100',              expanded: true,              icon: 'smile',              children: [                { title: 'leaf 1-0-0', key: '10010', icon: 'meh', isLeaf: true },                { title: 'leaf 1-0-1', key: '10011', icon: 'frown', isLeaf: true },              ],            },          ],          default: '10010',          ui: {            widget: 'tree-sel​ect',            treeTemplate: this.customTpl,          } as SFTreeSelectWidgetSchema,        },      },    };  }}

API

schema 属性

成员 说明 类型 默认值
[enum] 数据源 SFSchemaEnumType[] -
[readOnly] 禁用状态 boolean -

ui 属性

成员 说明 类型 默认值
[asyncData] 异步数据源 () => Observable<​SFSchemaEnumType[]> -
[size] 大小,等同 nzSize string default
[placeholder] 在文字框中显示提示讯息 string -
[notFoundContent] 当下拉列表为空时显示的内容 string -
[allowClear] 支持清除 boolean false
[dropdownMatchSelectWidth] 下拉菜单和选择器同宽 boolean true
[dropdownStyle] 下拉菜单的 style 属性 object -
[dropdownClassName] 下拉菜单的 className 属性 string -
[multiple] 支持多选(当设置 checkable 时自动变为true) boolean false
[hideUnMatched] 搜索隐藏未匹配的节点 boolean false
[checkable] 节点前添加 Checkbox 复选框 boolean false
[checkStrictly] checkable 状态下节点选择完全受控(父子节点选中状态不再关联) boolean false
[showIcon] 是否展示 TreeNode title 前的图标,没有默认样式 boolean false
[showExpand] 节点前添加展开图标 boolean true
[showLine] 节点前添加展开图标 boolean false
[defaultExpandAll] 默认展开所有树节点 boolean false

| [displayWith] | 如何在输入框显示所选的节点值的方法 | (node: NzTreeNode) => string | undefined | (node: NzTreeNode) => node.title | |---|---|---|---|

[expandedKeys] 默认展开指定的树节点 string[] -
[maxTagCount] 最多显示多少个 tag number -
[maxTagPlaceholder] 隐藏 tag 时显示的内容 TemplateRef<{ $implicit: NzTreeNode[] }> -
[treeTemplate] 自定义节点 TemplateRef<{ $implicit: NzTreeNode; origin: NzTreeNodeOptions }> -
[expandChange] 点击展开树节点图标调用 (e: NzFormatEmitEvent) => Observable<​SFSchemaEnum[]> -

异步数据务必先指定初始化数据(使用 enum、asyncData 选其一),否则无法触发 expandChange。