我有一个包含两个组合框的页面。我想让它们的方式是,当我更改第一个组合框时,从数据库获取的第二个组合框的内容被更改。我该怎么做?如何根据前一个内容更改组合框的内容?
回答
执行此类操作的常用方法是通过AJAX。您可以轻松地将AJAX行为添加到第一个DropDownChoice
,以填充/刷新第二个DropDownChoice
的选项。
假设您使用IModel
s来获得DropDownChoice
s的选择。获得第二个DropDownChoice
的选项的IModel
将使用第一个DropDownChoice
的ModelObject(因为它是依赖的)。
private DropDownChoice ddcCountry;
private DropDownChoice ddcCity;
//...
IModel countriesModel = new LoadableDetachableModel(){
@Override
protected Object load() {
return myService.getCountries();
}
};
IModel citiesModel = new LoadableDetachableModel(){
@Override
protected Object load() {
if (ddcCountry.getModelObject() != null){
return myService.getCities(ddcCountry.getModelObject());
}
else { return new ArrayList(); }
}
};
ddcCountry = new DropDownChoice("country", null, countriesModel);
ddcCity = new DropDownChoice("city", null, citiesModel);
您可以将一个AjaxFormComponentUpdatingBehavior
到第一DropDownChoice
。这将在<select>
标记上输出一个onchange
HTML事件处理程序,以便它将使用所选值更新该DropDownChoice
的模型对象,然后将调用行为的onUpdate()
。在onUpdate()
方法中,您只需将第二个DropDownChoice
添加到AjaxRequestTarget
,并通过带有更新选项的ajax响应将其写回。请记住在将要添加到AjaxRequestTarget
的所有组件上使用setOutputMarkupId(true)
。
例如,对于国家和城市2种依赖的选择:
ddcCity.setOutputMarkupId(true);
ddcCountry.add(new AjaxFormComponentUpdatingBehavior(){
@Override
protected void onUpdate(AjaxRequestTarget target) {
// here, ddcCountry's ModelObject has been already updated.
ddcCity.setModelObject(null); // clear selection
if (target != null) {
// Adding the ddc to the AjaxRequestTarget will write
// it back to the ajax response with new options pulled
// from the choices model.
target.addComponent(ddcCity);
}
}
}
如果你不使用IModels
为你的选择(即使用在构造List
对象),你只需要在onUpdate
方法中获得新的List
,并将其设置为ddcCity
与setChoices()
。您可以使用getComponent()
方法获得Component
的行为。如果你想支持没有JavaScript的用户
protected void onUpdate(AjaxRequestTarget target) {
// here, ddcCountry's ModelObject has been already updated.
List cities = myService.getCities(getComponent().getModelObject());
ddcCity.setChoices(cities);
ddcCity.setModelObject(null); // clear selection
if (target != null) {
target.addComponent(ddcCity);
}
}
,你应该从默认禁用处理添加一个提交按钮(也许在一个<noscript>
标签?),并且按钮的onSubmit
执行相同的逻辑。
有关其他参考,请参阅DropDownChoice Examples Wicket wiki页面,您可能会发现“Ajax”部分有趣。
- 1. RStudio - 根据另一个内容中的内容更改变量
- 2. ASP.NET AjaxControlToolkit根据Ajax动态更改组合框内容
- 3. 如何根据组合框选择更改列表框的内容?
- 4. 如何根据内容更改div类?
- 5. 如何根据当前时间更改div的内容?
- 6. 根据输入值更改div内容
- 7. 根据其内容更改python变量
- 8. 根据地理IP更改Div内容
- 9. 根据内容更改文字对齐
- 10. 根据数组中的数组更改div的内容
- 11. 根据内容
- 12. 根据内容
- 13. 如何根据其内容
- 14. 根据另一个组合框值更改组合框值?
- 15. 根据点击的内容更改文本框
- 16. 如何更改div内div的内容?
- 17. 如何根据CSS中的内容更改块的大小?
- 18. 如何根据其内容更改Postgres表数据?
- 19. Angular 2根据当前路由器状态更改sidenav内容
- 20. 如何更改InfoBox内容?
- 21. 如何更改iframe内容?
- 22. 如何更改SpannableString内容?
- 23. VBA Excel根据一个单元格的内容更改另一个单元格的内容
- 24. 更改内容
- 25. 根据内容高度追加内容
- 26. 如何根据定时器更改div的内容
- 27. 如何根据页面内容更改javascript中的url href?
- 28. 如何根据文档宽度更改分区的内容?
- 29. 如何根据下拉选项更改跨度的内容
- 30. 如何根据不同的请求更改内容区域?
非常感谢您的完整答案,我是Wicket的粉丝! :-) –
@NoushinKhaki很高兴这是有用的:-)在这个例子中,也许'DropDownChoices'作为'私人'成员不是最好的选择。此外,你应该在你的'Page' /'Component' detach()'上为它们中的每一个调用'detach()',以便分离'LoadableDetachableModel'选项并避免让它们在PageMap中被序列化。 –