在我的项目中,我有 50 多个表单,它们大多彼此相似并使用相同的 DropDownChoice
成分。我可以创建单独的 Panel
,在这里我定义了我的 DropDownChoice
,然后我将使用该 Panel
在我的另一种形式?否则,我如何实现这种情况?
例如 form1
有下一个字段:
姓名 ( TextField
)
姓氏 ( TextField
)
城市 ( DropDownChoice
)form2
有下一个字段:
代码 ( TextField
)
金额 ( TextField
)
城市 (再次相同 DropDownChoice
)
我想为这种方法做出漂亮的解决方案。
最佳答案
最好延长DropDownChoice
使用您预定义的参数,而不是 Panel
与真实 DropDownChoice
.
这种方法至少有两个优点:
Panel
一起提供。 -方法。 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.
}
}
在您的表单中只需使用:
Form form = ...;
form.add ( new CityDropDownChoice ( "cities" ) );
认为这种方法将满足您的需求。
关于forms - Wicket 形式的可重用 DropDownChoice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26606283/