0
votes

I have a JSF application using Primefaces 3.5. I have a page where after click in a commandButton I call a method in Managed Bean which will fill a list that will be showed in fields tblPerfis and txtPerfis below.

<ui:composition template="...">
    <ui:define name="content">
        <h:form id="formPrincipal">
            <br />
            <p:fieldset legend="Pesquisa de Perfil" >
                <p:panelGrid columns="2" >
                    <f:facet name="footer">
                        <p:commandButton id="btnPesquisar"
                            actionListener="#{perfilAcessoMB.pesquisar}" value="Pesquisar"
                            update="tblPerfis txtPerfis pnlPerfis"  
                            styleClass="ui-icon-search" />
                    </f:facet>
                </p:panelGrid>
            </p:fieldset>
            <br />

            <h:outputText id="txtPerfis" value="Perfis: #{perfilAcessoMB.perfis}" ></h:outputText>
            <p:dataTable id="tblPerfis" value="#{perfilAcessoMB.perfis}" var="perfil" emptyMessage="Nenhum perfil encontrado." >
                <p:column headerText="Nome">
                    <h:outputText value="#{perfil.descricao}"></h:outputText>
                </p:column>
            </p:dataTable>
            <p:outputPanel id="pnlPerfis">
                <p:fieldset id="resultadoPesquisa" legend="Resultado da Pesquisa">TESTE</p:fieldset>
            </p:outputPanel>
        </h:form>
    </ui:define>
</ui:composition>

The called method is the follow:

@ManagedBean
@ViewScoped
public class PerfilAcessoMB {

    public void pesquisar(ActionEvent event) {
        // Fill perfis List
    }

}

At principle, it works as waited. My problem happens when I want add rendered attribute:

<h:outputText id="txtPerfis" value="Perfis: #{perfilAcessoMB.perfis}" rendered="#{not empty perfilAcessoMB.perfis}" ></h:outputText>
<p:dataTable id="tblPerfis" value="#{perfilAcessoMB.perfis}" rendered="#{not empty perfilAcessoMB.perfis}" var="perfil" emptyMessage="Nenhum perfil encontrado." >
    <p:column headerText="Nome">
        <h:outputText value="#{perfil.descricao}"></h:outputText>
    </p:column>
</p:dataTable>
<p:outputPanel id="pnlPerfis" rendered="#{not empty perfilAcessoMB.perfis}">
    <p:fieldset id="resultadoPesquisa" legend="Resultado da Pesquisa">TESTE</p:fieldset>
</p:outputPanel>

Even when there is results, these fields are not showed. Does someone have any idea what is happening here?

Thanks,

Rafael Afonso

EDIT

Following a workmate suggestion, I changed the code to put dataTable and outputText inside outputPanel. The commandButton will reference the outputPanel but the rendered attibute will be put in datatable and outputText.

<p:commandButton id="btnPesquisar"  
    actionListener="#{perfilAcessoMB.pesquisar}" value="Pesquisar"
    update="pnlPerfis"  
    styleClass="ui-icon-search" />

<p:outputPanel id="pnlPerfis">
    <h:outputText id="txtPerfis" value="Perfis: #{perfilAcessoMB.perfis}" rendered="#{not empty perfilAcessoMB.perfis}" ></h:outputText>
    <p:dataTable id="tblPerfis" value="#{perfilAcessoMB.perfis}" rendered="#{not empty perfilAcessoMB.perfis}" var="perfil" emptyMessage="Nenhum perfil encontrado." >
        <p:column headerText="Nome">
            <h:outputText value="#{perfil.descricao}"></h:outputText>
        </p:column>
    </p:dataTable>
</p:outputPanel>

After this, the page worked as waited.

However, I still did not understand what happened. What is the explanation for this behaviour?

1
Simply because the commandButton can't update a component that is not rendered in the first time (dataTable). - Alexandre Lavoie

1 Answers

1
votes

Your problem occurs because our table is not rendered it simply does not exist in your html code, that is not in this context to be found to update or another source.

When you shut a larger scope as the panel and force an update in this it forces the table to check the condition for rendering, if yes the code is written and can be seen without problems.