表中的Apache Wicket变量链接文本

时间:2014-05-27 07:56:17

标签: apache hyperlink html-table wicket

我正在尝试使用操作链接构建一个表,其中链接标题和逻辑根据某些值而变化。

标记:

<wicket:extend>
<div class="jumbotron masthead">
<div class="container baseContainer">
<table>
<tr>
<th>NAME</th>
<th>DESCRIPTION</th>
<th>STATUS</th>
<th>ACTIONS</th>
</tr>
<tr wicket:id="cListview">
<td><span wicket:id="name"></span></td>
<td><span wicket:id="description"></span</td>
<td><span wicket:id="status"></span></td>
<td>
    <span ><a wicket:id="control" href="#"><wicket:container wicket:id="label" /></a></span>
    </td>
    <td><span wicket:id="edit"><a href="#">EDIT</a></span></td>
<td><span wicket:id="duplicate"><a href="#">DUPLICATE</a></span></td>
<td><span wicket:id="delete"><a href="#">DELETE</a></span></td>
</tr>
</table>
</div>
</div>
</wicket:extend>

Java逻辑:

// get  list from repo
List<User> list = (List<User>) getUserList();

    // render list view
    add(new ListView<User>("cListview", list) {
        private static final long serialVersionUID = 1747919695791673759L;

        @SuppressWarnings({ "rawtypes", "unchecked" })
        protected void populateItem(ListItem item) {
            final User user = (User) item.getModelObject();
            item.add(new Label("name", user.getName()));
            item.add(new Label("description", user.getName()));
            item.add(new Label("status", user.getIsEnabled()));

            // Check user status
            String controlLabel = "";
            String status = "new";
            // TODO create enumerated status field in User pojo or even
            // better a hasmap
            switch (status) {
            case "new":
                controlLabel = "ACTIVATE";
            case "running":
                controlLabel = "PAUSE";
            case "paused":
                controlLabel = "RESUME";
            default:
                break;
            }

            // activate/pause/resume link
            item.add(new Link("control", item.getModel()) {
                private static final long serialVersionUID = -5420108740617806989L;

                @Override
                public void onClick() {
                    User user = (User) getModelObject();
                    System.out.println(user.getName() + " : activated. ");
                }
            }).add(new Label("label", Model.of(controlLabel)));

我使用这些链接寻求帮助:

How do I change link-text in Wicket? how to create a repeating list of links inside a li tag using apache wicket?

由于我的变体链接标题位于表格行内,所以似乎不匹配。

我得到了这个例外:

Failed to handle: <wicket:container wicket:id="label"/>. It might be that no resolver has been registered to handle this special tag.  But it also could be that you declared wicket:id=label in your markup, but that you either did not add the component to your page at all, or that the hierarchy does not match. Container: [Link [Component id = control]]
 MarkupStream: [markup = bundle://306.49:1/com/cortex/web/pages/CampaignsPage.html
<a wicket:id="control" href="#"><wicket:container wicket:id="label"/></a>, index = 1, current =  '<wicket:container wicket:id="label"/>' (line 0, column 0)]
     at org.apache.wicket.markup.MarkupStream.throwMarkupException(MarkupStream.java:526)
     at org.apache.wicket.MarkupContainer.renderNext(MarkupContainer.java:1411)
     at org.apache.wicket.MarkupContainer.renderAll(MarkupContainer.java:1557)
     at org.apache.wicket.MarkupContainer.renderComponentTagBod


[EDIT]

When using span tags:

    <td><a wicket:id="control" href="#"><span wicket:id="label"></span></a></td>
I get:

Last cause: Unable to find component with id 'label' in [Link [Component id = control]]
    Expected: 'campaignsListview:0:control:label'.
    Found with similar names: 'cListview:0:label',...

1 个答案:

答案 0 :(得分:3)

您可以将标签添加到商品中,而不是像以下那样添加链接:

item.add(new Link("control"){...}).add(new Label("label"));

由于所有括号,它看起来有点不清楚。您可以提取链接并单独添加它以使您的代码更清晰:

...
Link link = new Link("control") { ... };
Label label = new Label("label");

link.add(label);
item.add(link)
...