【问题标题】:After search, p:dataTable won't update unless search is invoked twice or something into filter is typed and cleared搜索后,p:dataTable 不会更新,除非调用了两次搜索或输入并清除了过滤器中的某些内容
【发布时间】: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 useupdate 在具有 rendered 条件的元素上。当 #{cadastroClienteBean.listaVazia()} 评估为 false 时,您的表根本不会被搜索更新,因为在 DOM 树中不可用。相反,将您的表包装成 h:panelGroupui:fragment 并将更新应用到包装器组件。
  • 非常感谢。它完美地工作。我删除了我的数据表的render,并放了一个idtag。在我的commandButton 中,我插入了一个update 标记,将我的dataTable 的tag id 放入之前创建的。

标签: jsf primefaces datatable filtering ajax-update


【解决方案1】:

非常感谢。它完美地工作。我删除了我的数据表的渲染并放置了一个 id 标签。在我的 commandButton 中,我插入了一个更新标签,将我的 dataTable 的标签 ID 放入之前创建的标签中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2020-03-09
    • 2019-03-24
    • 2014-02-03
    相关资源
    最近更新 更多