how to create a repeating list of links inside a l

2019-04-15 03:33发布

问题:

In Apache Wicket I would like to create a repeating list of links from code. I am not sure what the template should be to get to an html result that looks like this:

<li><a href="whatevenrlink1">link1</a></li>
<li><a href="whatevenrlink2">link2</a></li>
<li><a href="whatevenrlink3">link3</a></li>

回答1:

so after much testing this worked for me. HTML should look like:

<ul>
  <ui wicket:id="LinkList"><a wicket:id="Link"><span wicket:id="Text"/></a></ui>
</ul>

and then the repeating view code will be:

RepeatingView view = new RepeatingView("LinkList");

add(view);

WebMarkupContainer list = new WebMarkupContainer(view.newChildId());
ExternalLink externalLink = new ExternalLink("Link", "http://www.google.com");
externalLink.add(new Label("Text","Google"));
list.add(externalLink);

view.add(list);


回答2:

You can use ListView to create a repeating list of links from code. A ListView is a repeater that makes it easy to display/work with Lists. A ListView holds ListItem children. Items can be re-ordered and deleted, either one at a time or many at a time.

Example:

<tbody>
   <tr wicket:id="rows" class="even">
     <td><span wicket:id="id">Test ID</span></td>
     ...

Though this example is about a HTML table, ListView is not at all limited to HTML tables. Any kind of list can be rendered using ListView.

The related Java code:

 add(new ListView<UserDetails>("rows", listData)
 {
    public void populateItem(final ListItem<UserDetails> item)
    {
            final UserDetails user = item.getModelObject();
            item.add(new Link("id", user.getId()));
    }
 });

Where listData contains the id of every link.



回答3:

There are many choices in how to implement this sort of thing, but they all use some sort of repeater.

See wicket repeater examples for many examples of this.