Pre-select data from Spring JPA in a Multi Grid

Hi guys,

I am trying to pre-select in Multi Grid data coming from the DB using Spring- JPA.

I have tried everything and I don’t understand why it doesn’t pre-select/pre-check the elements. I can see all the elements from the DB unchecked. I think it as to me with my for loop. or maybe is it a callback problem? it tries to pre-select before the arrival of the data?

Here is my code:

//Display all APS to choose
            Grid<ProjectAP> projectAPDialogGrid= new Grid<>(ProjectAP.class);
            projectAPDialogGrid.setMaxHeight("270px");
            projectAPDialogGrid.setColumns("pspElement", "plannedHours");
            projectAPDialogGrid.addThemeVariants(GridVariant.LUMO_NO_BORDER,GridVariant.LUMO_ROW_STRIPES);
            projectAPDialogGrid.setSelectionMode(Grid.SelectionMode.MULTI);

            ListDataProvider<ProjectAP> dataGridEmployeeProvider = DataProvider.ofCollection(projectAPRepository.findAll());
            projectAPDialogGrid.setDataProvider(dataGridEmployeeProvider);

            for (ProjectAP ap : projectInProgress.getProjectAPS()) {
                projectAPDialogGrid.asMultiSelect().select(ap);
            }
			.
			.
			.
			addAPLayout.add(dialogTitle, projectAPDialogGrid, addApsToProject);
            addAPDialog.add(addAPLayout);
            addAPDialog.open();

What do I do wrong?

Thanks for your time!

It could be because the projectAP’s that you iterate over are not the same object instances as the ones you loaded from the repository for the dataprovider. They may represent the same thing, but java does not see them the same. Therefore it thinks you try to select an item that is not present in the grid.

Can you try it like this? If my assumption is correct then this should work

List<ProjectAP> allProjectAPs = projectAPRepository.findAll();
ListDataProvider<ProjectAP> dataGridEmployeeProvider = DataProvider.ofCollection(allProjectAPs);
projectAPDialogGrid.setDataProvider(dataGridEmployeeProvider);

// iterate over the list of items that was used for the dataprovider! 
for (ProjectAP ap : allProjectAPs) {
	if(projectInProgress.getProjectAPS().contains(ap)){
		projectAPDialogGrid.asMultiSelect().select(ap);
	}
}

Edit: Hmm it may be that my own comparison (if(projectInProgress.getProjectAPS().contains(ap)){) also relies on same object instances.
you could make a list of projectAP ids and make the contains call on that list using ap.getId() as searching needle.

List<ProjectAP> allProjectAPs = projectAPRepository.findAll();
ListDataProvider<ProjectAP> dataGridEmployeeProvider = DataProvider.ofCollection(allProjectAPs);
projectAPDialogGrid.setDataProvider(dataGridEmployeeProvider);

List<Long> selectTheseIds = new ArrayList<>();
for(ProjectAP ap : projectInProgress.getProjectAPS()) {
	selectTheseIds.add(ap.getId());
}

// iterate over the list of items that was used for the dataprovider! 
for (ProjectAP ap : allProjectAPs) {
	if(selectTheseIds.contains(ap.getId())){
		projectAPDialogGrid.asMultiSelect().select(ap);
	}
}

Hey!!

Thanks so much! It works with your second solution. I understand now what the problem was :slight_smile:

Cheers to you man!