Reputation: 378
I am trying to access what value is selected by user in DropDownChoices in apache wicket. I am adding AjaxFormComponentUpdatingBehavior to DropDownChoices element. I am getting null as selected option in onUpdate method of behavior. I tried to access selected value by different ways as shown in following code but still getting null when debuing.
private String selectedMake;
private final Map<String, List<String>> modelsMap = new HashMap<String, List<String>>(); // map:company->model
modelsMap.put( "AUDI", Arrays.asList( "A4", "A6", "TT" ) );
modelsMap.put( "CADILLAC", Arrays.asList( "CTS", "DTS", "ESCALADE", "SRX", "DEVILLE" ) );
modelsMap.put( "FORD", Arrays.asList( "CROWN", "ESCAPE", "EXPEDITION", "EXPLORER", "F-150" ) );
IModel<List<? extends String>> makeChoices = new AbstractReadOnlyModel<List<? extends String>>() {
@Override
public List<String> getObject() {
return new ArrayList<String>( modelsMap.keySet() );
}
};
lastDropDownChoice = new DropDownChoice<String>( "daynamicTimeConstrains",
new PropertyModel<String>( this, "selectedMake" ), makeChoices );
lastDropDownChoice.add( new AjaxFormComponentUpdatingBehavior( "onchange" ) {
@Override
protected void onUpdate( AjaxRequestTarget target ) {
// Getting this as null
System.out.println( selectedMake );
// Getting this as null
getFormComponent().getModel().getObject();
}
} );
Upvotes: 0
Views: 1791
Reputation: 16131
Use the OnChangeAjaxBehavior. This code works for me:
public class HomePage extends WebPage {
private String text;
public String getText() {
return text;
}
private static final List l = Arrays.asList("a", "b", "c");
public HomePage(final PageParameters parameters) {
super(parameters);
add(ddc());
}
private DropDownChoice ddc(){
DropDownChoice ddc = new DropDownChoice("ddc", new PropertyModel(this, "text"), l);
ddc.add(new OnChangeAjaxBehavior() {
@Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println(getComponent().getDefaultModelObjectAsString());
System.out.println("getText: " + getText());
}
});
return ddc;
}
}
Upvotes: 1
Reputation: 383
I have had some issues with this in the past, mostly with textboxes because the input is not processed until the form is submitted. That said, when the property models do not seem to be updating properly, I will use getFormComponent().getConvertedInput() and cast it within the AjaxFormComponentUpdatingBehavior() as seen below:
lastDropDownChoice.add( new AjaxFormComponentUpdatingBehavior( "onchange" ) {
@Override
protected void onUpdate( AjaxRequestTarget target ) {
String input = (String) getFormComponent().getConvertedInput();
System.out.println( input );
selectedModel = input;
}
} );
I would be interested to hear from others what drawbacks this method may have, but it does seem to work more reliably then the many other seemingly potential options one could use.
As an aside, I typically always create a new IChoiceRenderer() and override the abstract values to be able to pass objects instead of strings and be able to display them exactly how I want as follows:
lastDropDownChoice = new IndicatingDropDownChoice("daynamicTimeConstrains", new PropertyModel(this, "selectedMake"), makeList, new IChoiceRenderer() {
@Override
public Object getDisplayValue(Object o) {
return makeList.indexOf(selectedMake) + "." + o.toString();
}
@Override
public String getIdValue(Object o, int i) {
return o.toString();
}
});
Upvotes: 0