如何从支持 bean 突出显示 primefaces 树节点

发布于 2024-10-30 21:42:49 字数 1746 浏览 1 评论 0原文

我正在使用 primefaces 树组件。树有一个上下文菜单(添加节点、编辑节点、删除节点)。执行某些操作后,我需要刷新树,然后突出显示添加或编辑的节点。

这是我的代码。

index.xhtml

        <p:treeNode>
            <h:outputText value="#{node}" />
        </p:treeNode>
    </p:tree>
    <p:contextMenu for="pTree" id="cmenu">
        <p:menuitem value="Add topic as child" update="pTree, cmenu"
                    actionListener="#{treeBean.addChildNode}" />
         <p:menuitem value="Add topic Below" update="pTree, cmenu"
                    actionListener="#{treeBean.addTopicBelow}" />
         <p:menuitem value="Delete Topic" update="pTree, cmenu"
                    actionListener="#{treeBean.deleteNode}" />
    </p:contextMenu>

treeBean.java

公共类 TreeBean 实现 Serialized {

private TreeNode root;

public TreeBean() {
    root = new DefaultTreeNode("Root", null);
    // GET the root nodes first L0
    List<TracPojo> rootNodes = SearchDao.getRootNodes111();
    Iterator it = rootNodes.iterator();

    while (it.hasNext()) {

        TracPojo t1 = (TracPojo) it.next();

        String tid = t1.getTopicID();

        TreeNode node1 = new DefaultTreeNode(t1, root);

    }


}
 public TreeNode getRoot() {
    return root;
 }


public void addChildNode(ActionEvent actionEvent) 
{

    List record = NewSearchDao.getRecord(selectedNode);

    Iterator it = record.iterator();
    while (it.hasNext()) {
        Object[] record1 = (Object[]) it.next();
        setParentID_dlg((String) record1[0]);
        setSortIndex((Integer) record1[2]);
    }

}

public void saveChilddNode() {
    System.out.println("Save as Child Node ........");

}

}

I am working with primefaces tree component. There is a context menu for the tree (add a node, edit node, delete node). After performing some operation, I need to refresh the tree and then highlight the node added or edited.

This is my code.

index.xhtml

        <p:treeNode>
            <h:outputText value="#{node}" />
        </p:treeNode>
    </p:tree>
    <p:contextMenu for="pTree" id="cmenu">
        <p:menuitem value="Add topic as child" update="pTree, cmenu"
                    actionListener="#{treeBean.addChildNode}" />
         <p:menuitem value="Add topic Below" update="pTree, cmenu"
                    actionListener="#{treeBean.addTopicBelow}" />
         <p:menuitem value="Delete Topic" update="pTree, cmenu"
                    actionListener="#{treeBean.deleteNode}" />
    </p:contextMenu>

treeBean.java

public class TreeBean implements Serializable {

private TreeNode root;

public TreeBean() {
    root = new DefaultTreeNode("Root", null);
    // GET the root nodes first L0
    List<TracPojo> rootNodes = SearchDao.getRootNodes111();
    Iterator it = rootNodes.iterator();

    while (it.hasNext()) {

        TracPojo t1 = (TracPojo) it.next();

        String tid = t1.getTopicID();

        TreeNode node1 = new DefaultTreeNode(t1, root);

    }


}
 public TreeNode getRoot() {
    return root;
 }


public void addChildNode(ActionEvent actionEvent) 
{

    List record = NewSearchDao.getRecord(selectedNode);

    Iterator it = record.iterator();
    while (it.hasNext()) {
        Object[] record1 = (Object[]) it.next();
        setParentID_dlg((String) record1[0]);
        setSortIndex((Integer) record1[2]);
    }

}

public void saveChilddNode() {
    System.out.println("Save as Child Node ........");

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-11-06 21:42:49

Primefaces p:treeNode 有一个属性 styleClass。您可以从支持 bean 动态设置它。视图将如下所示:

<p:tree>
  <p:treeNode styleClass="#{treeBean.styleClass}">
    <h:outputText value="#{node}" />
  </p:treeNode>
</p:tree>

然后使用 get/set 方法将成员 styleClass 添加到您的 TreeBean,该方法返回表示样式类的字符串:

public class TreeBean implements Serializable {
  private String styleClass;
  ...
  public String getStyleClass() {
    // your style selection logic here
  }
  ...
}

不要忘记将样式类添加到您的 css。

Primefaces p:treeNode has an attribute styleClass. You could set this dynamically from your backing bean. The view would look like:

<p:tree>
  <p:treeNode styleClass="#{treeBean.styleClass}">
    <h:outputText value="#{node}" />
  </p:treeNode>
</p:tree>

Then add a member styleClass to your TreeBean with get/set method that returns a string representing the style class:

public class TreeBean implements Serializable {
  private String styleClass;
  ...
  public String getStyleClass() {
    // your style selection logic here
  }
  ...
}

Don't forget to add the style classes to your css.

不必在意 2024-11-06 21:42:49

除非您将声明为 selection="#{treeBean.selectedNode}" 的 selectedNode 设置为 null,否则它已被选中,您唯一要做的就是从触发元件;在你的情况下是:

<p:menuitem update=":yourForm:pTree"   /*rest of the stuff*/    />

Unless you set the selectedNode, which you declare as selection="#{treeBean.selectedNode}", to null, it is already selected and the only thing you have to do is to update the tree component from the triggering component; in your case it is:

<p:menuitem update=":yourForm:pTree"   /*rest of the stuff*/    />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文