Getting selected value of a SelectOneMenu
Asked Answered
T

1

5

I'm testing the component "SelectOneMenu" on a jsf page. I'm populating this component dinamically though my ManageBean (that will get all Animals from database).

I would like to know if is possible to see the user selected item of that "SelectOneMenu" (combobox), I'm trying with value="#{animalsManage.animalSelect}" but it is only called on the beginning of the page. Also, I'm using an inputText to see the value of the selected intem of the "SelectOneMenu".

What I'm doing wrong?

JSF:

    <body>
    <ui:component>
        <h:form>
                    <h:outputText value="Select one Mets File" />
                    <h:selectOneMenu id="combo" value="#{animalsManage.animalSelected}">
                        <f:selectItem itemLabel="Select..."  noSelectionOption="true"/>
                        <f:selectItems value="#{animalsManage.allAnimals}" />
                    </h:selectOneMenu>
                    <h:inputText id="textbox" value="#{animalsManage.animalSelected }" />
        </h:form>
    </ui:component>
</body>

ManageBean:

    @ManagedBean
    @ViewScoped
    public class AnimalsManage implements Serializable {

    @EJB
    private AnimalsFacadeREST animalsFacadeREST;
    private String animalSelected;
    private List< SelectItem> selectAnimals;

    public List<SelectItem> getAllAnimals() {
            List<Animals> al = animalsFacadeREST.findAll();
            selectAnimals = new ArrayList< SelectItem>();
            int i = 0;
            for (Animals animal: al) {
                selectAnimals.add(new SelectItem(i, animal.getName()));
                i++;
            }
            return selectAnimals;
    }

    public String getAnimalSelected() {
       return animalSelected;
    }

    public void setAnimalSelected(String animalSelected) {
        this.animalSelected = animalSelected;
    }
}
Tarton answered 12/2, 2013 at 1:47 Comment(3)
you can just add a f:ajax inside <h:selectOneMenu> with event as "change" and also provide a render attribute with value "@form". This can show your selected value in the textbox. Something like this <f:ajax event="change" render="@form"> . Give a try.Orlandoorlanta
What is #{fileManage.fileName}? By the way, a <h:selectOneMenu> renders a dropdown, not a combobox. Get your terms straight. Last but not least, performing business logic in a getter is Bad. For some hints on how to use <h:selectOneMenu> properly, check its wiki page (put your mouse on top of the [selectonemenu] tag which you placed on the question until a black box shows up and then click therein the info link).Trunks
Thanks hemanth it helped. Also, thanks BalusC I edited the text hope it is fine now.Tarton
B
12

There are many solutions to the presented problem. I present here two basic ideas.

  1. Server-side solution. Simply attach <f:ajax> tag inside your <h:selectOneMenu> to update selected values and rerender user's choice, like in

    <h:selectOneMenu id="combo" value="#{animalsManage.animalSelected}">
        <f:selectItem itemLabel="Select..."  noSelectionOption="true"/>
        <f:selectItems value="#{animalsManage.allAnimals}" />
        <f:ajax execute="combo" render="textbox" />
    </h:selectOneMenu>
    <h:inputText id="textbox" value="#{animalsManage.animalSelected }" />
    

    If you like, you may also do some custom logic with selected element in ajax listener by specifying listener="#{animalsManage.performCustomAjaxLogic}" of <f:ajax> tag.

  2. Client-side solution. Simply update element with id="textbox" on basic change event. So, if you use jQuery the solution will be

    $('#combo').change(function() {
        $('#textbox').val($('#combo').val());
    });
    

    Thought the client-side solution will bind only text value of your input component.

Brand answered 12/2, 2013 at 13:2 Comment(1)
Thanks it worked! I prefered to use the listener with ajax to get what I wanted. Also, I had a problem with mojarra not been defined what made to the server to stop and couldn't run my code.Tarton

© 2022 - 2024 — McMap. All rights reserved.