List<HashMap<String, String>> rows = new ArrayList<>();
for (item : resultSet) {
HashMap<String, String> fakeBean = new HashMap<>();
fakeBean.put(FIRST, item.getFirstName());
fakeBean.put(LAST, item.getLastName());
rows.add(fakeBean);
}
grid.setItems(rows);
Ok , but what if we have 3 or more columns in resultset ?Thanks!
Then you add more entries to the HashMap and create more columns in the Grid with addColumn. They don’t even need to be hardcoded like in that example, you can just iterate through them in a loop.
What about adding columns without resorting to beans and using a backend service to add items. Service returns items as Collection<HasmMap<String, Object>>.
This is what I have tried so far, but I get 100 rows of ‘Olli5’.
Map<String, String> fakeBean = new HashMap<>();
fakeBean.put("firstName", "Olli5");
DataProvider<HashMap, Void> dataProvider = DataProvider.fromCallbacks(
// First callback fetches items based on a query
query -> {
int offset = query.getOffset();
int limit = query.getLimit();
List<HashMap> result = new ArrayList<HashMap>();
for (int i = offset; i < offset + limit; i++) {
HashMap m = new HashMap();
m.put("firstName", "Olli" + i);
result.add(m);
}
System.out.println("Offset:" + offset + " limit:" + limit + " Query: " + result.size() + " " + result);
return result.stream();
},
// Second callback fetches the number of items for a query
query -> {
return 100;
});
Grid grid = new Grid<HashMap<String, String>>();
grid.addColumn(fakebean -> fakeBean.get("firstName")).setHeader("FirstName").setWidth("270px").setFlexGrow(5);
grid.setDataProvider(dataProvider);