Thread: Getting RadioGroup selection

  1. #1
    Join Date
    Sep 2008
    Posts
    311

    Default Getting RadioGroup selection

    I have a simple radio group that allows the user to choose "Yes" or "No". The GXT samples show how to set the group up but not how to get the selected value. In browsing the API doc, it appears that you should be able to ask the RadioGroup instance for it's value to tell which radio was selected. The method getValue() on RadioGroup returns the selected Radio instance. From that, it also appears that you should be able to ask the Radio instance for it's name. Therefore group.getValue().getName() should be able to give you the name of the selected Radio instance. However, it doesn't. Instead, it gives me the name of the RadioGroup. Why doesn't this work? What is the proper way of getting the selected value of the radio group if this isn't?

    Radio noRadio = new Radio();
    noRadio.setName("no");
    noRadio.setBoxLabel("No");
    noRadio.setValue(true);

    Radio yesRadio = new Radio();
    yesRadio.setName("yes");
    yesRadio.setBoxLabel("Yes");
    yesRadio.setValue(false);

    redundantServerRadioGroup = new RadioGroup("group");
    redundantServerRadioGroup.add(noRadio);
    redundantServerRadioGroup.add(yesRadio);
    redundantServerRadioGroup.setValue(noRadio);

  2. #2
    Join Date
    Dec 2008
    Location
    Munich, Germany
    Posts
    172
    Try this
    Code:
    redundantServerRadioGroup.addListener(Events.Change, new Listener<FieldEvent>() {
    
        public void handleEvent(FieldEvent event) {
            boolean answer = yesRadio.getValue();
            
            if (answer) {
                Info.display("", "yes");
            } else {
                Info.display("", "no");
            }
        }
    
    });

  3. #3
    Join Date
    Sep 2008
    Posts
    311
    Why do I need to check the value of the yesRadio radio button? If I had ten options or radio buttons in my radio group, would I need to check all ten of them? I would think that I should be able to ask the radio group itself for which radio button (in the group) is selected.

  4. #4
    sven is offline Ext GWT - Development Team sven is on a distinguished road 7,343 Posts
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    7,343
    Therefore group.getValue().getName() should be able to give you the name of the selected Radio instance. However, it doesn't. Instead, it gives me the name of the RadioGroup. Why doesn't this work? What is the proper way of getting the selected value of the radio group if this isn't?
    They are part of a group. In a group all have the same name (this is the group logic all browsers have). So if you call getName() on a radio being in a RadioGroup, it returns the groupname.

    Getting the current selected value would be:

    Code:
    group.getValue().getValue()
    The first getValue() returns the currently selected radio. The second returns the value of the currently selected radio.

  5. #5
    Join Date
    Dec 2008
    Location
    Munich, Germany
    Posts
    172
    Just a thought, I think that the following would have been a more 'natural' naming
    Code:
    group.getRadio().getValue();
    // or
    group.getSelectedRadio().getValue();

  6. #6
    sven is offline Ext GWT - Development Team sven is on a distinguished road 7,343 Posts
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    7,343
    RadioGroup extends MultiField. What should getValue than return for you? Now you can easily handle with the radio. If we only return the value there, you dont know which radio is really selected.

    Also, as this is a very well documented feature, i dont see any reason to do this change.

© 2006-2010 Ext JS, Inc.