How to repaint a ListView via Ajax has been edited by August Detlefsen (Jan 02, 2007).

(View changes)

Content:

You can't, you need to put it into a WebMarkupContainer and repaint that container instead.

More
If you take a closer look at the markup, and the markup that
gets generated, you can clearly see the problem for a listview:

In the server side markup:

<ul>
<li wicket:id="listitem"><span wicket:id="label">some value</span></li>
</ul>

In the client side markup:

<ul>
<li><span>foo</span></li>
<li><span>bar</span></li>
<li><span>foobar</span></li>
</ul>

What you typically want to do is to redraw the complete <ul></ul>
part, because most browsers really don't appreciate when you replace
parts of a tag, especially when you are working with a <table>.

As you can see in this example, the <ul> tag is not a component, and
Wicket is not aware of it. Even if you were to do:
listview.setOutputMarkupId(true), this wouldn't work, as the listview
itself doesn't have a tag associated with it.

That is why you need to provide the WebMarkupContainer yourself as a
wrapper for the listview.

[Thanks to Igor & Martijn for the original postings to the Wicket-User list. Gwyn 22:32, 21 Aug 2006 (BST)]

Example

In order for the ListView to update from an external source such as a dtatabase you must use a detachable model such as LoadableDetachableModel for the list of items:

public class AjaxListPage extends WebPage {

public AjaxListPage {
    //get the list of items to display from provider (database, etc)
    //in the form of a LoadableDetachableModel 
    IModel itemsList = new LoadableDetachableModel() {
        @Override protected Object load() {
            return provider.getItemsList();    //getItemsList returns a java.util.List of the items to display
        }
    }

    //instantiate the list view with the detachable model
    ListView itemsView = new ListView("theList", itemsList) {
        protected void populateItem(final ListItem listItem) {
            //must override populateItem method of the ListView
        }
    };

    //encapsulate the ListView in a WebMarkupContainer in order for it to update 
    WebMarkupContainer listContainer = new WebMarkupContainer("theContainer");
    
    //generate a markup-id so the contents can be updated through an AJAX call
    listContainer.setOutputMarkupId(true);

    //add a timer behavior to update the container every 5 seconds
    listContainer.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(5)));

    //add the ListView to the container
    listContainer.add(itemsView);

    //add the container to the page
    add(listContainer);

}

}

Reply via email to