Anybody knows how to refresh the grid in V8 to reflect changes to items instances? I am currently replacing the associated items and selection (setItems(…) and setSelection(…)) but would like to avoid possible overkill (is the list retransmitted to client?)
Also, about refreshing the selection is still setEditorEnabled(true/false) recommended (in V8 getEditor().setEnabled(true/false))?
I don’t know if it is performance wise but I create a ListDataProvider instance and set this instance as the data provider on the grid. After changing an item I use myListDataProvider.refreshItem(changedItem); and call grid.markAsDirty(); .
Hi Mihael, I am trying to avoid containers as suggested for V8. But yes, with a ListDataProvide it works, but the class has changed, for example the getList() seems not supported any more (but upon insert/delete the grid is not refreshed even if marked ad dirty). Thank you indeed.
I have written one utility method for that purpose in Vaadin 8 (my grid uses in-memory data provider):
public static <T> void refreshGrid(Grid<T> grid, Collection<T> items)
{
// If grid is null then throw illegal argument exception.
Objects.requireNonNull(grid, "Grid cannot be null.");
// If items is null then create an empty collection implemented by the ArrayList.
if (Objects.isNull(items))
{
items = new ArrayList<>();
}
if (grid.getDataProvider().isInMemory())
{
ListDataProvider<T> dp = (ListDataProvider<T>) grid.getDataProvider();
dp.getItems().clear();
dp.getItems().addAll(items);
dp.refreshAll();
grid.markAsDirty();
}
else
{
grid.setItems(items);
}
}
Please note that I overwrite lazy data provider in else clause. This might not be desirable for some people so you can remove the else clause in such a case.
Usage:
Collection<Person> students = getStudentsEnrolledInFrench();
// Create a new grid with lazy data provider.
Grid<Person> grid = new Grid<>();
// Line bellow will call grid.setItems(students) which creates in-memory data provider with backend set to students.
GridUtils.refreshGrid(grid, students);
When your collection of students changes then you need to call refreshGrid() method: