Description
When selecting "old" data - the grid does not update the selected items correctly.
Expected outcome
- Clicking
Print current selection returns updated data
Minimal reproducible example
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.Grid.SelectionMode;
import com.vaadin.flow.component.grid.GridMultiSelectionModel;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.provider.ListDataProvider;
import com.vaadin.flow.router.Route;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
@Route(value = "grid-bug")
public class GridBugView extends VerticalLayout {
public GridBugView() {
var grid = new Grid<MyBean>();
grid.setSelectionMode(SelectionMode.MULTI);
grid.addColumn(e -> e.id).setHeader("ID");
grid.addColumn(e -> e.increasingValue).setHeader("Value");
grid.setItems(new MyBeanDataProvider(callExternalServiceToGetLatestData(0, 0, 0)));
add(grid);
add(new Button("Increment selected items", e -> {
var selectedItems = grid.getSelectedItems();
grid.setItems(new MyBeanDataProvider(callExternalServiceToGetLatestData(
selectedItems.contains(new MyBean("1", 0)) ? 1 : 0,
selectedItems.contains(new MyBean("2", 0)) ? 1 : 0,
selectedItems.contains(new MyBean("3", 0)) ? 1 : 0
)));
if (grid.getSelectionModel() instanceof GridMultiSelectionModel<MyBean> selectionModel) {
// reselect after applying new data provider
// this creates the problem - this does not "selects" the items from within the grid, but the old instances with value=0 which are passed as arguments.
// -- Expectations: the grid is selecting the items that are stored inside the grid.
selectionModel.selectItems(selectedItems.toArray(MyBean[]::new));
}
}));
add(new Button("Print current selection", e -> {
add(new Paragraph("Selected items: " + grid.getSelectedItems().size()));
grid.getSelectedItems().forEach(item -> {
add(new Paragraph("item: " + item.id + " - " + item.increasingValue));
});
}));
}
static List<MyBean> callExternalServiceToGetLatestData(int _1, int _2, int _3) {
var data = new ArrayList<MyBean>();
data.add(new MyBean("1", _1));
data.add(new MyBean("2", _2));
data.add(new MyBean("3", _3));
return data;
}
static class MyBeanDataProvider extends ListDataProvider<MyBean> {
MyBeanDataProvider(Collection<MyBean> items) {
super(items);
}
@Override
public Object getId(MyBean item) {
return item.id;
}
}
static class MyBean {
final String id;
final int increasingValue;
MyBean(String id, int increasingValue) {
this.id = id;
this.increasingValue = increasingValue;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {return false;}
return Objects.equals(id, ((MyBean) o).id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
}
}
Steps to reproduce
- Select 2 and 3
- Press
Increment selected items
- Press
Print current selection
- Expectation:
grid::getSelectedItems() contains the correct values (2=1, 3=1)
Environment
Vaadin version(s): 25.2.1 / latest
Browsers
No response
Description
When selecting "old" data - the grid does not update the selected items correctly.
Expected outcome
Print current selectionreturns updated dataMinimal reproducible example
Steps to reproduce
Increment selected itemsPrint current selectiongrid::getSelectedItems()contains the correct values (2=1, 3=1)Environment
Vaadin version(s): 25.2.1 / latest
Browsers
No response