Ajax radio buttons in Wicket 7

451 views Asked by At

I created a Panel in which Im trying to get live Ajax behaviour to some radio options.

The problem is that I can not set the current active option, in the setter "setSelectedLang", the parameter there is always null, even when I click on different radio options. How can that be fixed?

I am running Wicket version 7.5.0.

package xx.yy.admin

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormChoiceComponentUpdatingBehavior;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.panel.GenericPanel;
import org.apache.wicket.model.PropertyModel;
import xx.yy.admin.Survey;
import xx.yy.admin.AjaxListener;

import java.util.Arrays;
import java.util.List;


public class LanguageSelectionPanel extends GenericPanel<Void> {


private static final String SVENSKA = "Sve";
private static final String ENGELSKA = "Eng";
private AjaxListener listener;
private Form<Void> form;
private List<String> languages = Arrays.asList(SVENSKA, ENGELSKA);
private Survey surveyModel;
private String selectedLang = "Sve";

private RadioChoice languageRadioChoice;

public SurveyLanguageSelectionPanel(String id, Survey survey, AjaxListener listener) {
    super(id);
    this.surveyModel = survey;
    this.listener = listener;
    initialize();
}

protected void initialize() {
    super.onInitialize();
    form = new Form<Void>("langform") {
        @Override
        protected void onSubmit() {
            info("Init : ");
        }
    };
    add(form);

    languageRadioChoice = getLanguageSelection();

    form.add(languageRadioChoice);
}

private RadioChoice getLanguageSelection() {
    RadioChoice<String> langRadioChoice = new RadioChoice<>("languages",
            new PropertyModel<>(this, "selectedLang"), languages);

    // Add Ajax Behaviour...
    langRadioChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        protected void onUpdate(AjaxRequestTarget target) {
            listener.onUpdate(target);
        }
    });
    return langRadioChoice;
}



public void setSelectedLang(String selectedLang) {
    this.selectedLang = selectedLang;
}

public String getSelectedLang() {
    return selectedLang;
}

}

The html code:

<body>
<wicket:panel>

    <form style="" class="" wicket:id="langform">
        <wicket:enclosure child="languages">
            <span wicket:id="languages"></span>
        </wicket:enclosure>
        <br/>
        <hr/>
    </form>
</wicket:panel>
</body>
</html>
1

There are 1 answers

0
Jojje On

You have to use "AjaxFormChoiceComponentUpdatingBehavior" as described here:

onchange get current value with radioChoice

i.e.

langRadioChoice.add(new AjaxFormChoiceComponentUpdatingBehavior() {
        protected void onUpdate(AjaxRequestTarget target) {
            listener.onUpdate(target);
        }
    });