forms - Wicket 形式的可重用 DropDownChoice

标签 forms wicket panel wicket-6 dropdownchoice

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

    相关文章:

    c# - 在面板之间切换

    javascript - 如何将 javascript onSubmit 与 jQuery 提交相匹配?

    ruby-on-rails - 有条件地从 rails 表单中排除字段

    javascript - Symfony2 收集字段,用于发送电子邮件的简单列表

    java - 如何动态创建与 wicket 的静态链接?

    java - 在 Apache Wicket 与@MountPath 发生冲突

    java - 启用/禁用 Wicket 口中的按钮

    javascript - 使用 jquery.calculation.js 和动态添加的表单字段

    java - primefaces ajax 从另一种形式更新面板

    css - 在 sencha touch 中更改背景图像