playframeworksql-order-by

Crud module : Listing a OneToMany with a specific order


I'm using Crud module and I can make it work well.

But when I displaying the form to add a new User (or edit an existing User), I have a list of Cities that are displayed in a non-alphabetical way. I'd like to sort them based on the name.

I tried to add a @OrderBy to my model, but this doesn't work.

How can I do it?

Here is my model for information :

@Entity
public class City extends Model {
    @Required
    @OrderBy  //doesn't work
    public String nomination; // I had to set this var to this name ...

    public String zipcode;

    public City(String nomination, String zipcode) {
        this.nomination = nomination;
        this.zipcode = zipcode;
    }

    @Override
    public String toString(){
        return this.nomination;
    }
}

Solution

  • EDIT ON UPDATE

    I'm afraid it's nto trivial. In the framework the code that retrieves the list of cities is this one. As you can see it doesn't add any "order by" to the query as it's generic. The CRUD tag that paints the rels (here) just iterates over that field.

    The simplest solution would be to use the CRUD customization capabilites (here) and use teh #{crud.custom} tag to paint your own dropdown with the ordered list of cities.

    OLD ANSWER

    I believe your error is on where the @OrderBy annotation is placed. You should put it on the relation:

      @Entity 
        public class Foo{
           ...
           @OneToMany
           @OrderBy("nomination ASC")
           public List<City> cities;
           ...
        }
    

    This way the list "cities" will be ordered by the parameter.

    I just noticed that you mention you are using the CRUD module. In this case, as far as I remember, CRUD lists the fields ordered by the first column it displays. Try to change that column as per this page so the name is the first column.