Search code examples
jsfjsf-2primefacestreetable

JSF Primefaces treetable expand not working when updated through ajax


In my first page I show a command button which loads a treetable.

The treetable is displayed but expand node is not working.

I am using primefaces 4.0, jsf 2.2

Here is my code,

First page backing bean

@ManagedBean(name="loadBean")
public class AjaxLoadBean {
private boolean show;
private String currentPage;

public boolean isShow() {
    return show;
}

public void setShow(boolean show) {
    this.show = show;
}

public String getCurrentPage() {
    return currentPage;
}

public void setCurrentPage(String currentPage) {
    this.currentPage = currentPage;
}

public void newPage() {
    show = true;
    currentPage = "treeTable.xhtml";
}
}

first page

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">

<h:head></h:head>
<h:body>
<h:form>
    <p:commandButton value="Show" actionListener="#{loadBean.newPage}"
        update=":content" />
</h:form>

<h:panelGroup id="content">
    <h:panelGroup rendered="#{loadBean.show}">
        <ui:include src="#{loadBean.currentPage}"></ui:include>
    </h:panelGroup>
</h:panelGroup>
</h:body>
</html>

treeTable.xhtml

I stripped the ui:composition tag,

<h:form styleClass="form-horizontal">
<p:treeTable value="#{treeTableBean.root}" var="dataElement"
                id="table">
<p:column headerText="No.">
    <h:outputText value="#{dataElement.no}" />
</p:column>

<p:column headerText="Name">
    <h:outputText value="#{dataElement.name}" />
</p:column>

<p:column headerText="select">
    <p:selectBooleanCheckbox value="#{dataElement.select}" />
</p:column>
</p:treeTable>
</h:form>

TreeTableBean

@ManagedBean
public class TreeTableBean implements Serializable{
private static final long serialVersionUID = 1L;
private TreeNode root;

public TreeTableBean() {
    root = new DefaultTreeNode("root", null);

    new DefaultTreeNode(new TreeTableData(2, "N2", true), root);
    new DefaultTreeNode(new TreeTableData(3, "N3", true), root);
    new DefaultTreeNode(new TreeTableData(4, "N4", true), root);

    TreeNode subNode = new DefaultTreeNode(new TreeTableData(5, "N5", true), root);
    new DefaultTreeNode(new TreeTableData(1, "N5.1", true), subNode);
    new DefaultTreeNode(new TreeTableData(2, "N5.2", false), subNode);
    subNode.setExpanded(true);
}

public TreeNode getRoot() {
    return root;
}
}

Solution

  • I changed both the beans to ViewScoped and it is working.

    Viewscoped is the minimum that is needed to support ajax calls in components like datatable, tree, etc

    I posted in primefaces forums and got that info.

    http://forum.primefaces.org/viewtopic.php?f=3&t=35620#p113774