Wicket + jsTree, links generation in ajax response
I'm using jsTree jQuery plugin for drawing a tree. When user clicks a node, an ajax request containing node's id is send to server.
In my response I generating such piece of html (this is done in separate servlet):
<li id="node_id_1"><a href="foobar">Child item 1</a></li>
<li id="node_id_2"><a href="foobar">Child item 2</a></li>
<li id="node_id_3"><a href="foobar">Child item 3</a></li>
<li id="node_id_4"><a href="foobar">Child item 4</a></li>
<li id="node_id_5"><a href="foobar">Child item 5</a></li>
And this items are drawn under the selected node.
The question is: what value should I use for href attribute for开发者_StackOverflow中文版 referring wicket pages? I.e. I need links pointing to class MainPage with some parameters.
What you want is a RepeatingView or ListView that returns some BookmarkablePageLink objects:
Here is a Sample component:
public class JsTree extends Panel{
private static final long serialVersionUID = 1L;
public JsTree(final String id, final IModel<List<MyDomainObject>> model){
super(id);
this.add(new ListView<MyDomainObject>("list", model){
private static final long serialVersionUID = 1L;
@Override
protected void populateItem(final ListItem<MyDomainObject> item){
MyDomainObject modelObject = item.getModelObject();
final Map<String, String> params =
Collections.singletonMap("id", modelObject
.getObjectId());
item.add(
new BookmarkablePageLink<Void>(
"link", MyPage.class,
new PageParameters(params)
).add(new Label("label",modelObject.getName()))
).setOutputMarkupId(true);
}
});
}
}
And the corresponding HTML:
<html>
<body>
<wicket:head></wicket:head>
<wicket:panel>
<ul class="jsTree">
<li wicket:id="list">
<a href="#" wicket:id="link">
<wicket:container wicket:id="label" />
</a>
</li>
</ul>
</wicket:panel>
</body>
</html>
I've used a MyDomainObject
type that just has an id and a name. The name is displayed, the id is linked to. Basically you can add any serializeable parameter to a BookmarkablePageLink
and then parse the parameter from the page you link to using the Page.getPageParameters() method.
精彩评论