Search code examples
jsfjsf-2primefacestreeinplace-editing

Inplace inside Tree : how to save the modified TreeNode?


I'm using Primefaces 4.0 and I have a <p:tree> with editable node labels :

<p:tree id="treeSingle" dynamic="true"
    value="#{treeController.treeRoot}" var="node"
    selectionMode="single" selection="#{treeController.selectedNode}">
  <p:treeNode>
    <p:inplace editor="true" event="dblclick">
      <p:ajax event="save" listener="#{treeController.onSaveNodeLabel}" update="@this" />
      <p:inputText value="#{node.label}" />
    </p:inplace>
  </p:treeNode>
</p:tree>

What I want to do is saving the #{node.label} value when it is modified by the user.

The listener="#{treeController.onSaveNodeLabel}" calls a method which should save the modified TreeNode label. So I have created this method in my treeController class :

public void onSaveNodeLabel(AjaxBehaviorEvent event) {
    logger.info("onSaveNodeLabel");
    Inplace inplace = (Inplace)event.getSource();
    UITreeNode uiTreeNode = (UITreeNode) inplace.getParent();
    // What next?
}

Problem : the AjaxBehaviorEvent allows me to access the org.primefaces.component.tree.UITreeNode but not the org.primefaces.model.TreeNode which holds my data. Is there a solution to get to TreeNode from AjaxBehaviorEvent? Thanks!


Solution

  • Just send the node.label as a parameter to your function:

    #{treeController.onSaveNodeLabel(node.label)}

    So, in your web page:

    <p:inplace editor="true" event="dblclick">
      <p:ajax event="save" listener="#{treeController.onSaveNodeLabel(node.label)}" update="@this" />
      <p:inputText value="#{node.label}" />
    </p:inplace>
    

    And in your bean:

    public void onSaveNodeLabel(String nodeLabel) {
        //
    }