gpt4 book ai didi

forms - Wicket 形式的可重用 DropDownChoice

转载 作者:行者123 更新时间:2023-12-05 01:02:26 26 4
gpt4 key购买 nike

在我的项目中,我有 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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com