Search code examples
jsfprimefacesnodestreetable

node treetable primefaces disappearing


I'm developing a application with Primefaces in Liferay, I need use p:treeTable compenent, happens that the nodes are dissapearing when click over p:commandButton. I belive it has relation with ajax, if put code within managedBean in @PostConstruct, it works normal

This is my .xhtml

<h:body>
        <h:form id="form">
            <p:layout style="min-width:1050px;height:700px" styleClass="semBorda">
                <p:layoutUnit position="west" size="900" minSize="40" maxSize="310"
                    styleClass="semBorda">

                    <p:panelGrid columns="2" style="width:300px">
                        <f:facet name="header">
                            <h:outputText value="Campos" />
                        </f:facet>

                        <h:outputText value="Cidade" />
                        <p:selectCheckboxMenu value="#{relatorioBean.selectedCidades}"
                            label="Cidades" filter="true">
                            <f:selectItems value="#{relatorioBean.cidades}" />
                        </p:selectCheckboxMenu>

                        <!--                            <h:outputText value="Ramal" /> -->
                        <!--                            <p:selectBooleanCheckbox value="" /> -->

                        <!--                            <h:outputText value="Tipo de Ramal" /> -->
                        <!--                            <p:selectBooleanCheckbox value="" /> -->

                        <!--                            <h:outputText value="Suborgao" /> -->
                        <!--                            <p:selectBooleanCheckbox value="" /> -->

                        <!--                            <h:outputText value="Situação" /> -->
                        <!--                            <p:selectBooleanCheckbox value="" /> -->

                        <h:outputText value="Polo" />
                        <p:selectBooleanCheckbox value="#{relatorioBean.polo}" />

                        <!--                            <h:outputText value="Categoria Diurna" /> -->
                        <!--                            <p:selectBooleanCheckbox value="" /> -->

                        <!--                            <h:outputText value="Categoria Noturna" /> -->
                        <!--                            <p:selectBooleanCheckbox value="" /> -->

                        <h:outputText value="Teste" />
                        <p:inputText value="#{relatorioBean.teste}" />
                    </p:panelGrid>


                    <p:panelGrid columns="1" style="margin-left:200px; margin-top:10px">

                        <p:commandButton actionListener="#{relatorioBean.montarRaiz}"
                            value="Consultar" update="tree" styleClass="semB"  />


                    </p:panelGrid>


                </p:layoutUnit>

                <p:layoutUnit position="center" styleClass="">

                    <p:treeTable value="#{relatorioBean.raiz}" var="ramal" id="tree">

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


                    </p:treeTable>

                </p:layoutUnit>
            </p:layout>
        </h:form>

    </h:body>

My managedBean

package br.jus.tjpe.infosistelecom.controle;

import br.jus.tjpe.infosistelecom.dao.OrgaoDao;
import br.jus.tjpe.infosistelecom.dao.RamalDao;
import br.jus.tjpe.infosistelecom.factory.OrgaoDaoFactory;
import br.jus.tjpe.infosistelecom.factory.RamalDaoFactory;
import br.jus.tjpe.infosistelecom.modelo.Ramal;

import java.io.Serializable;
import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

import org.primefaces.event.NodeCollapseEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@ManagedBean
@RequestScoped
public class RelatorioBean implements Serializable {

    private ArrayList<Ramal> ramais;
    private ArrayList<String> cidades;
    private String[] selectedCidades;
    private TreeNode root = new DefaultTreeNode("Root Node", null);; 
    private boolean polo;
    private String teste;

    public String getTeste() {
        return teste;
    }

    public void setTeste(String teste) {
        this.teste = teste;
    }

    public boolean getPolo() {
        return polo;
    }

    public void setPolo(boolean polo) {
        this.polo = polo;
    }

    public String[] getSelectedCidades() {
        return selectedCidades;
    }

    public void setSelectedCidades(String[] selectedCidades) {
        this.selectedCidades = selectedCidades;
    }

    public ArrayList<String> getCidades() {
        return cidades;
    }

    public void setCidades(ArrayList<String> cidades) {
        this.cidades = cidades;
    }

    public TreeNode getRaiz() {
        return root;
    }

    public void setRaiz(TreeNode raiz) {
        this.root = raiz;
    }

    public ArrayList<Ramal> getRamais() {
        return ramais;
    }

    public void setRamais(ArrayList<Ramal> ramais) {
        this.ramais = ramais;
    }

    @PostConstruct
    public void init() {

        cidades = new ArrayList<String>();
        OrgaoDao daoOrgao = OrgaoDaoFactory.createOrgaoDao();
        cidades = daoOrgao.listarCidades();

//      ramais = new ArrayList<Ramal>();
//      RamalDao daoRamal = RamalDaoFactory.createRamalDao();
//      ramais = daoRamal.listarTudo();

//      DefaultTreeNode no = new DefaultTreeNode("hgshdgs", this.root);
//      DefaultTreeNode no1 = new DefaultTreeNode("hgssdsdshdgs", this.root);
//      DefaultTreeNode no2 = new DefaultTreeNode("hgshdnjhsdghjdagjhggs", no1);
//      DefaultTreeNode no3 = new DefaultTreeNode("hgshdnjhsdghjdagjhggs", this.root);
//      DefaultTreeNode no24= new DefaultTreeNode("hgshdnjhsdghjdagjhggs", no3);



    }

    public void montarRaiz() {

        //System.out.println("diuyfuidyfuidyfuidy");

//      root = new DefaultTreeNode("Root Node", null);
//      ramais = new ArrayList<Ramal>();
//      RamalDao daoRamal = RamalDaoFactory.createRamalDao();
//      ramais = daoRamal.listarTudo();


//      DefaultTreeNode no = new DefaultTreeNode(ramais.get(0), raiz); 
//      DefaultTreeNode no1 = new DefaultTreeNode(ramais.get(1), raiz); 
//      
//      for (Ramal ramal : ramais) {
//          
//          new DefaultTreeNode(ramal, this.root);
//          
//      }
//  
        DefaultTreeNode no = new DefaultTreeNode("hgshdgs", this.root);
        DefaultTreeNode no1 = new DefaultTreeNode("hgssdsdshdgs", this.root);
        DefaultTreeNode no2 = new DefaultTreeNode("hgshdnjhsdghjdagjhggs", no1);
        DefaultTreeNode no3 = new DefaultTreeNode("hgshdnjhsdghjdagjhggs", this.root);
        DefaultTreeNode no24= new DefaultTreeNode("hgshdnjhsdghjdagjhggs", no3);


//      System.out.println(selectedCidades[0]);
//      System.out.println(selectedCidades[1]);


    }

}

I did try to resolve it, but without sucess.


Solution

  • Try changing the scope of your ManagedBean to @ViewScoped(BalusC discussing scopes) Since this bean is currently defined as @RequestScoped, a new instance will be created and destroyed with each request. I'm just guessing here, but when your commandButton POSTs via ajax, JSF realizes it must rebuild the ManagedBean during the "RESTORE_VIEW" phase, but this somehow doesn't actually call the constructor because it's handling an ajax request. Still, from what you've found, JSF must call @PostConstruct methods during this the "RESTORE_VIEW" phase regardless of if the request is ajax or not.

    In other words, the treeTable disappears from the page because it doesn't exist on the server side at that time.