Reputation: 11
I recently started using wicket. I tried to use in your project example "Linked select boxes" from the book "Apache wicket by Vaynberg".
I recreated the example from the book:
public class LinkedSelectboxesPage extends WebPage {
private Country country;
private City city;
public LinkedSelectboxesPage() {
Database.buildData();
country = Database.getCountries().get(0);
FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
add(feedbackPanel);
Form form = new Form("form");
add(form);
DropDownChoice<Country> countries = new DropDownChoice<Country>(
"countries",
new PropertyModel<Country>(this, "country"),
new CountriesModel(),
new ChoiceRenderer<Country>("name", "code")) {
protected boolean
wantOnSelectionChangedNotifications() {
return true;
}
protected void
onSelectionChanged(Country newSelection) {
city = null;
}
};
countries.setRequired(true);
form.add(countries);
DropDownChoice<City> cities = new DropDownChoice<City>(
"cities",
new PropertyModel<City>(this, "city"),
new CitiesModel(),
new ChoiceRenderer<City>("name", "code"));
cities.setRequired(true);
form.add(cities);
}
private static class CountriesModel extends LoadableDetachableModel<List<? extends Country>> {
protected List<? extends Country> load() {
return Database.getCountries();
}
}
private class CitiesModel extends LoadableDetachableModel<List<? extends City>> {
protected List<? extends City> load() {
return Database.getCities(country.getCode());
}
}
}
When i try to use linked select-boxes in my project, breakpoint, which is fixed in front of the function body has not been achieved.
public class AddArticlePanel extends Panel {
private Type type;
private Subtype subtype;
private List<Type> typesList;
private List<Subtype> subtypesList;
@SpringBean
@SuppressWarnings("unused")
public static ITypeDao typeDao;
@SpringBean
@SuppressWarnings("unused")
private ISubtypeDao subtypeDao;
public AddArticlePanel(String id) {
super(id);
this.typesList = typeDao.loadAllTypes();
this.type = this.typesList.get(0);
Form form = new Form("form");
add(form);
FormComponent<String> tbTitleArticle = new TextField<String>("titleArticle").setRequired(true);
form.add(tbTitleArticle);
FormComponent<String> taTextArticle = new TextArea<String>("textArticle").setRequired(true);
form.add(taTextArticle);
DropDownChoice<Type> ddcTypes = new DropDownChoice<Type>(
"typeArticle",
new PropertyModel<Type>(this, "type"),
new TypesModel(),
new ChoiceRenderer<Type>("name", "id")) {
protected boolean wantOnSelectionChangedNotifications() {
return true;
}
protected void onSelectionChanged(Type newSelection) {
type = null;
}
};
ddcTypes.setRequired(true);
form.add(ddcTypes);
DropDownChoice<Subtype> ddcSubtypes = new DropDownChoice<Subtype>(
"subtypeArticle",
new PropertyModel<Subtype>(this, "subtype"),
new SubtypesModel(),
new ChoiceRenderer<Subtype>("name", "id"));
ddcSubtypes.setRequired(true);
form.add(ddcSubtypes);
}
public AddArticlePanel(String id, IModel<?> model) {
super(id, model);
}
private static class TypesModel extends LoadableDetachableModel<List<? extends Type>> {
protected List<? extends Type> load() {
return typeDao.loadAllTypes();
}
}
private class SubtypesModel extends LoadableDetachableModel<List<? extends Subtype>> {
protected List<? extends Subtype> load() {
return subtypeDao.loadSubtypesByTypeId(type.getId());
}
}
}
I can not understand why it happened. Help me, please and sorry for my english.
Upvotes: 0
Views: 4684
Reputation: 11
My solution is to use Ajax:
ddcTypes.add(new AjaxFormComponentUpdatingBehavior("onchange")
{
@Override
protected void onUpdate(AjaxRequestTarget target)
{
target.add(ddcSubtypes);
}
});
but I want to understand why the panel did not work before (without Ajax)...
????!!!!!!!
Upvotes: 1
Reputation: 11
I already use wicket 1.5.7.
I use class AddArticlePanel in class AddArticlePage
public class AddArticlePage extends AbstractBasePage {
public AddArticlePage() {
add(new HeaderPanel("header"));
add(new MenuPanel("menu"));
add(new AddArticlePanel("body")); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
add(new FooterPanel("footer"));
}
}
and class AddArticlePage used in class BlogApplication:
public class BlogApplication extends WebApplication {
@Override
public void init() {
this.getComponentInstantiationListeners().add(new SpringComponentInjector(this));
Injector.get().inject(AddArticlePanel.class);
mountPage("/add-article/", AddArticlePage.class);
}
@Override
public Class<? extends Page> getHomePage() {
return IndexPage.class;
}
}
and this constuction does not work when i open page with linked select-boxes. But if change class BlogApplication ->
public class BlogApplication extends WebApplication {
@Override
public void init() {
this.getComponentInstantiationListeners().add(new SpringComponentInjector(this));
Injector.get().inject(AddArticlePanel.class);
}
@Override
public Class<? extends Page> getHomePage() {
return AddArticlePage.class; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
it happen magic - linked select boxes will work fine. Maybe I did not quite understand how/when/why to use the method WebApplication.mountPage(string, class).
Upvotes: 0
Reputation: 5575
In your onSelectionChanged method you set the type to null. This is the member backing the model of your required DropDownChoice field ddcTypes as opposed to the example from the book where the countries-ddc onSelectionChanged method resets the city variable.
In your case you reset each and every change to the type to null, thus invalidating your form and preventing it from doing anything usefull.
Upvotes: 1