Browse Source

fix: remove node and its children in TreeAdapter

pull/5376/head
Arman Ozak 6 years ago
parent
commit
6ebe0850ec
  1. 11
      npm/ng-packs/packages/components/tree/src/lib/utils/nz-tree-adapter.ts

11
npm/ng-packs/packages/components/tree/src/lib/utils/nz-tree-adapter.ts

@ -41,17 +41,15 @@ export class TreeAdapter<T extends BaseNode = BaseNode> {
return this.tree; return this.tree;
} }
handleDrop(node: TreeNode<T>) { handleDrop({ key, parentNode }: TreeNode<T>) {
const { key, parentNode } = node;
const index = this.list.findIndex(({ id }) => id === key); const index = this.list.findIndex(({ id }) => id === key);
this.list[index].parentId = parentNode ? parentNode.key : null; this.list[index].parentId = parentNode ? parentNode.key : null;
this.tree = createTreeFromList(this.list); this.tree = createTreeFromList(this.list);
} }
handleRemove(node: TreeNode<T>) { handleRemove({ key }: TreeNode<T>) {
const { key, parentNode } = node; this.tree = createTreeFromList(this.list.filter(item => item.id !== key));
this.list = this.list.filter(({ id, parentId }) => id !== key && parentId !== key); this.list = createListFromTree(this.tree);
this.tree = createTreeFromList(this.list);
} }
} }
@ -66,6 +64,7 @@ function createTreeFromList<T extends BaseNode>(list: T[]): TreeNode<T>[] {
const node = map.get(row.id); const node = map.get(row.id);
if (parentId) { if (parentId) {
const parent = map.get(parentId); const parent = map.get(parentId);
if (!parent) return;
parent.children.push(node); parent.children.push(node);
parent.isLeaf = false; parent.isLeaf = false;
} else { } else {

Loading…
Cancel
Save