简体   繁体   English

ListView中的片段-Wicket

[英]Fragment inside a ListView - Wicket

I'm trying to add fragment inside a repeating li component: 我正在尝试在重复的li组件内添加片段:

<ul>
<li wicket:id="listView">HERE GOES MY FRAGMENT</li>
</ul>

The code to populate the listview is the following: 填充列表视图的代码如下:

@Override
protected void populateItem(final org.apache.wicket.markup.repeater.Item<Message> item) {
                    Message msg = item.getModelObject();
                    log.info("Adding message fragment to markup: " + item.getMarkupId());
                    item.add(new MessageFragement(item.getMarkupId(), "messageFragment", this, msg));
}

The generated expected code is: 生成的预期代码为:

<ul>
<li .... wicket:id="listView></li>
<li .... wicket:id="listView></li>
....
</ul>

But my fragment is not added and I receive the Runtime wicket exception: 但是未添加我的片段,并且收到运行时检票口异常:

The component(s) below failed to render. 以下组件无法渲染。 A common problem is that you have added a component in code but forgot to reference it in the markup (thus the component will never be rendered). 一个常见的问题是您已在代码中添加了一个组件,但忘记了在标记中引用它(因此该组件将永远不会被呈现)。

Why I cannot use the item markup id as componenent id for my fragment? 为什么我不能将商品标记I​​D用作片段的组件ID? I now this can be easily solved by adding an additional tag on the li: 我现在可以通过在li上添加其他标签来轻松解决此问题:

<ul><li wicket:id="listView"><div wicket:id="message"></div></li></ul>

And add the markup id "message" to the fragment. 并将标记id“消息”添加到片段中。 But I don't want it. 但是我不要。 I just want to use the already there <li> to host my fragment, is it even possible? 我只想使用已经存在的<li>来托管我的片段,甚至可能吗?

Thanks guys 多谢你们

I think I found a solution, or better, a workaround. 我想我找到了一种解决方案,或者更好的解决方法。

I do leave the container tag for the Fragment in the <li>: 我确实将Fragment的容器标签留在<li>中:

<ul>
<li wicket:id="listView"><div wicket:id="message">FRAGMENT</div></li>
</ul>

After that in the MessageFragment I just set in the Fragment constructor to not render the container tag using the setRenderBodyOnly(true) function: 之后,在MessageFragment中,我只是在Fragment构造函数中设置为不使用setRenderBodyOnly(true)函数呈现容器标签:

public MessageFragement(String id, String markupId, MarkupContainer markupProvider, final Message message) {
            super(id, markupId, markupProvider, new Model<Message>(message));

            setRenderBodyOnly(true);
...
}

And the result is as expected: 结果与预期的一样:

<ul>
    <li wicket:id="listView" wicketpath="...listView_1">MessageFragment1</li>
    <li wicket:id="listView" wicketpath="...listView_2">MessageFragment2</li>
    <li wicket:id="listView" wicketpath="...listView_3">MessageFragment3</li>
</ul>

If youd like to use your li list entry as placeholder for the fragment, you need to at least provide a wicket:fragment for your fragment which should be rendered in placeholder. 如果要使用li list条目作为片段的占位符,则需要至少为片段提供wicket:fragment,该片段应在占位符中呈现。

but, even if you setStripWicketTags to false which would result in the original wicket:id in your markup, this id will not be equal to the return of getMarkupId() because they will get at least a random suffix. 但是,即使将setStripWicketTags设置为false也会导致标记中原始的wicket:id,该ID也将不等于getMarkupId()的返回值,因为它们至少会得到一个随机的后缀。 also there is already a component attached to that Id so it is not possible to attach any other components to it 而且该ID已经附加了一个组件,因此无法将任何其他组件附加到该ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM