I have the simple model where trying to update the dataTable after fetch some results in my data table, I'm using EJB to fetch this result in my data base.
The problem is, after the search, my dataTable won't update unless that click over the search button 2 times or type something into fieldText filter and after clear it.
I tried put some function as "onclick and update" but all solutions not work.
A important case situation is: if the fieldText filter is deleted of my dataTble, the dataTable is update perfectly whithout erros :).
Someone can say another way or direction to solve my problem?
xhtml code:
<h:form id="frmPesquisa">
<p:panelGrid columns="3">
<h:outputLabel value="Pesquisar" for="pesquisa" />
<h:inputText id="pesquisa"
value="#{cadastroClienteBean.valorPesquisa}" />
<p:commandButton value="Pesquisar"
action="#{cadastroClienteBean.pesquisar}" update="tableResult"
onclick="PF('itemListases').clearFilters(); PF('itemListases').filter();"/>
</p:panelGrid>
<br />
<br />
<br />
<p:dataTable rendered="#{cadastroClienteBean.listaVazia()}" paginator="true" rows="5" widgetVar="itemListases"
emptyMessage="Não foram encontrados Itens"
filteredValue="#{cadastroClienteBean.filtrados}" id="tableResult"
value="#{cadastroClienteBean.clientes}" var="cliente" border="1"
cellpadding="5">
<p:column headerText="Código" filterBy="#{cliente.codigo}">
<h:outputText value="#{cliente.codigo}" />
</p:column>
<p:column headerText="Nome">
<h:outputText value="#{cliente.nome}" />
</p:column>
<p:column headerText="Idade">
<h:outputText value="#{cliente.idade}" />
</p:column>
<p:column headerText="Sexo">
<h:outputText value="#{cliente.sexo}" />
</p:column>
<p:column headerText="Profissão">
<h:outputText value="#{cliente.profissao}" />
</p:column>
</p:dataTable>
</h:form>
Bean code:
@Named
@SessionScoped
public class CadastroClienteBean implements Serializable {
private static final long serialVersionUID = 1L;
private Cliente cliente;
private List<Cliente> clientes;
private List<Cliente> filtrados;
private String valorPesquisa;
private String pesquisaAnterior;
@EJB
CadastroClienteEJB cadastroClienteEJB;
public CadastroClienteBean() {
System.out.println("===> Chamou o CONSTRUTOR");
cliente = new Cliente();
clientes = new ArrayList<Cliente>();
filtrados = new ArrayList<Cliente>();
}
public void pesquisar() {
if (!valorPesquisa.equals(pesquisaAnterior)) {
filtrados = new ArrayList<Cliente>();
if (this.valorPesquisa == null || this.valorPesquisa.equals("")) {
pesquisaAnterior = new String(valorPesquisa);
this.clientes = cadastroClienteEJB.buscarTodos();
} else {
pesquisaAnterior = new String(valorPesquisa);
this.clientes = this.cadastroClienteEJB
.pesquisar(this.valorPesquisa);
}
}
}