Java WICKET:控制更改时组件的可见性复选框
我正在使用wicket框架。Java WICKET:控制更改时组件的可见性复选框,java,checkbox,components,wicket,form-submit,Java,Checkbox,Components,Wicket,Form Submit,我正在使用wicket框架。 是否有人知道如何通过单击复选框组件来控制表单中组件的可见性,以及如何在提交表单时获取复选框状态的布尔值(true或false,取决于选中与否)。 我想做这样的事情: TextField nameField = new TextField("name"); public StateDB() { final AjaxCheckBox searchByFio = new AjaxCheckBox("searchByFio", new PropertyModel(this
是否有人知道如何通过单击复选框组件来控制表单中组件的可见性,以及如何在提交表单时获取复选框状态的布尔值(true或false,取决于选中与否)。
我想做这样的事情:
TextField nameField = new TextField("name");
public StateDB() {
final AjaxCheckBox searchByFio = new AjaxCheckBox("searchByFio", new PropertyModel(this, "checkBoxValue")) {
@Override
protected void onUpdate(AjaxRequestTarget target) {
nameField.setVisible(checkBoxValue);
target.add(nameField);
}
};
Form form = new Form("form");
nameField.setOutputMarkupPlaceholderTag ( true );
form.add(nameField);
add(form);
}
但结果并不是我所期望的。它只是像这样更改html元素发件人:
<input type="text" wicket:id="lastName" id="lastName" required="" value="" name="lastName">
致:
反之亦然要通过复选框单击更新任何组件可见性,可以使用“AjaxCheckBox”,例如:
//somewhere in the class create model variable:
boolean checkBoxValue = true;
//this is your hideable component
Label someComponent;
...
//and add PropertyModel for checkbox:
AjaxCheckBox checkBox = new AjaxCheckBox("checkBox", new PropertyModel(this, "checkBoxValue")) {
@Override
protected void onUpdate(AjaxRequestTarget target) {
//if checkbox is checked, then our component is shown
someComponent.setVisible(checkBoxValue);
target.add(someComponent);
}
};
...
//this is your component to update
someComponent = new Label("someComponent", "some text");
//this method allows you to hide/show component with ajax updates.
someComponent.setOutputMarkupPlaceholderTag ( true );
要在提交后获得复选框值,您可以检查您的checkBoxValue
值
更新
实际上,您可以重写可隐藏组件方法onConfigure()
,根据“checkBoxValue”设置其可见性(这只是另一种实现方法,可能更可取):
如果您这样做,那么您可以删除someComponent.setVisible(checkBoxValue)代码>来自AjaxCheckBox#onUpdate()的代码
更新2
组件WebMarkupContainer
允许您包装一些组件。当它被隐藏时,它们实际上是从标记中移除的。但容器的标记仍将以样式display:hidden
存在
在如下位置创建容器:
WebMarkupContainer container = new WebMarkupContainer ( "cont" );
//we will hide this container, so we can replace someComponent.setOutputMarkupPlaceholderTag ( true ); by this
container.setOutputMarkupPlaceholderTag ( true );
将容器添加到表单:
form.add(container);
将我们的someComponent
添加到此容器:
container.add(someComponent);
在AjaxCheckBox#onUpdate()
方法中使用ajax更新容器:
protected void onUpdate(AjaxRequestTarget target) {
//if checkbox is checked, then our component is shown
container.setVisible(checkBoxValue);
target.add(container);
}
在html代码中,这类似于
<div wicket:id="cont">
<input type="text" wicket:id="someComponent"/>
</div>
您可以向这样的容器中添加任意数量的组件,并且可以使用它来管理所有组件。我已经尝试处理第一个示例,但没有得到我所期望的结果。结果是:当我点击复选框时,它只是改变了textfield元素的样式——你们做错了什么。我用TextField试过这个例子,它也很有效。用你实现的代码更新你的问题。这是我的荣幸。我已经对你的问题进行了评论。Wicket的一个最大缺点是通常会在标签内放置一个复选框以使其正确显示,但Wicket不允许向标签添加任何组件。我错了,Wicket实际上只是更改组件的样式。但使用这种样式,用户无法在页面上看到您的文本字段。隐藏组件是正常的行为。Wicket必须知道下次对象必须出现在何处,所以它只需更改其样式即可。但如果出于某种原因确实要删除输入,则必须用WebMarkupContiner包装它,或者用另一个组件(如EmptyPanel)替换它。如果你想的话,我可以教你如何使用WebMarkupContiner包装器。我已经将我想通过复选框控制其可见性的组件分离到另一个窗体。而且还可以控制这种形式的可见性。但我认为更新我的答案是错误的。
protected void onUpdate(AjaxRequestTarget target) {
//if checkbox is checked, then our component is shown
container.setVisible(checkBoxValue);
target.add(container);
}
<div wicket:id="cont">
<input type="text" wicket:id="someComponent"/>
</div>