问题
I'm running into a small issue with RadioGroup. My RadioGroup has the values true and false. The Radio types I have in my radiogGroup use a Model that stores true or false.
On an Ajax onChange event I want to do some handling, and to do so I need to know the selected radio in my radioGroup and another identical radioGroup. The problem is getValue() only returns the initial value from my POJO. Whenever I click on a radio button to change the selected Radio getValue() it still returns the initial value.
When I save my changes, my POJO gets the correct value. I am finding this bizarre, I have spent hours trying to figure our what I'm missing.
回答1:
If you only need to handle some data in the backend i would suggest you override the wantOnSelectionChangedNotifications()
method of the RadioGroup
object and then use the onSelectionChanged()
method (again from the RadioGroup) to handle the logic based on the newSelection
witch you receive as parameter in the method.
IF you want to update the interface the you would need to use the AjaxFormChoiceComponentUpdatingBehavior
witch give you access to AjaxRequestTarget
so you can update the view.
回答2:
Use a AjaxFormChoiceComponentUpdatingBehavior instead of a AjaxFormComponentUpdatingBehavior.
From the javadoc:
public abstract class AjaxFormChoiceComponentUpdatingBehavior extends AjaxFormComponentUpdatingBehavior
This is a Ajax Component Update Behavior that is meant for choices/groups that are not one component in the html but many.
Use the normal AjaxFormComponentUpdatingBehavior for the normal single component fields
回答3:
You need to make sure that the Value (state) from the Checkbox gets pushed to the model. have a look at AjaxFormComponentUpdatingBehavior. See
http://www.jroller.com/karthikg/entry/wicket_and_ajax
they explain a bit about this.
disclaimer: i don't have access to the sources where i have done such a thing and write this only with what i remember from it
回答4:
Put some code in the question so we can help. For me the solution was the processInput() method. Call it on your RadioGroup to update the model attached to the RadioGroup. I do it this way:
My sample Radio:
private Radio myRadio() {
Radio localmyRadio = new Radio("myRadio", Model.of(someValue), myRadioGroup);
localmyRadio.add(new AjaxEventBehavior("click") {
@Override
protected void onEvent(AjaxRequestTarget target) {
myRadioGroup.processInput(); //updates the model of the radiogroup
//do some stuff
target.appendJavaScript("$(\".jq-ui-button\").button();");
}
});
return localmyRadio ;
}
My sample RadioGroup:
private RadioGroup myRadioGroup() {
RadioGroup localmyRadioGroup = new RadioGroup("myRadioGroup", selectedRadioModel);
localmyRadioGroup.setOutputMarkupId(Boolean.TRUE);
return localmyRadioGroup;
}
This way when i click the radio the model of the RadioGroup gets the value of the variable someValue
. Read the selected value from the RadioGroup model. In this case it's the value of the variable selectedRadioModel
.
来源:https://stackoverflow.com/questions/2795182/radiogroup-getvalue-does-not-return-correct-selected-value