如何在 onClick() 中禁用/更改检票口按钮链接的样式
在 Wicket 应用程序中,我有一堆
In a Wicket app, I have a bunch of <button>
elements to which I'm attacking a Link
component. Now in the onClick()
method of the component I want to disable or change the style of the button. How can I do that? Calling setEnabled(false)
has no effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
重复使用 onClick() 是对内存中的同一个对象进行操作。如果您不使用 Ajax,您仍然可以在 Link 的匿名子类中维护某些状态。然后,您可以使用 onBeforeRender() 和 onComponentTag() 来更改每次的显示方式。
AttributeModifiers(或其他行为)不适合这种情况,因为如果您将它们添加到 onClick() 方法中,它们将开始在每次单击时堆叠在同一链接上 - 因为它们是作为链接状态的一部分进行维护的。
您的链接可以跟踪各种状态,允许您的 onClick() 方法通过重复点击来启用/禁用/更改/等。
您还可以重写 onBeforeRender()、isVisible() 以及每次链接显示在页面上时运行的其他方法。无论您单击按钮多少次,构造函数、onConfigure() 和其他构造函数都仅运行一次。
Repeated uses of onClick() are operating on the same object in memory. If you're not using Ajax, you can still maintain some state in an anonymous subclass of Link. Then, you can use onBeforeRender() and onComponentTag() to change how it is displayed each time.
AttributeModifiers (or other behaviors) aren't good for this case because, if you add them in the onClick() method, they will begin stacking on the same link for each click - since they are maintained as part of the Link's state.
Your Link can keep track of all manner of state, allowing your onClick() method to enable/disable/change/etc with repeated clicks.
You can also override onBeforeRender(), isVisible(), and other methods that are run each time the link is displayed on the page. The constructor, onConfigure(), and others are run just once, regardless of how many times you click the button.
我认为这在 Wicket 中并不是一个好主意。当然,这可以通过欺骗来完成,但更简单的是:
AttributeModifier
,并为其使用一个返回如上所述派生值的模型。无论您选择哪一个,原则都是让 Wicket“拉”入渲染信息,而不是显式推送它。
I don't think this is an entirely good idea in Wicket. Of course it could be done by trickery, but it's far simpler to either:
isEnabled()
method to return a value derived from the model of the form/component.AttributeModifier
when you create the component, and use a model for it which returns a value derived as above.Whichever you choose, the principle is to let Wicket "pull" rendering information in rather than pushing it explicitly.
Michael Borgwardt 提供的答案几乎是正确的。
我想补充一点,您需要使用 HTML 按钮元素而不是
(链接)。最初的答案可能会令人困惑,因为 Wicket 中也存在 Link 和 Button。
The answer provided by Michael Borgwardt is nearly correct.
I would like to add, that you need to use HTML button element instead of
<a>
(link). Original answer can be counfusing, because Link and Button also exist in Wicket.我认为 AjaxCallDecorator 应该是您需要用来禁用/更改按钮样式的类。
I think AjaxCallDecorator should be the class you need to use to disable/change style of the button.
问题是你使用了Link。禁用链接使用
而不是
/
The problem is that you use Link. Disabled Links use
<span>
instead of<a>/<button>
and are surrounded with<em>
by default.Using Button component will set 'disabled' attribute in the element.
看看 SimpleAttributeModifier 和 AttributeAppender。根据您的实际要求,其中之一应该可以解决问题。 SimpleAttributeModifier 添加或替换在 wicket 中具有预表示的任何 HTML 标签的属性(替换 css 类),而 AttributeAppender 附加到属性(添加另一个 css 类)。这也适用于启用/禁用按钮,但我还没有尝试过。
示例:
对于 Ajax,您还必须将组件添加到目标。
更详细的例子:
Java代码:
对应的HTML:
Take a look at SimpleAttributeModifier and AttributeAppender. Depending on your actual requirements one of those should do the trick. SimpleAttributeModifier adds or replaces an attribute of any HTML-Tag that has a prepresentation in wicket (replaces the css class), while AttributeAppender appends to the attributes (adds another css class). This should work for enabling/disabling buttons as well but I haven't tried that.
Example:
For Ajax you'll have to add the component to the target as well.
More detailed example:
Java code:
corresponding HTML: