0

I am looking to add a sort button to my data that is displayed on my screen. At the monet i have my repo set up as shown.

@Repository
public interface PlayerRepository extends JpaRepository<Player, Long> {

Player findAllById(Long id);
List<Player> findByOrderByPlayerNameAsc();
}

And in my controller class i have it as such:

@GetMapping("/player")
public String displayPlayer(Model model) {

    model.addAttribute("player", service.findAll());

    return "/player";
}

I want to be able to create a new method and have it as such so that when i click a button on the html of my application it calls this method and sorts the data. At the moment on loading of the screen it displays all data unfiltered, if i change it to use the "findByOrderByPlayerNameAsc" one it automatically filters upon loading. But i would like this to only happen when a button is clicked.

 public String displayPlayerFilteredByName(Model model) {

    model.addAttribute("player", service.findByOrderByPlayerNameAsc());

    return "/player";
}

Is this possible using simply java and html? Or is there an alternative using JS?

1 Answer 1

1

The only way, avoiding JavaScript and dynamic front-end, would be to request from back-end newly generated view or .html of your players.

  1. That requires to set a new mapping eg.
@GetMapping("/players/asc-names")
public String displayPlayersByPlayerNameAsc(Model model) {
   ...
}

where button would'be just a link to new endpoint.

  1. Another way would be giving additional information to you're get /player request and extracting it via @RequestBody on back-end. But that already requeries some JavaScript being set up on the button.

  2. If your application isn't too complex yet. I'd check front-end frameworks such as Angular, which could make it easier to expand in the future.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah i was thinking about that approach @ 1. Would mean i would make a new html screen for each column that i want sortable. Seems like a lot of repetitive work so i guess i will look at angular or else try and implement this using javascript. Its only a small project that i want to create to learn programing. Thanks for your input:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.