Load values from an already saved Selectitem into a database on the screen

Asked

Viewed 1,383 times

2

I have a problem to load the selected value in <f:selectItem> by the user When render gets that way:

<option value="320">Coordenador Desenvolvimento</option>

I wanted the attribute to be set selected="selected"

Follow my xhtml:

<div class="span3">
    <h:selectOneMenu id="txttecnicosselecionaveis" required="true" 
                     requiredMessage="Selecione o tecnico responsável pelo cliente" 
                     binding="#{cadastradorCliente.txtTecnicosSelecionaveis}" disabled="#{cadastradorCliente.desabilitarCampos}" styleClass="span12">
        <f:selectItems value="#{cadastradorCliente.tecnicosSelecionaveis}" 
                       itemLabel="#{cliente.tecnico.nome}" itemValue="#{cliente.tecnico.id}"/>
    </h:selectOneMenu>
</div>

My MB where I recover the data

public String actSelecionarTabela()
{
    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession sessao = (HttpSession) context.getExternalContext().getSession(true);

    if (sessao != null) 
    {
        sessao = (HttpSession) context.getExternalContext().getSession(true);
        sessao.setAttribute("cliente", cliente);
    }

    txtCnpj.setValue(cliente.getCnpj());
    txtRazaoSocial.setValue(cliente.getRazaoSocial());
    txtNomeFantasia.setValue(cliente.getNomeFantasia());
    txtInscricaoEstadual.setValue(cliente.getInscricaoEstadual());
    txtSite.setValue(cliente.getSite());

    txtEndereco.setValue(cliente.getEndereco().getDescricao());
    txtResponsavel.setValue(cliente.getResponsavel().getNome());
    txtTelefone.setValue(cliente.getResponsavel().getContato().getTelefone());
    txtEmail.setValue(cliente.getResponsavel().getContato().getEmail());
    txtCemail.setValue(cliente.getResponsavel().getContato().getCemail());
    txtAtivo.setValue(cliente.isAtivo());       
    txtTecnicosSelecionaveis.setValue(cliente.getTecnico().getCodigo());
    txtGerentesSelecionaveis.setValue(cliente.getGerente().getCodigo());

When I return the data saved on the screen back so my selectItem inserir a descrição da imagem aqui

I would like to return already loaded the values that were selected the first time inserir a descrição da imagem aqui

Here is the result of database saved data inserir a descrição da imagem aqui

I’ve tried several ways and I can’t fix it, anyone has any idea?

  • "i wanted it to be set the attribute Selected="Selected" - I didn’t understand.

  • Example I have already registered in database with the id of selectItem, however I want to load on the screen back the data for the user to be able to edit, I can recover on screen all the data of the register less my selected item

  • 1

    How about posting a property to the bean that returns the selected item, and then setting the component’s "value" attribute pointing to this property <h:selectOneMenu value="#{cadastradorCliente.tecnicoSelecionado}">?

  • @Caffé my returns to xhtml are returning the correct value, the problem is when I click to select on the screen and trigger my actionListener "txtTecnicosSelectible.setValue(client.getTecnico().getCodigo());" It brings me the code that was saved it just doesn’t click on the screen :(

  • 1

    I imagine that the problem is not only display, but that you also could not yet save in the bank a selected item on the page (inserted in the bank by another means). Right?

  • With you, I save the id and name of the selected technician and manager , I can also change this data through the application. It just doesn’t display :(

  • "the problem is when I click to select on the screen and fires my actionListener..." Well, I really couldn’t understand the problem. Good luck there!

Show 2 more comments

1 answer

2


Missing include the property value in the selectOneMenu.

For example, if your Managed bean owns the property tecnicoResponsavel indicating the previously selected technician you can do something like this:

<h:selectOneMenu id="txttecnicosselecionaveis" required="true" 
                     requiredMessage="Selecione o tecnico responsável pelo cliente" 
                     binding="#{cadastradorCliente.txtTecnicosSelecionaveis}"
                     disabled="#{cadastradorCliente.desabilitarCampos}" 
                     <!-- Carrega id do tecnico responsavel -->
                     value="#{cadastradorCliente.tecnicoResponsavel.id}"
                     styleClass="span12">

In order for a certain item to be preselected value of selectOneMenu should match the itemValue of selectItems (in that case, #{cliente.tecnico.id})


Sources:

  1. Soen - JSF: default Selection for <f:selectItem> Within <h:selectOneMenu>
  2. Soen - JSF f:selectItems set item Selected.
  • was that same guy, I’m still crawling in development, thank you very much.

  • Whoa, thank the @Caffè, only saw his comment now.

  • @Caffé thanks a lot for the help I ended up forgetting you!.

  • @What Anthony meant was that this same suggestion I had already made in the comments, right at the beginning of our conversation. Success there!

  • @Caffé I’m sorry, I had not understood your suggestion, thank you very much.

  • @Caffe, can I have a question? yesterday I thought I had solved the problem I created a property(String) in my MB that receives the selected technician that comes from the bank after recording, I have already performed a debug and the variables are being filled, but at the time of rendering it does not appear in the Selectonemenu.

Show 1 more comment

Browser other questions tagged

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