5

在我的项目中,我有 50 多个表单,它们大多彼此相似并且使用相同的DropDownChoice组件。Panel我可以在我定义 my 的地方创建单独的 ,DropDownChoice然后我将Panel在我的其他表单中使用它吗?否则,我该如何实施这种情况?

例如

form1有下一个字段:
姓名( TextField)
( TextField)
城市( DropDownChoice)

form2有下一个字段:
代码TextField
金额TextField
城市(再次相同DropDownChoice

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

4

1 回答 1

5

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

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

  1. 您不需要创建单独的标记文件,因为它带有Panel-approach。
  2. 您可以直接使用DropDownChoice方法。否则,应转发Panel's 方法等方法,或为 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" ) );

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

于 2014-10-28T11:20:34.750 回答