ng2-tree icon indicating copy to clipboard operation
ng2-tree copied to clipboard

moving a node to another node is triggering nodeRemoved and nodeMoved both

Open iamsunny opened this issue 7 years ago • 8 comments

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 avatar Jan 08 '18 20:01 iamsunny

@iamsunny Hi, Have you solved the problem? I get in same problem with you.

Dylanchouxd avatar Jan 24 '18 01:01 Dylanchouxd

@MIse3uc @iamsunny It was done like that by design. But I see the confusion and agree, that it should be fixed

rychkog avatar Feb 01 '18 11:02 rychkog

@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; } }

golddream-y avatar Apr 19 '18 12:04 golddream-y

Does anybody have a solution without changing the current library implementation?

akshaygolash avatar Dec 06 '18 23:12 akshaygolash

We removed the nodeRemoved event as it wasn't needed in our case.

iamsunny avatar Dec 10 '18 13:12 iamsunny

Just checked e.lastindex if '0' then we can consider it's moving ... other wise removing

ketan-shinde avatar Jul 22 '19 10:07 ketan-shinde

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!

nommac avatar Nov 19 '19 10:11 nommac

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);
    }

nommac avatar Nov 19 '19 17:11 nommac