spring-projects / spring-projects/spring-data-commons

Adjust Paging's sort in order to make it easy to build link based on it

Open
#2,952 4 comments 0 reactions 1 assignee View on GitHub

@mp911de is already working on this.

Since Oct 16, 2023.

status: waiting-for-triage
Dominant language
Java
Stars
838
Forks
730
PR merge metrics
No merged PRs in 30d

Description

I'm trying to write link like this

/resources?page=11&size=100&sort=name,asc.

var pageRessources = ressourceRepository.findAll(pageable);

    model.addAllAttributes(
        Map.of(
            "pageResources", pageResources,
            "pageable", pageable
            ));

    return "resource/list";
  }

And my thymleaf templates looks like this

<a
              class="item icon"
              th:href="@{/ressoures(page=1, size=${pageable.pageSize}, sort=${pageable.sort})}"
            >
              <i class="ui icon angle double left"></i>
              &nbsp;
            </a>
            

My issue is that this template, will produce the following link

/ressources?page=1&size=100&sort=name:ASC

But this is not handeled correctly by spring mvc. I could not found a way to easely build the given link with the current API.

So I ended up writing this

var currentSort = pageable.getSort().stream().map((s) -> "%s,%s".formatted(s.getProperty(), s.getDirection())).findFirst().orElse("");

Maybe the best solution is not in spring-data-core but in spring mvc. Or maybe there is a solution that I wasnt able to find.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.