问题
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