更新数据后更新数据表

时间:2017-06-26 13:03:53

标签: java jsf primefaces xhtml

编辑后更新数据表中的列时遇到问题。 我使用对话框来编辑您想要的数据。您正在保存数据库中的所有内容,即问题 重新更新表格,字段正在使用新值更新, 只有一个没有更新,只是对话框中的selectOneMenu。 未更新的字段来自Lotação,其余字段正在运行。

我的桌子..

<h:head>
</h:head>
<h:body>
    <ui:composition template="/WEB-INF/template/template.xhtml">
        <ui:define name="TituloCorpo">Alterar Impressora</ui:define>
        <ui:define name="Corpo">

            <div align="center">
                <h:form id="form">

                    <!-- BOTÕES QUE GERA RELATÓRIOS EM PDF E CSV -->
                    <div style="width:2958px">

                        <h:commandLink>
                            <h:graphicImage value="/resources/img/icon_pdf.png" width="35" title="Relatório em PDF"/>
                            <p:dataExporter type="pdf" target="tab" fileName="impressoras" pageOnly="true"/>
                        </h:commandLink>

                        <h:commandLink>
                            <h:graphicImage value="/resources/img/icon_csv.png" width="35" title="Relatório em CSV"/>
                            <p:dataExporter type="csv" target="tab" fileName="impressoras" pageOnly="true" />
                        </h:commandLink>
                    </div>

                    <!-- SCRIPT QUE CRIA UMA TABELA -->
                    <p:dataTable id="tab" var="impressora" widgetVar="tab" value="#{impressoraMB.impressoras}" editable="true" reflow="true" style="width:1100px">

                        <p:column id="fabricante" headerText="Fabricante" filterBy="#{impressora.fabricante}" filterMatchMode="contains" style="width:170px">
                            <h:outputLabel value="#{impressora.fabricante}"/>
                        </p:column>

                        <p:column id="modelo" headerText="Modelo" filterBy="#{impressora.modelo}" filterMatchMode="contains" style="width:170px">
                            <h:outputLabel value="#{impressora.modelo}"/>
                        </p:column>

                        <p:column id="modeloCartucho" headerText="Modelo do Cartucho" filterBy="#{impressora.modeloCartucho}" filterMatchMode="contains" style="width:180px">
                            <h:outputLabel value="#{impressora.modeloCartucho}"/>
                        </p:column>

                        <p:column id="patrimonio" headerText="Patrimonio" filterBy="#{impressora.patrimonio}" filterMatchMode="contains" style="width:120px">
                            <h:outputLabel value="#{impressora.patrimonio}"/>
                        </p:column>

                        <p:column id="lotacao" headerText="Lotação" filterBy="#{impressora.nome}" filterMatchMode="contains" style="width:110px">
                            <h:outputLabel value="#{impressora.nome}"/>
                        </p:column>

                        <p:column headerText="Alterar" style="width:70px" exportable="false">
                            <p:commandButton update=":formAlterar:panelAlterar" icon="ui-icon-pencil" title="Alterar" style="height:35px;width:35px"
                            oncomplete="PF('alterarImpre').show()" ajax="true">
                                <f:setPropertyActionListener value="#{impressora}" target="#{impressoraMB.impressora}"/>
                            </p:commandButton>
                        </p:column>

                        <p:column headerText="Remover" style="width:90px" exportable="false">
                            <p:commandButton update=":formExcluir:panelExcluir" oncomplete="PF('excluirImpre').show()" icon="ui-icon-trash"
                            styleClass="btn  btn-small" style="height:35px;width:35px" title="Excluir">
                                <f:setPropertyActionListener value="#{impressora}" target="#{impressoraMB.impressora}"/>
                            </p:commandButton>
                        </p:column>
                    </p:dataTable>
                </h:form>
            </div>
            <!-- PAGINA COM A CAIXA DE DIALOGO -->
            <ui:include src="/panel.xhtml"/>
        </ui:define>
    </ui:composition>
</h:body>

我的对话..

<p:dialog id="editar" header="Altere os dados desejados" widgetVar="alterarImpre" width="600" location="center"
draggable="true" modal="true" responcive="true" showEffect="fade" hideEffect="fade">
    <h:form id="formAlterar">
        <h6 align="center"><i>*Para abrir o campo de edição, clique em cima do valor</i></h6>
        <br/>
        <h:panelGrid id="panelAlterar" columns="2" cellpadding="5" width="75%">
            <h:outputLabel for="fabricante" value="Fabricante:"/>
            <p:inplace id="fabricante" editor="true" emptyLabel="Me edite">
                <p:inputText value="#{impressoraMB.impressora.fabricante}" required="true" label="text"/>
            </p:inplace>

            <h:outputLabel for="modelo" value="Modelo:"/>
            <p:inplace id="modelo" editor="true" emptyLabel="Me edite">
                <p:inputText value="#{impressoraMB.impressora.modelo}" required="true" label="text"/>
            </p:inplace>

            <h:outputLabel for="modeloCartucho" value="Modelo do Cartucho: "/>
            <p:inplace id="modeloCartucho" editor="true" emptyLabel="Me edite">
                <h:inputText value="#{impressoraMB.impressora.modeloCartucho}" required="true" label="text"/>
            </p:inplace>

            <h:outputLabel for="patrimonio" value="Patrimonio"/>
            <p:inplace id="patrimonio" editor="true" emptyLabel="Me edite">
                <p:inputText value="#{impressoraMB.impressora.patrimonio}" required="true" label="text"/>
            </p:inplace>

            <h:outputLabel for="lotacoes" value="Lotações:"/>
            <h:panelGroup>
                <h:selectOneMenu id="lotacoes" value="#{impressoraMB.impressora.id_Lotacoes}" effect="fold" required="true"
                immediate="true" style="width:100%">
                    <f:selectItems value="#{lotacoesMB.listLotacoes}" var="item"
                    itemLabel="#{item.nome}" itemValue="#{item.id}"/>
                    <f:ajax execute="@this"/>
                </h:selectOneMenu>
            </h:panelGroup>
        </h:panelGrid>

        <br/>
        <div align="center">
            <h:panelGrid>
                <h:panelGroup>
                    <p:commandButton id="btnAlterar" value="Alterar" action="#{impressoraMB.alterar}" onclick="PF('alterarImpre').hide()"
                    oncomplete="PF('form').hide(); #{impressoraMB.impressora}" ajax="true" process="@form" title="Alterar">
                    <f:ajax execute="@all" render=":form:tab"/>
                    </p:commandButton>

                    <p:commandButton id="btnCancelar" value="Cancelar" onclick="PF('alterarImpre').hide()" title="Cancelar"/>
                </h:panelGroup>
            </h:panelGrid>
        </div>
    </h:form>
</p:dialog>

我的豆..

public class ImpressoraMB {

private Impressora impressora;
private ImpressoraDAO dao;
private List<Impressora> impressoras;

public ImpressoraMB() {
    impressora = new Impressora();
    impressoras = new ArrayList<Impressora>();
    dao = new ImpressoraDAO();
}

public List<Impressora> getImpressoras() {
    if (impressoras.size() == 0) {
        impressoras = dao.getImpressoras();
    }
    return impressoras;
}

public void adicionar() {
    dao.adicionar(impressora);
    impressora = new Impressora();
}

public void remover() {
    dao.remover(impressora);
    impressoras.remove(impressora);
    impressora = new Impressora();
}

public void alterar() {
    dao.alterar(impressora);
    impressora = new Impressora();
}

public void showMsgAdicionar() {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Impressora adicionada", "com sucesso!");
    RequestContext.getCurrentInstance().showMessageInDialog(message);
}

public Impressora getImpressora() {
    return impressora;
}
public void setImpressora(Impressora impressora) {
    this.impressora = impressora;
}

}

非常感谢你。

2 个答案:

答案 0 :(得分:0)

您必须在命令按钮或ajax调用中使用update属性。

<f:ajax execute="@all" update=":form:tab" render=":form:tab"/>

{{1}}

确保在支持的bean

中更新对象

答案 1 :(得分:0)

在您的对话框中

<h:selectOneMenu id="lotacoes" value="#{impressoraMB.impressora.id_Lotacoes}" effect="fold" 
    required="true" immediate="true" style="width:100%">

,而您的DataTable使用

<p:column id="lotacao" headerText="Lotação" filterBy="#{impressora.nome}" 
    filterMatchMode="contains" style="width:110px">
        <h:outputLabel value="#{impressora.nome}"/>
</p:column>

如果更改两个不同.xhtml文件之一的值,则应该具有预期结果