【发布时间】:2016-06-03 02:45:22
【问题描述】:
我有一个简单的模型,在我的数据表中获取一些结果后尝试更新 dataTable,我正在使用 EJB 在我的数据库中获取这个结果。
问题是,搜索后,我的数据表不会更新,除非单击搜索按钮 2 次或在 fieldText 过滤器中输入内容并清除它。
我尝试将一些功能设置为“onclick and update”,但所有解决方案都不起作用。
一个重要的情况是:如果我的dataTble中删除了fieldText过滤器,那么dataTable会完美更新,没有错误:)。
谁能说出解决我问题的其他方法或方向?
xhtml代码:
<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>
豆码:
@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);
}
}
}
【问题讨论】:
-
Do not use 和
update在具有rendered条件的元素上。当#{cadastroClienteBean.listaVazia()}评估为 false 时,您的表根本不会被搜索更新,因为在 DOM 树中不可用。相反,将您的表包装成h:panelGroup或ui:fragment并将更新应用到包装器组件。 -
非常感谢。它完美地工作。我删除了我的数据表的
render,并放了一个idtag。在我的commandButton中,我插入了一个update标记,将我的dataTable 的tagid 放入之前创建的。
标签: jsf primefaces datatable filtering ajax-update