Dhruv
Dhruv

Reputation: 10693

Different Context menu on each node of tree

In my JSF project I want to display different context menu on every node of tree based on some conditions (To be precise some permissions)

Present according to my xhtml, I have binded context menu with the tree so I am getting same menu on every node of the tree. Here is the code:

<p:contextMenu for="TreeID">
    <p:menuitem value="Create" update=":centerPanel" actionListener="#{someBean.createPrivilege}" onstart="statusDialog.show();"
        oncomplete="statusDialog.hide();" />
    <p:menuitem value="Edit" update=":commonDialog :centerPanel" actionListener="#{someBean.editPrivilege}"
        onstart="statusDialog.show();" oncomplete="statusDialog.hide();" />
    <p:menuitem value="Delete" onstart="delPrivilegeConfirmDialog.show();" />
</p:contextMenu>
<p:scrollPanel mode="native" styleClass="scroll-panel">
    <p:tree id="TreeID" value="root" var="node" selectionMode="single"
        selection="#{someBean.selectedNode}" dynamic="true">
        <p:ajax listener="#{someBean.onNodeSelect}" update=":centerPanel" event="select" onstart="statusDialog.show();"
            oncomplete="statusDialog.hide();" />
        <p:treeNode id="someID">
            <h:outputText value="#{node}" id="lblNode" />
        </p:treeNode>
    </p:tree>
</p:scrollPanel>

But according to my requirement I want different context menu on every node, basically I have 3 options in my context menu like Create, Edit , Delete.. then I need to hide 1 or 2 option on every node based on certain conditions.

How would I do that?

Thanks in advance.

Upvotes: 2

Views: 4028

Answers (1)

Pascal Kesseli
Pascal Kesseli

Reputation: 1670

Assuming that you are using PrimeFaces, recent versions provide the option to set different context menus for different node types using the "nodeType" attribute:

<p:contextMenu for="TreeID" nodeType="type1">
    <p:menuitem value="Create" update=":centerPanel" actionListener="#{someBean.createPrivilege}" onstart="statusDialog.show();"
        oncomplete="statusDialog.hide();" />
    <p:menuitem value="Edit" update=":commonDialog :centerPanel" actionListener="#{someBean.editPrivilege}"
        onstart="statusDialog.show();" oncomplete="statusDialog.hide();" />
    <p:menuitem value="Delete" onstart="delPrivilegeConfirmDialog.show();" />
</p:contextMenu>
<p:contextMenu for="TreeID" nodeType="type2">
    <!-- Other menu items -->
</p:contextMenu>
<p:scrollPanel mode="native" styleClass="scroll-panel">
    <p:tree id="TreeID" value="root" var="node" selectionMode="single"
        selection="#{someBean.selectedNode}" dynamic="true">
        <p:ajax listener="#{someBean.onNodeSelect}" update=":centerPanel" event="select" onstart="statusDialog.show();"
            oncomplete="statusDialog.hide();" />
        <p:treeNode id="someID" type="type1">
            <h:outputText value="#{node}" id="lblNode" />
        </p:treeNode>
        <p:treeNode id="someID" type="type2">
            <h:outputText value="#{node}" id="lblNode" />
        </p:treeNode>
    </p:tree>
</p:scrollPanel>

Just keep in mind that you need to set the node type for all nodes generated by the model:

TreeNode x = new DefaultTreeNode("type1", data, parent);

Upvotes: 9

Related Questions