AppJars / commons-frontend

Commons utilities for front-end enterprise features
Apache License 2.0
1 stars 1 forks source link

Paging utility for configuring grids #9

Open ngonzalezpazFC opened 2 years ago

ngonzalezpazFC commented 2 years ago

I am using the methods from this utility class for configuring grids:

public class PagingUtils {

  /**
   * A method to convert given Vaadin sort hints to Spring Data specific sort instructions.
   *
   * @param vaadinSortOrders a list of Vaadin QuerySortOrders to convert to
   * @return the Sort object for Spring Data repositories
   */
  public static Sort toSpringDataSort(List<QuerySortOrder> vaadinSortOrders) {
    return Sort.by(vaadinSortOrders.stream()
        .map(sortOrder -> sortOrder.getDirection() == SortDirection.ASCENDING ? Sort.Order.asc(sortOrder.getSorted())
            : Sort.Order.desc(sortOrder.getSorted()))
        .collect(Collectors.toList()));
  }

  /**
   * A method to sort given Vaadin Grid.Column in a given SortDirection.
   *
   * @param column the Grid.Column to which the sortDirection will be set
   * @param sortDirection the sort direction that will be setted to the Grid.Column
   * @return a list with a GridSortOrder for the column
   * 
   * @see com.vaadin.flow.component.grid.Column
   */
  public static <T> List<GridSortOrder<T>> sortGridColumn(Column<T> column, SortDirection sortDirection) {
    ArrayList<GridSortOrder<T>> list = new ArrayList<>();
    list.add(new GridSortOrder<>(column, sortDirection));
    return list;
  }

  /**
   * This method handles Grid.Column sort orders and lazy requests from Grid. Returns a Pageable that
   * can be setted to Grid.setItems after being converted to Stream.
   * 
   * @param <U> the query type
   * @param query the query that contains the page attributes
   * @return Pageable to use as argument of a Spring repository method that implements
   *         PagingAndSortingRepository
   * 
   * @see org.springframework.data.repository.PagingAndSortingRepository
   * @see com.vaadin.flow.component.grid.Grid
   */
  public static <U> Pageable getLazyAndSortedPageable(Query<U, Void> query) {
    return PageRequest.of(query.getPage(), query.getPageSize(), toSpringDataSort(query.getSortOrders()));
  }
}

If a repository interface extends PagingAndSortingRepository (it can use Page findAll(Pageable pageable)), the grid can be configured as follows: grid.setItems(query -> repository.findAll(PagingUtils.getLazyAndSortedPageable(query)).stream()); Grid handles the query parameters it needs while scrolling, so it gets lazy loaded, and the colums of String type values can be sorted. I also use the method sortGridColumn to set a default sort order, for example, sorting by "id" in ascending order: grid.sort(PagingUtils.sortGridColumn(grid.getColumnByKey("id"), SortDirection.ASCENDING));

mlopezFC commented 2 years ago

They are just one liners helper methods, but still interesting. I would improve the second one as this:

    public static <T> List<GridSortOrder<T>> sortGridColumn(Column<T> column, SortDirection sortDirection) {
        return Arrays.asList(new GridSortOrder<>(column, sortDirection));
    }

Go ahead and create a PR.