Here is my hack:
The first thing you need to do is implement Comparable in the class you want to sort, and then create the compareTo method.
Then enter the crud module in your project. In the application /views/tags.crud you will find a file called relatedField.html that processes the fields for adding relationships. This file is divided into two parts: one for creating custom boxes with several = true and one for creating drop-down menus. If you want both of these sorts to be edited in both cases.
Replace %{ _field.choices.each() { } with %{ _field.choices.sort().each() { }% (basically adding groovy syntax to sort the collection) and the input fields will be sorted.
Full example of Java classes:
Class Link:
@Entity public class Book extends Model {
@Required public String title; @Required @ManyToMany(cascade=CascadeType.PERSIST) public List<Author> authors; @Required @ManyToOne public Publisher publisher;
} Code>
Associated class:
public class Author extends Model implements Comparable {
@Required public String firstName; @Required public String lastName; public int compareTo(final Author otherAuthor) { if (this.lastName.equals(otherAuthor.lastName)) { if (this.firstName.equals(otherAuthor.firstName)) { return 0; } else { return this.firstName.compareTo(otherAuthor.firstName); } } else { return this.lastName.compareTo(otherAuthor.lastName); } }
} Code>
This structure, compared to hack on relationField.html, will make the selection options sorted.
source share