【问题标题】:How to get the root of a PrimeFaces tree node?如何获取 PrimeFaces 树节点的根?
【发布时间】:2018-03-05 22:50:03
【问题描述】:

我有一个从控制器获取 TreeNode 的 html 页面。

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:composition>
    <pa:panelTree styleClass="corps-grade-list"
        treeContent="#{affectationController.getDossierEnBrefAffectations()}"
        rendered="#{agentModele.estMigre}" />
</ui:composition>
</html>

这样,affectationController.getDossierEnBrefAffectations() 是一个返回此树节点的函数。我想得到这棵树的根节点。

我尝试使用 treeContent="${affectationControleur.consulterDossierEnBrefAffectations.children[0]}",因为 TreeNode 类具有函数 getChildren。但是它不是 EL 的正确语法。

【问题讨论】:

    标签: primefaces tree


    【解决方案1】:

    如果您尚未扩展DefaultTreeNode 类或实现接口,请执行此操作。现在您可以简单地将这个方法添加到您的树节点实现中:

    public TreeNode getRoot() {
      if (getParent() == null) {
        return this;
      }
      TreeNode root = getParent();
      while (root.getParent() != null) {
        root = root.getParent();
      }
      return root;
    }
    

    这允许您使用:#{bean.treeNode.root}

    如果您无法更改模型,您可以在 bean 中添加类似的内容:

    public TreeNode getRoot(TreeNode node) {
      if (node.getParent() == null) {
        return node;
      }
      TreeNode root = node.getParent();
      while (root.getParent() != null) {
        root = root.getParent();
      }
      return root;
    }
    

    这允许您使用:#{bean.getRoot(treeNode)}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多