dqt
dqt

Reputation: 65

Prevent nodes to be dropped on nodes of certain type in Primefaces tree

I have a tree and my code is:

<p:tree id="tree_newCms_pl"
        value="#..............."
        var="item"
        animate="true"
        selectionMode="single" selection=".............."
        dynamic="true"
        draggable="true" droppable="true">

enter image description here

I only want to allow to change the position of a node, and prevent it from being dropped onto a different parent. How can I fix this?

Upvotes: 1

Views: 4914

Answers (2)

Ashish Mathew
Ashish Mathew

Reputation: 823

This can be done using an attribute. dropRestrict="sibling".

<p:tree id="tree_newCms_pl"
        value="#..............."
        var="item"
        animate="true"
        selectionMode="single" selection=".............."
        dynamic="true"
        draggable="true" droppable="true" dropRestrict="sibling">

Description as per documentation

Defines parent-child restrictions when a node is dropped valid values are none (default) and sibling.

Upvotes: 3

user2075499
user2075499

Reputation: 93

I've resolved a similar issue by doing the following.

In the backing bean checking the type of the drop TreeNode in the event, and only updating the underlying data model if it's a valid target for the node being dragged.

TreeNode dragNode = event.getDragNode();
TreeNode dropNode = event.getDropNode();

if ( dropNode.getType().equals( VALID_TYPE ) )
{
   //Update the underlying data structure here
}
else
{
  //Display a warning to the user if required
}

I'm also in the listener event updating the tree so it's redrawn from the data model, e.g.

<p:ajax event="dragdrop" listener="#{managingBean.onDragDrop}" update="tree_newCms_p"/>

If you don't redraw the tree, it'll show the element in the wrong position, even no underlying move has been performed.

Upvotes: 3

Related Questions