0

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 ........");

}

}

neni
  • 813
  • 5
  • 16
  • 34

2 Answers2

1

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*/    />
Ahmet
  • 908
  • 1
  • 17
  • 26
1

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.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • Hi Matt, thank you for the reply. StyleClass is used to specify the node style (mp3Style, picture, etc...). But, my requirement is different. In a single selection tree, If we click on a tree node it will be highlighted in some color right (Ex: http://www.primefaces.org/showcase/ui/treeSelectionSingle.jsf). I want to refresh the tree, expand the parent and highlight the newly added tree node in blue colour(Treenode, not the data). – neni Apr 07 '11 at 10:02
  • To give clear picture: 1) I am having a singleSelect tree with context menu (add child, edit, delete). 2) When I click on add child, a dialog will get open which contains the required fields and a button(SAVE). 3) when I click on SAVE button, I am inserting the node to database and reloading the tree. I am loosing the selected node and tree is getting collapsed. 4) I have to refresh only the tree and highlight the node which is added freshly. – neni Apr 07 '11 at 10:12