【发布时间】:2021-07-04 09:33:40
【问题描述】:
我正在使用 Wicket 8.10。
我有一个 Wicket 组件,我想根据某些外部条件动态隐藏或显示它。为此,我有以下代码:
var mccc = new MyCoolCustomComponent("component"); // Custom component I wrote
mccc.setOutputMarkupId(true);
mccc.setOutputMarkupPlaceholderTag(true);
mccc.setVisible(false); //Should be hidden initially
var container = new WebMarkupContainer("container");
container.setOutputMarkupId(true);
container.setOutputMarkupPlaceholderTag(true);
container.add(mccc);
add(container);
var updateTimer = new AbstractAjaxTimerBehavior(Duration.seconds(1)) {
@Override
protected void onTimer(AjaxRequestTarget target) {
if(FooSingleton.instance().isBar()) {
mccc.setVisible(true);
} else {
mccc.setVisible(false);
}
target.add(mccc);
}
};
container.add(updateTimer);
对应的 HTML 如下所示:
<div wicket:id="container" >
<div wicket:id="component"/>
</div>
我期望发生的事情:组件最初是隐藏的。当isBar() 返回true 时,组件会显示出来,一旦它再次返回false,它就会被隐藏。
实际发生的情况:组件最初是隐藏的。它在 isBar() 变为 true 时显示,但 不会 在 isBar() 返回 false 时变为不可见。
我也想过使用AttributeModifier 来使用CSS display 属性,但是我找不到如何更改修饰符的值。
【问题讨论】:
-
这应该可以按预期工作。您可以查看浏览器的网络选项卡并检查 Ajax 响应的内容 - 这样您就可以了解问题出在浏览器还是服务器上。