ng2-tree
ng2-tree copied to clipboard
moving a node to another node is triggering nodeRemoved and nodeMoved both
Hi,
moving a node from one parent to another is trigger "nodeRemoved" first, and then triggering - "nodeMoved".
"nodeRemoved" event is triggered when you remove a node or leaf and so I couldn't find a way to distinguish if the "nodeRemoved" has been triggered by moving a node or by removing a node.
Am I missing anything or it's just a bug?
Thanks!
@iamsunny Hi, Have you solved the problem? I get in same problem with you.
@MIse3uc @iamsunny It was done like that by design. But I see the confusion and agree, that it should be fixed
@iamsunny Sorry, my English is very poor. So I write the code directly. This is just a temporary solution.
ng2-tree (version:v2.0.0-rc.11)
tree.types.ts (add code)
export enum TreeAction {
Move,
........
}
tree.service.ts (line:52)
public fireNodeRemoved(tree: Tree, action?: TreeAction): void {
this.nodeRemoved$.next(new NodeRemovedEvent(tree, tree.positionInParent, action));
}
tree.events.ts(line:33)
export class NodeRemovedEvent extends NodeDestructiveEvent {
public action: TreeAction;
public constructor(node: Tree, public lastIndex: number, action?: TreeAction) {
super(node);
this.action = action;
}
}
in your project
public handleRemoved(e: NodeRemovedEvent): void {
if (e.action === TreeAction.Move) {
return;
}
}
Does anybody have a solution without changing the current library implementation?
We removed the nodeRemoved event as it wasn't needed in our case.
Just checked e.lastindex if '0' then we can consider it's moving ... other wise removing
Just checked e.lastindex if '0' then we can consider it's moving ... other wise removing
Sorry not working for me. lastindex equal 0 in both cases.
¿Does someone find any other solution to this problem without changing source code? Thanks!
Finally I solved the problem using custom event. I'll paste my workaround that maybe can help somebody:
-
In html template remove event input to avoid triggering delete function
(nodeRemoved)="onNodeRemoved($event)"
-
in tree settings add a custom menú for delete instead the default menuitemaction "Remove":
`
menuItems: [
{ action: NodeMenuItemAction.Custom, name: 'remove', cssClass: 'remove' },
{ action: NodeMenuItemAction.Rename, name: 'rename', cssClass: 'rename' },
{ action: NodeMenuItemAction.Custom, name: 'download', cssClass: 'new-tag' },
] `
- Capture custom events and do delete actions there:
public onMenuItemSelected(e: MenuItemSelectedEvent) {
this.logEvent(e, `You selected ${e.selectedItem} menu item`);
if (e.selectedItem === 'remove') {
// Call onNodeRemoved(e) default function
this.onNodeRemoved(e);
}