Grid 8 without Bean class

You can configure your Grid something like this:

final Grid<List<String>> grid = new Grid<>();

Binder<List<String>> binder = new Binder<>();
grid.getEditor().setEnabled(true);
grid.getEditor().setBinder(binder);
grid.getEditor().setBuffered(false);

for (int i=0;i<65;i++) {
	final int index = i;
	TextField textField = new TextField();
	Binding<List<String>, String> binding = binder.forField(textField).bind(list -> list.get(index), (list, value) -> list.set(index, value));
	grid.addColumn(list -> list.get(index)).setId(""+i).setCaption("Column "+i).setEditorBinding(binding).setEditable(true);
	}
}

Olli Tietäväinen:

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!

Carp Daniela-Iuliana:

Olli Tietäväinen:

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.

There is more complete example app here with Grid without bean

https://github.com/TatuLund/GridFlip/blob/master/src/main/java/org/vaadin/gridflip/MyUI.java

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);

That’s possibly because here:

grid.addColumn(fakebean -> fakeBean.get(

you have a lambda parameter with lowercase b but on the right side you’re calling fakeBean.get for with an uppercase B which is a different variable

You’re perfectly right. Thanks for the quick response.