How to fill Vaadin Grid with Map items type

此生再无相见时 提交于 2019-12-13 08:52:36

问题


I want to fill my Vaadin Grid with Map<String, Object> items. Thats because I get my data from database like this(maybe it's main problem):

List<Map<String, Object>> result = jdbcTemplate.queryForList("select * from db", new Object[]{});

Now lets say that I create my test List and fill it with some maps:

List<Map<String, Object>> items = new ArrayList<>();
items.add(map);
items.add(map2);

I know how to do it with non dynamic data model, for example:

grid.addColumn(Person::getName).setCaption("Name");
grid.addColumn(Person::getAge).setCaption("Age");

But how can I create my Grid columns so it will use my keys from map? What I have by now:

Grid<Map<String, Object>> grid = new Grid<>();

items.get(0).keySet().forEach(key ->
    grid.addColumn(c -> c.get(key)));

grid.setItems(items);

It gives me output:

b2 / b3 / b1

And expected:

a1 / a2 / a3

b1 / b2 / b3

I have no clue how can I get to my item in every single map in list. And also why doesnt it keep an order? Appreciate every answer!


回答1:


Use LinkedHashMap instead of Map.

LinkedHashMap preserves the insertion order.



来源:https://stackoverflow.com/questions/45548691/how-to-fill-vaadin-grid-with-map-items-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!