17

我有一个树表和两个按钮:

  • 一个用于折叠树和
  • 另一个用于扩展,

但他们不工作。我在支持豆上做了root.setExpanded(true);root.setExpanded(false);但它不起作用。

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

4 回答 4

21

这是折叠和展开的递归方法。

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);
    }
}
  • 变量 n 是您想要展开或折叠的节点。
  • 当变量选项为 false 时,n 的所有子节点都折叠,在另一种情况下,展开
  • setSelected(false) 表示该节点未被选中
于 2012-10-11T14:35:03.153 回答
8

您应该将该expanded属性设置为true不仅适用于根节点,还应该适用于所有子节点。

于 2012-09-14T16:04:00.687 回答
4

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);
    }
}
于 2016-04-11T19:19:17.157 回答
-1

问题是:“这怎么可能实现?” 在代码中...

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);
....
于 2016-03-15T10:38:57.673 回答