如何在 Wicket 中隐藏检查 AjaxCheckBox 的 WebMarkUpContainer

How to hide a WebMarkUpContainer checking an AjaxCheckBox in Wicket

好的,基本上我有一个包含 DateTextField 组件的 WebMarkUpContainer,我想让它仅在我检查 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