Alex Alex - 10 days ago 6
Java Question

Primefaces Tree: don't remove the node after drag and drop

Is it possible not to remove the node from the original PrimeFacesTree after dragging it? The default behaviour is that a node that was dragged and dropped in another place is removed. Can i prevent that from happening?

I'm using Primefaces 4.0

Answer

There isn't any premade attribute to duplicate node on dropEvent.

The solution is to add a listener to your <p:tree> element :

<p:tree listener="#{managingBean.onDragDrop}" />

Then you need to re-create node on initial location by duplicating it in your backbean method :

public void onDragDrop(TreeDragDropEvent event) {
    TreeNode dragNode = event.getDragNode();
    TreeNode dropNode = event.getDropNode();
    int dropIndex = event.getDropIndex();

    // Logic to repopulate initial Tree element
}

And don't forget to re-draw your tree