p:删除,更新或插入后,dataTable永远不会刷新

时间:2013-08-22 02:22:56

标签: jsf primefaces

好吧,我有一个插入新实体的p:对话框,但是在这个实体保存在数据库中之后p:dataTable继续等于,新行不会显示。

查看我的p:对话框

<!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的正确形式是什么。上面这个例子是一个插入,但我也有一个不起作用的删除。我使用了以下代码:

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

1 个答案:

答案 0 :(得分:0)

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

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

这将允许您重新加载表Feed列表。

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

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

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