Lazy loadin, problem sorting

Hi,
I’m trying to implement a sorting lazy-data loading in my grid.
I’ve followed the instruction in the doc but I have a problem.
In the method Dataprovider.fromCallBacks, “query.getSortOrders” return
an empty list.
Is someone have an idea ?

this is the code used

dataProv = DataProvider.fromCallbacks(
						query -> {
							int offset = query.getOffset();
							int limit = query.getLimit();
							
							//List<ComposantTSort> sortOrders = query.getSortOrders().stream().map(sortOrder->new ComposantTSort(sortOrder.getSorted(),sortOrder.getDirection().equals(SortDirection.ASCENDING))).collect(Collectors.toList());

							List<ComposantTSort> sortOrders = new ArrayList<>();
				              for(SortOrder<String> queryOrder : query.getSortOrders()) {
				            	  ComposantTSort sort =  new ComposantTSort(
				                          queryOrder.getSorted(),
				                                  queryOrder.getDirection() == SortDirection.DESCENDING);
				                  sortOrders.add(sort);
				              }
							
							Stream<ComposantT> composants = compSer.getCompBySousfam(getParamSelSousFam(), offset,
									limit, sortOrders);
							return composants;
						},

						query -> (int) compSer.getCountCompBySousfam(getParamSelSousFam()));

Thank you.

Please show the code how you create the grid and the columns.

If the sort orders are empty, that means the user has not sorted any column yet. Your code should allow for an empty sortOrders list.

This the code that create my grid and my column :

private Grid<ComposantT> gridComp = new Grid<>(20);

gridComp.addThemeVariants(GridVariant.LUMO_COLUMN_BORDERS, GridVariant.LUMO_ROW_STRIPES);

colCodArt = gridComp.addColumn(compo -> compo.getArticleT().getArktcodart())
				.setHeader(new Html("<b>Code article<b>")).setSortProperty("arktcodart");
		colCodArt.setWidth("150px");
		colFam = gridComp.addColumn
		(
			(ComposantT composantT) -> 
			{
				if (composantT.getSousfamille() != null)
				{
					if (composantT.getSousfamille().getFamille() != null)
						return composantT.getSousfamille().getFamille().getFamNom();
					else return null;
				}
				else return null;
			}
		).setHeader(new Html("<b>Famille<b>"));

For the moment I try just to add sorting on the column “colCodArt”.

Just a precision, I have the problem directly when I load the grid for the first time. So I don’t have the possibility to sort the
column because the grid is not displayed. But after It’s already empty when I change the sort.

In this case you could manually add your desired initial sorting order like this:

dataProv = DataProvider.fromCallbacks(
	query -> {
		int offset = query.getOffset();
		int limit = query.getLimit();

		List<ComposantTSort> sortOrders = new ArrayList<>();		
		for(SortOrder<String> queryOrder : query.getSortOrders()) {
			ComposantTSort sort =  new ComposantTSort(
				queryOrder.getSorted(),
				queryOrder.getDirection() == SortDirection.DESCENDING);
			sortOrders.add(sort);
		}
		
		// If user has not sorted (a.k.a initial sorting), sort by 'colCodArt'
		if(sortOrders.isEmpty()){
			sortOrders.add(new ComposantTSort("colCodArt", false));
		}

		Stream<ComposantT> composants = compSer.getCompBySousfam(getParamSelSousFam(), offset,
				limit, sortOrders);
		return composants;
	},

	query -> (int) compSer.getCountCompBySousfam(getParamSelSousFam()));

First thank you for your help.

So Ok, I’ve believed it was because of an error in my code that this method return nothing.
So I’ve managed this empty sort but after I have an error when I want to sort my list with the comparator.

My code is :

		
		Comparator<ComposantT> comparator = (o1,o2)->0;
		for (ComposantTSort composantTSort : sortOrders ) {
			switch (composantTSort.getPropertyName()){
				case ComposantTSort.CODART :
					comparator = comparator.thenComparing(ComposantT::getArktcodart);
					break;
			}
			if (!composantTSort.isDescending()) comparator = comparator.reversed();
		}

		List<ComposantT> items = compRepo.findByFamcodeAndSfamcode(famcode, sfamcode, pageRequest).getContent();
		
		items.sort(comparator);

when the instruction “items.sort(comparator);” is execute I have this error

java.lang.UnsupportedOperationException: null
	at java.base/java.util.Collections$UnmodifiableList.sort(Collections.java:1343) ~[na:na]

	at com.steel.dao.ComposantTService.findByFamcodeAndSfamcode(ComposantTService.java:132) ~[classes/:na]

	at com.steel.dao.ComposantTService.getCompBySousfam(ComposantTService.java:62) ~[classes/:na]

	at com.steel.view.UpdateCompView.lambda$94(UpdateCompView.java:1022) ~[classes/:na]

	at com.vaadin.flow.data.provider.CallbackDataProvider.fetchFromBackEnd(CallbackDataProvider.java:137) ~[flow-data-2.1.9.jar:2.1.9]

	at com.vaadin.flow.data.provider.AbstractBackEndDataProvider.fetch(AbstractBackEndDataProvider.java:61) ~[flow-data-2.1.9.jar:2.1.9]

	at com.vaadin.flow.data.provider.DataCommunicator.fetchFromProvider(DataCommunicator.java:362) ~[flow-data-2.1.9.jar:2.1.9]

	at com.vaadin.flow.data.provider.DataCommunicator.activate(DataCommunicator.java:642) ~[flow-data-2.1.9.jar:2.1.9]

	at com.vaadin.flow.data.provider.DataCommunicator.collectKeysToFlush(DataCommunicator.java:589) ~[flow-data-2.1.9.jar:2.1.9]

	at com.vaadin.flow.data.provider.DataCommunicator.flush(DataCommunicator.java:461) ~[flow-data-2.1.9.jar:2.1.9]

	at com.vaadin.flow.data.provider.DataCommunicator.lambda$requestFlush$2f364bb9$1(DataCommunicator.java:425) ~[flow-data-2.1.9.jar:2.1.9]

	at com.vaadin.flow.internal.StateTree.lambda$runExecutionsBeforeClientResponse$1(StateTree.java:368) ~[flow-server-2.1.9.jar:2.1.9]

	at java.base/java.util.ArrayList.forEach(ArrayList.java:1510) ~[na:na]

For you , is the good code to use the comparator ?

You shouldn’t sort the result of the query (which returns only one page) but call the database with the sort.

In pageRequest, you can add the needed information to sort your data.

Great !!! It’s easier to use the sort with pagerequest and the grid is sorted now. Thanks a lot.