Browse Source

Merge pull request #5406 from abpframework/feat/5405

feat: add handleUpdate to tree adapter
pull/5416/head
Bunyamin Coskuner 6 years ago
committed by GitHub
parent
commit
3b5b10808e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      npm/ng-packs/packages/components/tree/src/lib/utils/nz-tree-adapter.ts

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

@ -48,8 +48,26 @@ export class TreeAdapter<T extends BaseNode = BaseNode> {
} }
handleRemove({ key }: TreeNode<T>) { handleRemove({ key }: TreeNode<T>) {
this.tree = createTreeFromList(this.list.filter(item => item.id !== key)); this.updateTreeFromList(this.list.filter(item => item.id !== key));
}
handleUpdate({ key, children }: { key: string; children: T[] }) {
/**
* When we need to update a node with new children, first we need to remove any descendant nodes.
* If we remove immediate children and create a new tree, any other descendant nodes will be removed
* and we won't need to recursively remove sub children.
* Then, you simply add back the new children and create a new tree.
*/
const listWithDescendantNodesRemoved = this.updateTreeFromList(
this.list.filter(item => item.parentId !== key),
);
this.updateTreeFromList(listWithDescendantNodesRemoved.concat(children));
}
updateTreeFromList(list: T[]) {
this.tree = createTreeFromList(list);
this.list = createListFromTree(this.tree); this.list = createListFromTree(this.tree);
return this.list;
} }
} }

Loading…
Cancel
Save