AjaxLink 的 Wicket AttributeModifier
在 wicket 中,AttributeModifier 不会更改 AjaxLink 的属性“class”。它应该更改类属性并更改链接的外观。
public class TestPage extends WebPage {
private AjaxLink link1;
private AjaxLink link2;
public TestPage() {
super();
link1 = new AjaxLink("link1") {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
switchView("view1");
}
};
link2 = new AjaxLink("link2") {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
switchView("view2");
}
};
link1.setOutputMarkupId(true);
link2.setOutputMarkupId(true);
link1.add(new AttributeModifier("class", true, new Model<String>("active")));
link2.add(new AttributeModifier("class", true, new Model<String>("inactive")));
add(link1);
add(link2);
}
private void switchView(String viewName) {
if (viewName.equals("view1")) {
link1.add(new AttributeModifier("class", true, new Model<String>("active")));
link2.add(new AttributeModifier("class", true, new Model<String>("inactive")));
} else if (viewName.equals("view2")) {
link1.add(new AttributeModifier("class", true, new Model<String>("inactive")));
link2.add(new AttributeModifier("class", true, new Model<String>("active")));
}
}
}
相应的 html 文件如下所示:
<html xmlns:wicket>
<body>
<wicket:extend>
<div id="tabs">
<ul>
<li><a wicket:id="link1">View1</a></li>
<li><a wicket:id="link2">View2</a></li>
</ul>
</div>
</wicket:extend>
</body>
</html>
谢谢
In wicket AttributeModifier doesn't change attribute "class" for AjaxLink. It should change class attribute and change how link looks like.
public class TestPage extends WebPage {
private AjaxLink link1;
private AjaxLink link2;
public TestPage() {
super();
link1 = new AjaxLink("link1") {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
switchView("view1");
}
};
link2 = new AjaxLink("link2") {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
switchView("view2");
}
};
link1.setOutputMarkupId(true);
link2.setOutputMarkupId(true);
link1.add(new AttributeModifier("class", true, new Model<String>("active")));
link2.add(new AttributeModifier("class", true, new Model<String>("inactive")));
add(link1);
add(link2);
}
private void switchView(String viewName) {
if (viewName.equals("view1")) {
link1.add(new AttributeModifier("class", true, new Model<String>("active")));
link2.add(new AttributeModifier("class", true, new Model<String>("inactive")));
} else if (viewName.equals("view2")) {
link1.add(new AttributeModifier("class", true, new Model<String>("inactive")));
link2.add(new AttributeModifier("class", true, new Model<String>("active")));
}
}
}
Corresponding html file looks like:
<html xmlns:wicket>
<body>
<wicket:extend>
<div id="tabs">
<ul>
<li><a wicket:id="link1">View1</a></li>
<li><a wicket:id="link2">View2</a></li>
</ul>
</div>
</wicket:extend>
</body>
</html>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确答案已经给出,您必须将组件添加到 AjaxTarget。但是,为了创建更多“Wickety”代码,您可以将类重写为如下所示:
它使用模型,这是 Wicket 中推荐的。然而,那里存在大量的代码重复,所以我可能会建议使用一个不同的组件:
当然,所有这些东西都没有经过测试或编译,所以不能保证。 :)
另外,Wicket 邮件列表中的人肯定会想出一些更优雅的东西......
享受吧。
The correct answer was already given, you have to add the components to the AjaxTarget. However, for the sake of creating more "Wickety" code, you could rewrite your class to something like this:
It makes use of models and that is recommended in Wicket. However, there is heavy code duplication going on there, so I would maybe suggest going for a distinct component:
All this stuff is not tested or compiled, of course, so no guarantees. :)
Also, the guys from the Wicket mailing list will most definitely come up with something more elegant...
Enjoy.
您实际上并没有告诉 wicket 在 ajax 响应中执行任何操作。
一个可能的解决方法是将行:(
或调用执行此操作的函数)添加到两个 onClick 方法中。
You're not actually telling wicket to do anything in the ajax response.
A likely fix is to add the lines:
(or a call to a function doing this) to both of your onClick methods.