How to update a form with ui:repeat without duplicating oc fields in jsf?

Asked

Viewed 286 times

2

I’m having a problem with my ui:repeat components, I want to update and not repeat the components, have as?

my form:

<h:body>
    <h:form >
        <h:outputLabel class="col-sm-2 col-sm-2 control-label" value="CEP"  />  
        <p:inputMask mask="99999-999" id="cep"
                     placeholder="#{clienteController.cliente.cep}" 
                     value="#{clienteController.cep}"  
                     maxlength="9" 
                     required="true" 
                     requiredMessage="Campo Obrigatório !"
                     /> 
        <p:spacer width="55" height="10" />
        <h:commandButton  id="Buscar_Dados" value="Buscar Dados" 
                          action="#{clienteController.buscarDadosCEP(clienteController.cep)}" >
            <f:ajax execute="@form"   render="panelCEP"/>
        </h:commandButton>
    </h:form>

    <h:form id="panelCEP" >
        <!--INICIO AUTOPREENCHIMENTO DO ENDERECO-->
        <h:panelGroup  >
            <ui:repeat  id="alterarClienteCEPDados" value="#{clienteController.enderecos}" var="dadosEnderecoAlterar" varStatus="status">
                <h:column >
                    <div class="form-group">
                        <h:outputLabel  value="Logradouro" class="col-sm-2 col-sm-2 control-label" />
                        <div class="col-sm-10">
                            <h:inputText value="#{dadosEnderecoAlterar.logradouro}" 
                                         class="form-control" />
                        </div>
                    </div>

                    <div class="form-group">
                        <h:outputLabel  value="Número" class="col-sm-2 col-sm-2 control-label" />

                    </div>

                    <div class="form-group">
                        <h:outputLabel  value="Bairro" class="col-sm-2 col-sm-2 control-label" />
                        <div class="col-sm-10">
                            <h:inputText value="#{dadosEnderecoAlterar.bairro}" class="form-control"  disabled="true"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <h:outputLabel  value="Cidade" class="col-sm-2 col-sm-2 control-label" />
                        <div class="col-sm-10">
                            <h:inputText value="#{dadosEnderecoAlterar.cidade}" class="form-control" disabled="true" />
                        </div>
                    </div>

                    <div class="form-group">
                        <h:outputLabel  value="Estado" class="col-sm-2 col-sm-2 control-label" />
                        <div class="col-sm-10">
                            <h:inputText value="#{dadosEnderecoAlterar.UF}" class="form-control" disabled="true" />
                        </div>
                    </div>
                </h:column>
            </ui:repeat>
        </h:panelGroup>
    </h:form>
</h:body>

My method with filling out the list:

public void buscarDadosCEP(String cep) throws Exception {

    Endereco dadosEndereco = new Endereco();
    cepService.getDadosCep(cep);
    if (cepService.getResultado() == 1) {
        cliente.setBairro(cepService.getBairro());
        cliente.setCidade(cepService.getMunicipio());
        cliente.setLogradouro(cepService.getLogradouro());
        cliente.setCep(cepService.getCep());
        cliente.setEstado(cepService.getUf());
        dadosEndereco.setBairro(cepService.getBairro());
        dadosEndereco.setCidade(cepService.getMunicipio());
        dadosEndereco.setCep(cepService.getCep());

        dadosEndereco.setLogradouro(cepService.getLogradouro());
        dadosEndereco.setUF(cepService.getUf());
        enderecos.add(dadosEndereco);
        enderecos.size();
        setCampoCepAlterar(true);

I put the zip code and click on the button to auto-fill, but when I click again it duplicates the fields, how do I not duplicate and update only that specific fields, without duplicate them?

1 answer

2


Just set a new address array in the method.

endereco = new ArrayList<>():

Browser other questions tagged

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