Java – wicket table with variable number of columns

I have been creating tables by adding a listview in the page (providing my data as list < MyObject >) and assigning corresponding IDs to each column in the HTML file

But now I have a situation, instead of a simple list < MyObject >, I have a list < map < string, MyObject > > I also get a list of all possible keys for nested mappings (list < string >) Now I need to create a table where each value of the map should be in the column, and the name of the key points to the value

Suppose I have the following data:

keys = ['a','b']

data = [ { 'a' = 1,'b' = 2 },{ 'a' = 3,'b' = 4 },{ 'a' = 5,'b' = 6}]

I want to create a table:

<table>
    <tr>
        <th>a</th>
        <th>b</th>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
    </tr>
    <tr>
        <td>3</td>
        <td>4</td>
    </tr>
    <tr>
        <td>5</td>
        <td>6</td>
    </tr>
</table>

Knowing that the name and number of keys in a nested map can be changed, what is the best way to implement it in wicket?

Solution

You can nest listviews The tag you want looks like this:

<table>
  <tr><th wicket:id="headerlist"><span wicket:id="header"></span></th></tr>
  <tr wicket:id="contentlist"><td wicket:id="column">
    <span wicket:id="data"></span>
  </td></tr>
</table>

Then you will need three listviews The first (headerlist) will populate the headings in the key list This is simple, so I'll skip this example

The second (content list) will be in your data list In the populateitems method, you will add a third listview (column), which will traverse the key list again:

add(new ListView<Map<String,MyObject>>("contentlist",data) {
  protected void populateItem(ListItem<Map<String,MyObject>> item) {
    final Map<String,MyObject> map = item.getModelObject();
    // Inner list - using item.add to add to the inner list
    item.add(new ListView<String>("column",keys) {
      protected void populateItem(ListItem<String> item) {
        String key = item.getModelObject();
        item.add(new Label("data",map.get(key).toString()));
      }
    });
  }
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>