2013-02-15 66 views
3

我正在使用JSF 2.1和Primefaces 3.3。我正在使用primefaces樹組件來從數據庫中創建樹。我想在所有級別按字母順序排列樹節點。請幫助我。排序Primefaces中的樹節點

回答

6

您必須使用Collections.sort和Comparator類對ManagedBean中的Primefaces DefaultTreeNode對象進行排序。

public TreeNodeComparator() implements Comparator<TreeNode> { 
    public int compare(TreeNode n1, TreeNode n2) { 
    // This assumes the tree node data is a string 
    return n1.getData().compareTo(n2.getData()); 
    } 
} 

在你的託管bean中,你將需要組裝你的子列表而不添加他們的父母。那可以晚一點。現在,爲每個級別建立你的子列表並將parent設置爲null;

TreeNode node1 = new DefaultTreeNode("node1", null); 
TreeNode node2 = new DefaultTreeNode("node2", null); 
TreeNode child1node1 = new DefaultTreeNode("zgnagn", null); 
TreeNode child2node1 = new DefaultTreeNode("vvnieeianag", null); 
TreeNode child1node2 = new DefaultTreeNode("cajkgnagair", null); 
TreeNode child2node2 = new DefaultTreeNode("ajaavnagwokd", null); 
rootNodeChildren.add(node1); 
rootNodeChildren.add(node2); 
node1Children.add(child1node1); 
node1Children.add(child2node1); 
node2Children.add(child1node2); 
node2Children.add(child2node2); 

爲什麼我們的一切設置爲空的原因是因爲當父被設置在DefaultTreeNode它被添加到父母的孩子名單。您設置父節點的順序決定了它們將出現在Tree組件中的順序。

知道我們可以使用我們的比較器來單獨排序每個列表。

Collections.sort(rootNodeChildren, new TreeNodeComparator()); 
Collections.sort(node1Children, new TreeNodeComparator()); 
Collections.sort(node2Children, new TreeNodeComparator()); 

現在所有的列表進行排序,所以我們可以通過循環及相應的父母一次一個。您可以編寫一個算法來確定這一點,或者您可以保留一個單獨的數據結構來構建樹層次結構,而無需添加到列表中。

另一種方式,並可能更容易整體,是隻覆蓋DefaultTreeNode類,並給它一個排序方法:

public SortableDefaultTreeNode extends DefaultTreeNode { 

    public void sort() { 
    TreeNodeComparator comparator = new TreeNodeComparator(); 
    Collections.sort(this.children, comparator); 
    for (TreeNode child : children) { 
     child.sort(); 
    } 
    } 
} 

現在,你可以建立自己的樹節點出來,然後調用root.sort(),它會遞歸排序按字母順序排列每個級別的所有孩子。

+0

謝謝maple_shaft。它拯救了我的一天。 – neni 2013-02-18 04:43:52

+0

@neni您的歡迎:) – 2013-02-18 11:55:27

0

你也可以使用一個通用的可比的樹節點的形式給出,如:

基地從primefaces DefaultTreeNode採取未修改的變化在下面的代碼冷落。

如果孩子不應該對T一個限制可以使用TreeNodeComparable<T extends Comparable<?>>compareTo()方法轉換爲Comparable

public class TreeNodeComparable<T extends Comparable<T>> implements TreeNode, Serializable, 
    Comparable<TreeNodeComparable<T>> 
{ 
    private static final long serialVersionUID = ...; 

    private T data; 

    private List<TreeNodeComparable<T>> children; 


    public TreeNodeComparable(final String type, final T data, final TreeNodeComparable<T> parent) 
    { 
     this.type = type; 
     this.data = data; 
     this.children = (List) new TreeNodeChildren(this); 
     if (parent != null) 
      parent.getChildren().add(this); 
    } 

    /** 
    * Comparison only depends on the underlying data 
    * 
    * @see ObjectUtils#compare(Comparable, Comparable) 
    */ 
    @Override 
    public int compareTo(final TreeNodeComparable<T> node) 
    { 
     if (node == null) 
      throw new NullPointerException("node"); 

     return ObjectUtils.compare((T) this.getData(), (T) node.getData()); 
    } 

    /** 
    * Recursively sorts the complete tree. 
    */ 
    public void sort() 
    { 
     Collections.sort(this.children); 
     for (final TreeNodeComparable<T> child : this.children) 
     { 
      child.sort(); 

      // must reset parent due to PF problems 
      // http://forum.primefaces.org/posting.php?mode=reply&f=3&t=39752 
      child.setParent(this); 
     } 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    public boolean equals(final Object obj) 
    { 
     if (this == obj) 
      return true; 
     if (obj == null || this.getClass() != obj.getClass()) 
      return false; 

     final TreeNodeComparable<T> other = (TreeNodeComparable<T>) obj; 

     return ObjectUtils.equals(this.data, other.data); 
    } 

    @Override 
    public int hashCode() 
    { 
     return new HashCodeBuilder().append(this.data).toHashCode(); 
    } 

    public void setData(final Object data) 
    { 
     if (data != null && !(data instanceof Comparable)) 
      throw new IllegalArgumentException(); 
     this.data = (T) data; 
    } 

    @SuppressWarnings(
    { 
     "unchecked", "rawtypes" 
    }) 
    public List<TreeNode> getChildren() 
    { 
     return (List) this.children; 
    } 
}