Wicket Form中可重复使用的DropDownChoice

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)

我想为这种方法做出漂亮的解决方案.

Mic*_*rov 5

最好DropDownChoice使用预定义的参数进行扩展,而不是Panel使用真实的参数进行扩展DropDownChoice.

这种方法至少有两个优点:

  1. 您不需要创建单独的标记文件,因为它附带了Panel-approach.
  2. 您可以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)

认为这种方法适合您的需求.

  • 为了扭转局面,如果您 ** 做 ** 想要在下拉菜单中添加特殊标记,则可以使用 `Panel`。 (2认同)