Wicket : Add a list to a datatable

462 views Asked by At

I have a list of object to display in a table with wicket. Those object are composed of a String and an Array of String.

My problem is that i don't know how to add this array into my table. And second, i have some css that I need to apply to each of my String of the array, so each of them have to be on a different div/span/li.

Can it be a good idea to concatenate all those elements and add the "div" manually ?

Thank you for your help :)

2

There are 2 answers

0
CocoAll On BEST ANSWER

I have been to obssesed with using a datatable. By using a list in a list it work just fine !

JAVA :

ListView<Profil> listProfil = new ListView<Profil>("listProfils", profils) {
    protected void populateItem(ListItem<Profil> item) {
        Profil tmpProfil = item.getModelObject();
        item.add(new Label("libelle", tmpProfil.getLibelle()));
        item.add( new ListView("listChamps", tmpProfil.getChamps()) {
            protected void populateItem(ListItem item) {
                item.add(new Label("libelleChamps", item.getModel()));
            }
        });
    }
});

And the associate HTML template :

<tr wicket:id="listProfils">
    <div class="row">
        <td class="col-xs-4 col-md-3 col-lg-3 text">
            <span wicket:id="libelle"></span>
        </td>
        <td class="col-xs-7 col-md-8 col-lg-8 text" colspan="3">
            <span wicket:id="listChamps">
                <span wicket:id="libelleChamps" 
                class="label label-default badge badge-tag" >Champs</span>
            </span>
        </td>
    </div>
</tr >
0
Acasmol On

As a good practice in Wicket, to build a Table in HTML that is being fed from a List, you need the following elements:

HTML

<table wicket:id="yourWicketIdOfDataTableObject">[table]
</table>

JAVA

A POJO (pojoObject) that represents each element (o regiser) of your table

A DataProvider (dataProviderObject) class that extends from SortableDataProvider<pojoObject, String>

You need to override the iterator(), size() and model() methods according to your needs.

A List<IColumn<pojoObject,String>> columnsObject

The above object will represent the colums of your table.

You could add columns as follows:

columnsObject.add(new PropertyColumn<pojoObject,String>(new Model<String>("nameOfTheColum"),pojoObjectPropertyName))

A DefaultDataTable tableObject:

DataTable<pojoObject, String> = new DefaultDataTable("yourWicketIdOfDataTableObject", columnsObject, dataProviderObject, NumOfColumns)

The above object will represent the table.

As you might see pojoObject wraped by columnsObject and dataProviderObject, and these two will be wrapped by tableObject at the end. Your array will be accessed in the iterator() method of the dataProviderObject, because it needs to retrieve the iterator of the list; The pojoObject that represent the each element of your actual list will be necessary (in case you don't have one yet)

At the end, you only need add tableObject in their Wicket Parent, as any other Wicket Component.