Ayb*_*kov 5 java checkbox components wicket form-submit
我正在使用wicket框架.
有没有人知道如何通过单击checkBox组件来控制表单中组件的可见性,以及如何在提交表单时获取复选框状态的布尔值(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);
}
Run Code Online (Sandbox Code Playgroud)
但结果并不是我所期待的.它只是改变了像这样的html元素
:
<input type="text" wicket:id="lastName" id="lastName" required="" value="" name="lastName">
Run Code Online (Sandbox Code Playgroud)
至:
<input id="lastName" style="display:none">
Run Code Online (Sandbox Code Playgroud)
反之亦然
Mic*_*rov 10
要通过复选框单击更新任何组件可见性,可以使用"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 );
Run Code Online (Sandbox Code Playgroud)
要在提交后获得checkBox值,您可以检查您的checkBoxValue值.
UPDATE
实际上你可以覆盖你的可隐藏组件方法onConfigure(),根据'checkBoxValue'来设置它的可见性(这只是获得它的另一种方式,可能更优选):
someComponent = new Label("someComponent", "some text")
{
@Override
protected void onConfigure() {
setVisible ( checkBoxValue );
}
};
Run Code Online (Sandbox Code Playgroud)
如果您喜欢这样,那么您可以someComponent.setVisible(checkBoxValue);从AjaxCheckBox#onUpdate()方法中删除代码
更新2
Component 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 );
Run Code Online (Sandbox Code Playgroud)
将容器添加到表单:
form.add(container);
Run Code Online (Sandbox Code Playgroud)
将我们添加someComponent到此容器:
container.add(someComponent);
Run Code Online (Sandbox Code Playgroud)
在AjaxCheckBox#onUpdate()带有ajax的方法更新容器中:
protected void onUpdate(AjaxRequestTarget target) {
//if checkbox is checked, then our component is shown
container.setVisible(checkBoxValue);
target.add(container);
}
Run Code Online (Sandbox Code Playgroud)
在HTML代码中,这就像
<div wicket:id="cont">
<input type="text" wicket:id="someComponent"/>
</div>
Run Code Online (Sandbox Code Playgroud)
您可以向此类容器添加任意数量的组件,您可以使用它来管理它们.
| 归档时间: |
|
| 查看次数: |
8671 次 |
| 最近记录: |