19

I have a tree table and two buttons:

  • one for collapsing the tree and
  • the other for expanding,

but they don't work. At the backing bean I did root.setExpanded(true); and root.setExpanded(false); but it doesn't work.

<center>
    <p:treeTable value="#{roleMB.root}" var="roleTreeTableVar"
        binding="#{roleMB.treeTable}" id="roleTreeTable">
        <f:facet name="header">
            <center>
                <p:commandButton value="Réduire tout"
                    icon="ui-icon-folder-collapsed" style="font-size: 0.9em" 
                    actionListener="#{roleMB.expandAll}" 
                    update=":roleTreeTableForm:roleTreeTable"  />
                <p:spacer width="30px" />
                <p:commandButton value="Développer tout"
                    icon="ui-icon-folder-open" style="font-size: 0.9em"  
                    actionListener="#{roleMB.collapseAll}" 
                    update=":roleTreeTableForm:roleTreeTable"  />
                <p:spacer width="30px" />
            </center>
        </f:facet>

        <p:column style="width:150px">
            <f:facet name="header"> 
                Nom de Role 
            </f:facet>
            <h:outputText value="#{roleTreeTableVar.nom}" />
        </p:column>

        <p:column style="width:100px">
            <f:facet name="header"> 
                Id de role 
            </f:facet>
            <h:outputText value="#{roleTreeTableVar.id}" />
        </p:column>

        <p:column style="width:20px">
            <p:commandLink oncomplete="dlgAddRole.show()" value="Ajouter"
                update=":addRoleForm:selectRolesNamesId">
                <f:setPropertyActionListener value="#{roleTreeTableVar}"
                    target="#{roleMB.selectedRole}" />
            </p:commandLink>
            <p:commandLink oncomplete="delRole.show()" value="Supprimer">
                <f:setPropertyActionListener value="#{roleTreeTableVar}"
                    target="#{roleMB.selectedRole}" />
            </p:commandLink>
            <p:commandLink oncomplete="editRole.show()" value="Modifier">
                <f:setPropertyActionListener value="#{roleTreeTableVar}"
                    target="#{roleMB.selectedRole}" />
            </p:commandLink>
        </p:column>
    </p:treeTable>
</center>
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
BenMansourNizar
  • 1,558
  • 4
  • 21
  • 42

4 Answers4

20

This is recursive method for Collapse and Expande.

public void collapsingORexpanding(TreeNode n, boolean option) {
    if(n.getChildren().size() == 0) {
        n.setSelected(false);
    }
    else {
        for(TreeNode s: n.getChildren()) {
            collapsingORexpanding(s, option);
        }
        n.setExpanded(option);
        n.setSelected(false);
    }
}
  • The variable n is the node than you want to expand or colapse.
  • When The variable option is false, all children of the n is collapse, in the another case, is expanded
  • setSelected(false) indicate than this node isn't selected
Aritz
  • 30,971
  • 16
  • 136
  • 217
fperez
  • 482
  • 1
  • 5
  • 17
  • but where should one execute this method? in getSelection()? in setSelection()? in a listener? pls – Niko Mar 21 '14 at 11:22
  • in my case, i create a button up the treeTable, this button call method collapsingORexpanding(Treenode, true) if i wish colapse, remember this instruction (n.setSelected(false);) set all node no selected – fperez Mar 21 '14 at 15:03
  • 1
    It worked like a charm! It would be very useful if the primefaces TreeTable already had this method "built inside" – hbobenicio Jan 16 '15 at 20:56
8

You should set the expanded property to true not just for the root node but for all the children nodes as well.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
alpha2011
  • 365
  • 6
  • 18
5

A possible solution might look this:

view.xhtml
<h:form id="form">
    ...
    <p:commandButton update=":form:tree" actionListener="#{view.collapseAll}"
            icon="fa fa-minus" title="#{msg.collapseAll}"/>                             
    <p:commandButton update=":form:tree" actionListener="#{view.expandAll}"
            icon="fa fa-plus" title="#{msg.expandAll}"/>
    ...
    <p:treeTable id="tree" ...>
    ...
    </p:treeTable>
    ...
</h:form>
View.java
@Named
@ViewScoped
public class View implements Serializable {

    private TreeNode root;

    ...

    public void collapseAll() {
        setExpandedRecursively(root, false);
    }

    public void expandAll() {
        setExpandedRecursively(root, true);
    }

    private void setExpandedRecursively(final TreeNode node, final boolean expanded) {
        for (final TreeNode child : node.getChildren()) {
            setExpandedRecursively(child, expanded);
        }
        node.setExpanded(expanded);
    }
}
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
-1

For question: "how can this be realized?" In code...

public void reloadTree() {


    assocRoot = new DefaultTreeNode();
    assocRoot.setExpanded(true);

    TreeNode currentAssocNode;
    AbstractDocument parentAssoc = getParentDocAssociation();
    if(parentAssoc !=null) {
        TreeNode parentAssocNode = new DefaultTreeNode(parentAssoc);
        parentAssocNode.setExpanded(true);
....