Hello,
given:
Column<T> column;
T item;
How can I get the column-value for the item (in Vaadin 10.0.4)?
In Vaadin 8 this was:
column.getValueProvider().apply(item);
Hello,
given:
Column<T> column;
T item;
How can I get the column-value for the item (in Vaadin 10.0.4)?
In Vaadin 8 this was:
column.getValueProvider().apply(item);
By checking the source code, I can confirm that reference to ValueProvider
is not saved in the Column
class. This is probably because Column
can be created without a ValueProvider
also (For example, directly passing a Renderer
).
I assume you could still save a reference to the ValueProvider when you’re creating the Column, and use that saved reference later.
I’m not sure how to do this. In my case I have an ExcelExporter that exports a grid to an excel file.
The ExcelExporter takes only the grid as an argument and runs the following code:
ListDataProvider<T> listDataProvider = (ListDataProvider<T>) grid.getDataProvider();
Collection<T> items = listDataProvider.getItems();
for (T item : items) {
// if not defined, visibleColumns = grid.getColumns();
for (Column<T> column : visibleColumns) {
// FIXME this line I would like to do....
Object value = column.getValueProvider().apply(item);
buildExcelCell(value);
}
}
A generic workaround is to build a Map of (Column, ValueProvider) when you add columns. This could be in your extended Grid.
Then the ExcelExporter would need a grid and a valueProviderMap as arguments?
If that’s the solution, I think there is missing something in the framework.
The grid knows how to show the (formated) values when given the items, so why not implement
a public method that returns the (formated) values (similar to Vaadin 8)? This method could
also check if there is a ValueProvider or a Renderer available for the specific column.
V10 Grid
is using a Renderer
in all the cases. Even if you use a ValueProvider
to create a Column, it internally uses a TemplateRenderer
.
Also, a Grid cell may contain more that one value (you can render complex structures using TemplateRenderer
). So, one-to-one mapping between a Grid
and an Excel sheet is not always possible.
A dirty hack for getting the cell content would be to get the cell json representation by reflection
Method generateJsonMethod = DataCommunicator.class.getDeclaredMethod("generateJson", Object.class);
generateJsonMethod.setAccessible(true);
JsonValue jsonValue = (JsonValue) generateJsonMethod.invoke(grid.getDataCommunicator(), item);
and then parse the jsonValue.
This is not a general solution to the problem, but works for me.
Hello, is there any other way to read the values of the cells from a grid? Unfortunately I can’t really find any example code on this.
Hi, I also need a way to access particular cell in Grid.
Need to set/get value to the cell based on event listener.
Vikas Rana:
Hi, I also need a way to access particular cell in Grid.
Need to set/get value to the cell based on event listener.
Hi Vikas,
Can you explain a little bit your use case? It can probably be done with a component renderer or a template renderer but without any detail it’s hard to say.