Update in datatable after you have updated data

Asked

Viewed 478 times

0

Speak guys, I have a problem updating a column in my datatable after an edit. I use a dialog to edit the desired data. Everything is saving in the database, the problem is when updating the table, the fields are being updated with the new values, only one that is not updating, precisely from a selectOneMenu that is in the dialog. The field you’re not upgrading is Stocking, the rest is screwing up.

My table..

<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>

My dialog..

<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 render=":form"/>
                </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('tab').filter(); #{impressoraMB.impressora}" ajax="true" process="@this" title="Alterar">
                    </p:commandButton>

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

My Bean. 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;
}

}

From now on, thank you.

2 answers

1

Try to reload the page when you update onchange="window.location.Reload();" :

 <h:selectOneMenu id="lotacoes"  ... onchange="window.location.reload();">
   <f:selectItems ... />
   <f:ajax ... />
</h:selectOneMenu>
  • Thank you for the reply João Pedro, but when I select a value it already reloads the page, then does not change the values.

0


Problem solved. what was wrong was because in my database I was using printer.name and in my dialog I was using printer.id_Lotacoes, so in one I was using id and in the other the description.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.