tree-select 树选择

树型选择控件。

注意:

  • tree-select 的数据源必须包含 titlekey 键名

代码演示

tree-select 树选择 - 图1

基础样例

最简单的用法。

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

  1. import { Component } from '@angular/core';
  2. import { SFSchema, SFTreeSelectWidgetSchema } from '@delon/form';
  3. import { NzMessageService } from 'ng-zorro-antd/message';
  4. import { of } from 'rxjs';
  5. import { delay } from 'rxjs/operators';
  6. @Component({
  7. selector: 'form-tree-select-simple',
  8. template: `
  9. <sf [schema]="schema" (formSubmit)="submit($event)"></sf>
  10. `,
  11. })
  12. export class FormTreeSelectSimpleComponent {
  13. schema: SFSchema = {
  14. properties: {
  15. status1: {
  16. type: 'string',
  17. title: '基本',
  18. enum: [
  19. { title: '待支付', key: 'WAIT_BUYER_PAY' },
  20. { title: '已支付', key: 'TRADE_SUCCESS' },
  21. { title: '交易完成', key: 'TRADE_FINISHED' },
  22. ],
  23. default: 'WAIT_BUYER_PAY',
  24. ui: {
  25. widget: 'tree-select',
  26. } as SFTreeSelectWidgetSchema,
  27. },
  28. status2: {
  29. type: 'string',
  30. title: '多选',
  31. enum: [
  32. { title: '待支付', key: 'WAIT_BUYER_PAY' },
  33. { title: '已支付', key: 'TRADE_SUCCESS' },
  34. { title: '交易完成', key: 'TRADE_FINISHED' },
  35. ],
  36. default: ['WAIT_BUYER_PAY', 'TRADE_SUCCESS'],
  37. ui: {
  38. widget: 'tree-select',
  39. multiple: true,
  40. } as SFTreeSelectWidgetSchema,
  41. },
  42. status3: {
  43. type: 'string',
  44. title: '可勾选',
  45. default: ['WAIT_BUYER_PAY', 'TRADE_FINISHED'],
  46. ui: {
  47. widget: 'tree-select',
  48. checkable: true,
  49. asyncData: () =>
  50. of([
  51. { title: '待支付', key: 'WAIT_BUYER_PAY' },
  52. { title: '已支付', key: 'TRADE_SUCCESS' },
  53. { title: '交易完成', key: 'TRADE_FINISHED' },
  54. ]).pipe(delay(10)),
  55. } as SFTreeSelectWidgetSchema,
  56. },
  57. // 异步数据
  58. async: {
  59. type: 'string',
  60. title: 'Async',
  61. enum: [
  62. { title: '待支付', key: 'WAIT_BUYER_PAY' },
  63. { title: '已支付', key: 'TRADE_SUCCESS' },
  64. { title: '交易完成', key: 'TRADE_FINISHED' },
  65. ],
  66. ui: {
  67. widget: 'tree-select',
  68. expandChange: () => {
  69. return of([
  70. { title: '待支付', key: 'WAIT_BUYER_PAY' },
  71. { title: '已支付', key: 'TRADE_SUCCESS' },
  72. { title: '交易完成', key: 'TRADE_FINISHED' },
  73. ]).pipe(delay(10));
  74. },
  75. } as SFTreeSelectWidgetSchema,
  76. },
  77. },
  78. };
  79. constructor(public msg: NzMessageService) {}
  80. submit(value: any) {
  81. this.msg.success(JSON.stringify(value));
  82. }
  83. }

tree-select 树选择 - 图2

自定义图标

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

  1. import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
  2. import { SFSchema, SFTreeSelectWidgetSchema } from '@delon/form';
  3. import { NzTreeNode } from 'ng-zorro-antd/tree';
  4. @Component({
  5. selector: 'form-tree-select-customized-icon',
  6. template: `
  7. <sf *ngIf="schema" [schema]="schema"></sf>
  8. <ng-template #customTpl let-node>
  9. <span class="ant-tree-node-content-wrapper" [class.ant-tree-node-selected]="node.isSelected">
  10. <span> <i nz-icon [nzType]="node.isExpanded ? 'folder-open' : 'folder'"></i> {{ node.title }} </span>
  11. </span>
  12. </ng-template>
  13. `,
  14. })
  15. export class FormTreeSelectCustomizedIconComponent implements OnInit {
  16. @ViewChild('customTpl', { static: true }) private customTpl!: TemplateRef<{ $implicit: NzTreeNode }>;
  17. schema: SFSchema;
  18. ngOnInit(): void {
  19. this.schema = {
  20. properties: {
  21. status: {
  22. type: 'string',
  23. title: '基本',
  24. enum: [
  25. {
  26. title: 'parent 1',
  27. key: '100',
  28. expanded: true,
  29. icon: 'smile',
  30. children: [
  31. { title: 'leaf 1-0-0', key: '10010', icon: 'meh', isLeaf: true },
  32. { title: 'leaf 1-0-1', key: '10011', icon: 'frown', isLeaf: true },
  33. ],
  34. },
  35. ],
  36. default: '10010',
  37. ui: {
  38. widget: 'tree-select',
  39. treeTemplate: this.customTpl,
  40. } as SFTreeSelectWidgetSchema,
  41. },
  42. },
  43. };
  44. }
  45. }

API

schema 属性

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

ui 属性

成员说明类型默认值
[asyncData]异步数据源() => Observable<SFSchemaEnumType[]>-
[size]大小,等同 nzSizestringdefault
[placeholder]在文字框中显示提示讯息string-
[notFoundContent]当下拉列表为空时显示的内容string-
[allowClear]支持清除booleanfalse
[dropdownMatchSelectWidth]下拉菜单和选择器同宽booleantrue
[dropdownStyle]下拉菜单的 style 属性object-
[dropdownClassName]下拉菜单的 className 属性string-
[multiple]支持多选(当设置 checkable 时自动变为true)booleanfalse
[hideUnMatched]搜索隐藏未匹配的节点booleanfalse
[checkable]节点前添加 Checkbox 复选框booleanfalse
[checkStrictly]checkable 状态下节点选择完全受控(父子节点选中状态不再关联)booleanfalse
[showIcon]是否展示 TreeNode title 前的图标,没有默认样式booleanfalse
[showExpand]节点前添加展开图标booleantrue
[showLine]节点前添加展开图标booleanfalse
[defaultExpandAll]默认展开所有树节点booleanfalse
[displayWith]如何在输入框显示所选的节点值的方法(node: NzTreeNode) => string | undefined(node: NzTreeNode) => node.title
[expandedKeys]默认展开指定的树节点string[]-
[maxTagCount]最多显示多少个 tagnumber-
[maxTagPlaceholder]隐藏 tag 时显示的内容TemplateRef<{ $implicit: NzTreeNode[] }>-
[treeTemplate]自定义节点TemplateRef<{ $implicit: NzTreeNode; origin: NzTreeNodeOptions }>-
[expandChange]点击展开树节点图标调用(e: NzFormatEmitEvent) => Observable<SFSchemaEnum[]>-

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