BSe*_*zin 4 forms wicket panel dropdownchoice wicket-6
在我的项目中,我有50多个表单,它们大多是彼此相似的并使用相同的DropDownChoice组件.我可以创建单独的Panel,我定义我的DropDownChoice,然后我将以Panel另一种形式使用它吗?否则,我如何实现这种情况?
例如
form1有下一个字段:
name(TextField)
surname(TextField)
city(DropDownChoice)
form2有下一个字段:
Code(TextField)
Amount(TextField)
city(再次相同DropDownChoice)
我想为这种方法做出漂亮的解决方案.
最好DropDownChoice使用预定义的参数进行扩展,而不是Panel使用真实的参数进行扩展DropDownChoice.
这种方法至少有两个优点:
Panel-approach.DropDownChoice直接使用方法.否则,您应该转发方法Panel的方法,或者为DDC实现getter方法.所以,这样的事情会更好:
public class CityDropDownChoice extends DropDownChoice<City> // use your own generic
{
/* define only one constructor, but you can implement another */
public CityDropDownChoice ( final String id )
{
super ( id );
init();
}
/* private method to construct ddc according to your will */
private void init ()
{
setChoices ( /* Your cities model or list of cities, fetched from somewhere */ );
setChoiceRenderer ( /* You can define default choice renderer */ );
setOutputMarkupId ( true );
/* maybe add some Ajax behaviors */
add(new AjaxEventBehavior ("change")
{
@Override
protected void onEvent ( final AjaxRequestTarget target )
{
onChange ( target );
}
});
}
/*in some forms you can override this method to do something
when choice is changed*/
protected void onChange ( final AjaxRequestTarget target )
{
// override to do something.
}
}
Run Code Online (Sandbox Code Playgroud)
在您的表单中,只需使用:
Form form = ...;
form.add ( new CityDropDownChoice ( "cities" ) );
Run Code Online (Sandbox Code Playgroud)
认为这种方法适合您的需求.
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |