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

余生长醉 提交于 2019-12-08 06:19:25

问题


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">

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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18999476/prevent-nodes-to-be-dropped-on-nodes-of-certain-type-in-primefaces-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!