2016-03-30 5 views
1

Ich implementiere ein Baummodul mit PrimeFaces-Framework. Ich möchte den Wurzelknoten in vertikaler Ausrichtung zeigen. Ich habe herausgefunden, dass mit dem gleichen Code, Horizontal Ausrichtung zeigt seine Root-Knoten, aber Vertical Ausrichtung nicht. Warum das? Gibt es trotzdem den Wurzelknoten im vertikalen Modus?Primes TreeNode, Wurzelknoten in vertikaler Ausrichtung

Vertikal:

- Documents 
- Videos 
- Images 

Horizontal:

   ----------- Images ---------- Travel.gif 
       - 
---- Root Node ----------- Videos ---------- Play.avi 
       -     
       ----------- Documents ------- Expenses.doc 

Hier sind die Codes:

<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:p="http://primefaces.org/ui"> 
<h:head> 
<script name="jquery/jquery.js" library="primefaces"></script> 
</h:head> 
<h:form> 
<p:tree value="#{treeManagedBean.root}" var="node" dynamic="true" orientation="horizontal" 
      selectionMode="single" selection="#{treeManagedBean.singleSelectedTreeNode}"> 
    <p:treeNode expandedIcon="ui-icon ui-icon-folder-open" 
        collapsedIcon="ui-icon ui-icon-folder-collapsed"> 
     <h:outputText value="#{node}"/> 
    </p:treeNode> 
    <p:treeNode type="document" icon="ui-icon ui-icon-document"> 
     <h:outputText value="#{node}"/> 
    </p:treeNode> 
    <p:treeNode type="image" icon="ui-icon ui-icon-image"> 
     <h:outputText value="#{node}"/> 
    </p:treeNode>     
    <p:treeNode type="video" icon="ui-icon ui-icon-video"> 
     <h:outputText value="#{node}"/> 
    </p:treeNode> 
    <p:ajax event="select" listener="#{treeManagedBean.onNodeSelect}"></p:ajax> 
    <p:ajax event="unselect" listener="#{treeManagedBean.onNodeUnSelect}"></p:ajax> 
    <p:ajax event="expand" listener="#{treeManagedBean.onNodeExpand}"></p:ajax> 
    <p:ajax event="collapse" listener="#{treeManagedBean.onNodeCollapse}"></p:ajax>         
</p:tree> 

@ManagedBean 
@SessionScoped 
public class TreeManagedBean { 
// TreeNode instance 
private TreeNode root; 
private TreeNode singleSelectedTreeNode; 
private TreeNode [] multipleSelectedTreeNodes; 
private TreeNode [] checkboxSelectedTreeNodes; 

public TreeManagedBean(){ 
    // This is the root node, so it's data is root and its parent is null 
    this.root = new DefaultTreeNode("Root Node", null); 
    // Create documents node 
    TreeNode documents = new DefaultTreeNode("Documents", this.root); 
    // Create document node 
    TreeNode document01 = new DefaultTreeNode("document","Expenses.doc", documents); 
    // Create images node 
    TreeNode images = new DefaultTreeNode("Images", this.root); 
    // Create image node 
    TreeNode image01 = new DefaultTreeNode("image","Travel.gif", images); 
    // Create videos node 
    TreeNode videos = new DefaultTreeNode("Videos", this.root); 
    // Create video node 
    TreeNode video01 = new DefaultTreeNode("video","Play.avi", videos); 
} 

public TreeNode getRoot() { 
    return root; 
} 

public void setRoot(TreeNode root) { 
    this.root = root; 
} 

public TreeNode getSingleSelectedTreeNode() { 
    return singleSelectedTreeNode; 
} 

public void setSingleSelectedTreeNode(TreeNode singleSelectedTreeNode) { 
    this.singleSelectedTreeNode = singleSelectedTreeNode; 
} 

public TreeNode[] getMultipleSelectedTreeNodes() { 
    return multipleSelectedTreeNodes; 
} 

public void setMultipleSelectedTreeNodes(TreeNode[] multipleSelectedTreeNodes) { 
    this.multipleSelectedTreeNodes = multipleSelectedTreeNodes; 
} 

public TreeNode[] getCheckboxSelectedTreeNodes() { 
    return checkboxSelectedTreeNodes; 
} 

public void setCheckboxSelectedTreeNodes(TreeNode[] checkboxSelectedTreeNodes) { 
    this.checkboxSelectedTreeNodes = checkboxSelectedTreeNodes; 
} 

public void onNodeSelect(NodeSelectEvent event){ 
    System.out.println("Node Data ::"+event.getTreeNode().getData()+" :: Selected"); 
} 

public void onNodeUnSelect(NodeUnselectEvent event){ 
    System.out.println("Node Data ::"+event.getTreeNode().getData()+" :: UnSelected"); 
} 

public void onNodeExpand(NodeExpandEvent event){ 
    System.out.println("Node Data ::"+event.getTreeNode().getData()+" :: Expanded"); 
} 

public void onNodeCollapse(NodeCollapseEvent event){ 
    System.out.println("Node Data ::"+event.getTreeNode().getData()+" :: Collapsed"); 
} 

public String printSelectedNodes(){ 
    System.out.println("Single Selection Is :: "+this.singleSelectedTreeNode.getData()); 
    for(TreeNode n : this.multipleSelectedTreeNodes){ 
     System.out.println("Multiple Selection Are :: "+n.getData()); 
    } 
    for(TreeNode n : this.checkboxSelectedTreeNodes){ 
     System.out.println("CheckBox Selection Are :: "+n.getData()); 
    } 
    return ""; 
} 
} 

Antwort

2

Ich hatte dasselbe Problem in meinem Projekt auf Primefaces 5.2. Ich habe gerade gefälschte Wurzel mit Null-Wert und Null-Eltern hinzugefügt. So können Sie etwas tun:

this.root = new DefaultTreeNode(null, null); 
    TreeNode fakeRoot = new DefaultTreeNode("Root Node", root); 
    TreeNode documents = new DefaultTreeNode("Documents", fakeRoot); 
    TreeNode document01 = new DefaultTreeNode("document","Expenses.doc", documents); 
    TreeNode images = new DefaultTreeNode("Images", fakeRoot); 
    TreeNode image01 = new DefaultTreeNode("image","Travel.gif", images); 
    TreeNode videos = new DefaultTreeNode("Videos", fakeRoot); 
    TreeNode video01 = new DefaultTreeNode("video","Play.avi", videos); 

Und accordding zu Primefaces's sources Wurzelknoten nur für nicht vertikal Baum rendred, so dass Sie dieses Verhalten nicht ohne Bearbeitung Quellen ändern können:

... 
    boolean vertical = tree.getOrientation().equals("vertical"); 
    ... 
    if(!vertical && rowKey.equals("root")) { 
     encodeHorizontalTreeNodeChildren(context, tree, tree.getValue(), tree.getClientId(context), null, tree.isDynamic(), tree.isCheckboxSelection()); 
    } else { 
     ... 
     if(vertical) { 
       encodeTreeNodeChildren(context, tree, node, clientId, tree.isDynamic(), tree.isCheckboxSelection(), tree.isDroppable()); 
     } else { 
       encodeHorizontalTreeNodeChildren(context, tree, node, tree.getClientId(context), rowKey, tree.isDynamic(), tree.isCheckboxSelection()); 
     } 
    }