繁体   English   中英

Wicket:在提交时更改AjaxButton的文本

[英]Wicket: Changing the text of an AjaxButton on submit

我是Wicket的菜鸟并尝试在提交时更改AjaxButton的文本。 因此,我们的想法是,第一次加载页面时,用户看到一个标记为“1”的AjaxButton,点击按钮后,按钮的标签变为“2”,然后在下一次点击“3”后等等...这可不难,但正如我所说,当涉及检票口时,我是一个noobie。 所有帮助赞赏!

form.add(new AjaxButton("ajax-button", form)
    {
        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form)
        { //how to change Button label here?
         }

}

答案很简单:使用模型。

        //counter field declared in page class
        private int counter;

            ...

    form.add(new AjaxButton("ajax-button", new PropertyModel<String>(this,
            "counter", form)) {

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            counter++;
            target.addComponent(this);

        }
    });

这可能是Wicket最重要的规则:当你需要改变一些东西时,使用一个模型。 这需要一些时间来习惯,特别是如果你有更多“传统”框架的经验,并且还没有使用Swing。

Nb:将计数器保留在页面类中可能不是一个好主意,但总体思路是一样的。

除了biziclop的答案,这里是一个带有参数变化的文本解决方案。

在你的java代码中:

AjaxButton yourButton = new AjaxButton("btnId"){
//your button's implementation goes here
};
int yourVariable = 42;
Label yourLabel = new Label("labelId", new Model<String>() {
        public String getObject() {
            String text = MessageFormat.format(new Localizer().getString("IdForLocalizerInYourLocalizerFile", null), yourVariable);
            return text;
        }
    })
yourButton.add(yourLabel);

在你的HTML中:

  <a type="submit" wicket:id="btnId">
    <span wicket:id="labelId">[This text will never be seen, will be replaced by "The var..."]</span>
  </a>

最后,您的本地化文件将包含如下行:

IdForLocalizerInYourLocalizerFile= The variable's value is {0}. It will be replaced whenever it changes and button component is added to target. Text will remain.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM