TreeNodes.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { NzFormatEmitEvent, NzTreeNode, NzTreeNodeOptions } from 'ng-zorro-antd/tree';
  17. export class TreeNodes {
  18. activated!: NzTreeNode;
  19. request!: any[];
  20. nodes!: any[];
  21. checkable!: boolean;
  22. checkedKeys!: any[];
  23. selectedKeys!: any[];
  24. _rootNode!: any;
  25. constructor(checkable: boolean) {
  26. this.checkable = checkable;
  27. this.checkedKeys = [];
  28. this.selectedKeys = [];
  29. }
  30. init(treeAttrs: any) {
  31. this._rootNode = { title: treeAttrs.rootNode.title, key: treeAttrs.rootNode.key, expanded: true, isLeaf: false };
  32. this.request = treeAttrs.nodes;
  33. }
  34. build(): any[] {
  35. return this.buildTree(this._rootNode);
  36. }
  37. buildTree(rootNode: any): any[] {
  38. let treeNodes: any[] = [];
  39. for (let node of this.request) {
  40. if (node.key != rootNode.key && node.parentKey == rootNode.key) {
  41. let treeNode = { title: node.title, key: node.key, expanded: false, isLeaf: true };
  42. this.buildTree(treeNode);
  43. treeNodes.push(treeNode);
  44. rootNode.isLeaf = false;
  45. }
  46. }
  47. rootNode.children = treeNodes;
  48. return [rootNode];
  49. }
  50. }