防止将节点拖动到另一个级别

时间:2018-02-21 15:41:00

标签: primefaces jsf-2 tree

我想阻止将节点拖到另一个级别。例如,节点“节点0.0”只能替换为节点0,节点1,节点2,不能替换为节点0.0,节点0.1,节点1.0,节点1.1或根节点。

我尝试比较药物节点和dropnode的父数据,onDragDrop方法中的点数,但是如果它们不相等则如何回滚操作?

<h:form id="form">
    <p:tree id="tree1" value="#{dDropView.root1}" var="node" selectionMode="single" selection="#{dDropView.selectedNode1}"
            draggable="false" droppable="true" dragdropScope="demo" >
        <p:ajax event="dragdrop" listener="#{dDropView.onDragDrop}" />
        <p:treeNode>
            <h:outputText value="#{node}" />
        </p:treeNode>
    </p:tree>
</h:form>


@ManagedBean(name="dDropView")
@ViewScoped
public class DDropView implements Serializable {
    private TreeNode root1;

    private TreeNode selectedNode1;

    @PostConstruct
    public void init() {
        root1 = new DefaultTreeNode("Root", null);
        TreeNode node0 = new DefaultTreeNode("Node 0", root1);
        TreeNode node1 = new DefaultTreeNode("Node 1", root1);
        TreeNode node2 = new DefaultTreeNode("Node 2", root1);

        TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);
        TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);

        TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);
        TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);
    }

    public TreeNode getRoot1() {
        return root1;
    }

    public TreeNode getSelectedNode1() {
        return selectedNode1;
    }

    public void setSelectedNode1(TreeNode selectedNode1) {
        this.selectedNode1 = selectedNode1;
    }

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

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Dragged " + dragNode.getData(), "Dropped on " + dropNode.getData() + " at " + dropIndex);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

0 个答案:

没有答案