简体   繁体   中英

Fragment inside a ListView - Wicket

I'm trying to add fragment inside a repeating li component:

<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 now this can be easily solved by adding an additional tag on the li:

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

And add the markup id "message" to the fragment. But I don't want it. I just want to use the already there <li> to host my fragment, is it even possible?

Thanks guys

I think I found a solution, or better, a workaround.

I do leave the container tag for the Fragment in the <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:

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.

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. also there is already a component attached to that Id so it is not possible to attach any other components to it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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