好的,基本上我有一个WebMarkUpContainer,其中包含一个DateTextField组件,并且我想仅在检查AjaxCheckBox时使其可见。

总的来说,我的代码是:

private static final class Results extends BootstrapForm<ResultsModel>
    {

 final AjaxCheckBox isExamsSuccess = new AjaxCheckBox("isExamsSuccess") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                updateModel();
                toggleStep(target);
            }
        };

        final WebMarkupContainer wmc = new WebMarkupContainer("wmc");

        final DateTextField startDate = new DateTextField("startDate",
                    new DateTextFieldConfig()
                       .autoClose(true).withFormat("dd/MM/yyyy")
                       .withLanguage("el").withEndDate(new DateTime()));


public Results(String id, CompoundPropertyModel<ResultsModel> propertyModel)
        {

            super(id, propertyModel);
            add(isExamsSuccess);
            wmc.add(startDate);
            add(wmc);

  protected void toggleStep(AjaxRequestTarget target) {
            if(isExamsSuccess.getModelObject() == true){
                isExamsSuccess.setModelObject(true);
                wmc.setVisible(true);
                target.add(wmc);
            }
            else {
                wmc.setVisible(false);
                target.add(wmc);
            }
        }
}


我真的很感谢您的帮助

最佳答案

您的代码看起来不错!您只需要将wmc的初始可见性设置为取决于isExamsSuccess

wmc = new WebMarkupContainer("wmc") {
   @Override public void onConfigure() {
     super.onConfigure();
     setVisible(isExamsSuccess.getModelObject());
   }
}
wmc.setOutputMarkupPlaceholderTag(true);


另外,您需要调用setOutputMarkupPlaceholderTag(true),因为Wicket需要能够找到HTML元素以将可见性从off转换为on

08-24 12:36