【问题标题】:p:dataTable never refreshes after a delete, update or insertp:dataTable 在删除、更新或插入后从不刷新
【发布时间】:2013-08-22 02:22:56
【问题描述】:

好吧,我有一个插入新实体的 p:dialog,但是在将该实体保存到数据库后 p:dataTable 保持不变,新行不会出现。

看看我的 p:dialog:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head>

</h:head>
<h:body>
    <ui:composition>
        <h:form id="formCadastrar">

            <p:dialog width="900px" height="500px" header="Cadastrar Dentista"
                widgetVar="dialogCadastrar" modal="true">

                <p:panelGrid columns="2" styleClass="semBorda">

                    <p:commandButton icon="ui-icon-disk" value="Salvar"
                        actionListener="#{dentistaMB.salvar}"
                        update=":formDentistas:dataTableDentistas" />

                    <p:commandButton value="Cancelar" icon="ui-icon-close"
                        onclick="confirmCancelar.show()" type="button" />

                </p:panelGrid>


                <p:panelGrid id="panelGridCadastar" styleClass="semBorda"
                    columns="2">
                    <h:outputText value="Nome: *" />
                    <p:inputText id="nome"
                        value="#{dentistaMB.dentista.pessoaFisica.nome}" size="40"
                        required="true" requiredMessage="O Nome do Dentista é obrigatório">
                    </p:inputText>

                    <h:outputText value="CRO *" />
                    <p:inputText id="cro" value="#{dentistaMB.dentista.cro}" size="10"
                        required="true" requiredMessage="O Número do CRO é obrigatório" />
                </p:panelGrid>

                <p:panelGrid columns="2" styleClass="semBorda">
                    <p:commandButton icon="ui-icon-disk" value="Salvar"
                        actionListener="#{dentistaMB.salvar}"
                        update=":formDentistas:dataTableDentistas" />

                    <p:commandButton value="Cancelar" icon="ui-icon-close"
                        onclick="confirmCancelar.show()" />
                </p:panelGrid>

                <p:confirmDialog id="confirmCancelar" message="Deseja cancelar ?"
                    showEffect="fade" hideEffect="fade" header="Cancelar"
                    severity="alert" widgetVar="confirmCancelar" appendToBody="true">
                    <p:commandButton value="Sim" oncomplete="confirmCancelar.hide()"
                        actionListener="#{dentistaMB.cancelar}" />
                    <p:commandButton value="Não" onclick="confirmCancelar.hide()"
                        type="button" />
                </p:confirmDialog>

            </p:dialog>

        </h:form>
    </ui:composition>


</h:body>
</html>

看看我的 p:dataTable

<h:form id="formDentistas">

                <p:growl autoUpdate="true" id="growlmessages" />

                <p:dataTable rowKey="#{dentista.id}" var="dentista" value="#{dentistaMB.dentistas}"
                    paginator="true" emptyMessage="Não foi encontrado nenhum registro"
                    rows="10" id="dataTableDentistas"
                    selection="#{dentistaMB.selectedDentista}" selectionMode="single">

                    <f:facet name="header">Lista de Dentistas</f:facet>
                    <p:column headerText="Nome" sortBy="nome" filterBy="nome" id="nome"
                        width="200px">
                    #{dentista.pessoaFisica.nome}
                </p:column>

                    <p:column headerText="Data Nascimento" sortBy="dataNascimento"
                        filterBy="dataNascimento" id="dataNascimento" width="60px">
                    #{dentista.pessoaFisica.dataNascimento}
                </p:column>

                    <p:column headerText="CRO" sortBy="cro" filterBy="cro" id="cro"
                        width="60px">
                    #{dentista.cro}
                </p:column>


                    <f:facet name="footer">
                        <div class="align_text_left">
                        <p:commandButton icon="ui-icon-plus" value="Novo" id="cadastrar"
                            oncomplete="dialogCadastrar.show()" />

                        <p:column headerText="Ações" style="width:50px;">
                            <p:commandButton value="Alterar" icon="ui-icon-pencil" />

                            <p:commandButton value="Remover" icon="ui-icon-trash"
                                action="#{dentistaMB.deletar}"
                                update=":formDentistas:dataTableDentistas" />
                        </p:column>
                        </div>
                    </f:facet>

                </p:dataTable>
            </h:form>

那么,在实体发生更改后更新 p:dataTable 的正确形式是什么?上面这个例子是关于一个 insert,但是 remove 也不起作用。我使用了以下代码:

<p:commandButton value="Remover" icon="ui-icon-trash"
                                action="#{dentistaMB.deletar}"
                                update=":formDentistas:dataTableDentistas" />

【问题讨论】:

  • 确保您的托管 bean 是 @ViewScoped 并且您使用 @PostConstruct 方法从数据库中检索数据。另外,不要忘记在插入/更新/删除操作后再次检索数据。
  • 我的错误,我忘记再次查找“牙医”列表,然后数据表永远不会更新。还是谢谢。

标签: jsf primefaces


【解决方案1】:

在您的对话框中,您必须添加一个 ajax 事件来触发表单更新,例如:

            <p:ajax event="close" listener="#{dentistaMB.load}"
            update=":formDentistas" immediate="true" global="false" />

这将允许您重新加载表格提要列表。

在您的加载方法中,您需要更新(从数据库重新获取数据)牙医列表。如果您正在使用过滤,请不要忘记更新您的过滤列表。

在 managedbean 加载函数中你可以使用:

    public void load(){
        dentistas.clear();
        filteredDentistas.clear();
        dentistas.addAll(getDentistService().getDentists());
        filteredDentistas.addAll(getDentistService().getDentists());
}

【讨论】:

    猜你喜欢
    • 2012-09-28
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 2018-07-02
    • 2011-03-11
    • 1970-01-01
    相关资源
    最近更新 更多