在Primefaces中对树节点进行排序 [英] Sorting tree nodes in Primefaces

查看:118
本文介绍了在Primefaces中对树节点进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 2.1和Primefaces 3.3.我正在使用primefaces树组件从数据库中创建树.我想在所有级别上按字母顺序对树节点进行排序.请帮助我.

I am working with JSF 2.1 and Primefaces 3.3. I am using the primefaces tree component for crating the tree from Database. I wanted to sort the tree nodes in alphabetical order at all levels. Please help me on this.

推荐答案

您必须使用Collections.sort和Comparator类在ManagedBean上对Primefaces DefaultTreeNode对象进行排序.

You would have to sort Primefaces DefaultTreeNode objects at the ManagedBean using Collections.sort and a Comparator class.

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;

In your managed bean you will need to assemble your child lists without adding their parents yet. That can come later. For right now build your child lists out for each level and set the parent to 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);

我们将所有内容都设置为null的原因是,当在DefaultTreeNode上设置了父级时,会将其添加到父级子级列表中.设置节点父级的顺序决定了它们在树"组件中的显示顺序.

The reason why we are setting everything to null is because when the parent is set on the DefaultTreeNode it is added to the parents child list. The order in which you set a nodes parents determines the order they will appear in the Tree component.

知道我们可以使用比较器对每个列表进行单独排序.

Knowing that we can use our comparator to sort each list individually.

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

现在所有列表都已排序,因此我们可以一次遍历并找到合适的父母.您可能可以编写一种算法来确定这一点,或者可以保留一个单独的数据结构来构建树的层次结构,而无需添加到列表中.

Now all of the lists are sorted so we can loop through and the appropriate parents one at a time. You can probably write an algorithm to determine this or you can keep a separate data stucture that builds the tree hierarchy without adding to the list.

另一种方法(可能总体上更容易)是重写DefaultTreeNode类并为其提供一种排序方法:

Another way, and probably easier overall, is to just override the DefaultTreeNode class and give it a sort method:

public SortableDefaultTreeNode extends DefaultTreeNode {

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

现在,您可以先构建TreeNode,然后调用root.sort(),它将按字母顺序递归地对每个级别的所有子级进行排序.

Now you can just build your TreeNodes out and then call root.sort() and it will recursively sort all of its children at each level alphabetically.

这篇关于在Primefaces中对树节点进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆