Reputation: 39899
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 alphabetically 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?
Thanks for your help!
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;
}
}
Upvotes: 3
Views: 1047
Reputation: 151
Ran into the same issue today.
If you don't want to hack JPAPlugin or create custom fields every time you have a OneToMany relationship, you can also reorder them client-side via a little bit of JavaScript.
Here is use jQuery but you could also easily do it in Vanilla
<script type="text/javascript">
innerSort = function (a, b) {
return (a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase()) ? 1 : -1;
};
$('select').each(function( index ) {
var previousVal = $(this).val();
$(this).find('option').sort(innerSort).appendTo($(this));
$(this).val(previousVal);
});
</script>
Just place this bit of code into the CRUD/layout.html template (you can override it to make your own, see Play tutorial), at the end of the page
Upvotes: 0
Reputation: 2894
I got this working by hacking JPAPlugin.java:
--- a/framework/src/play/db/jpa/JPAPlugin.java
+++ b/framework/src/play/db/jpa/JPAPlugin.java
@@ -771,7 +771,11 @@ public class JPAPlugin extends PlayPlugin {
@SuppressWarnings("unchecked")
public List<Object> list() {
- return getJPAContext().em().createQuery("from " + field.getType().getName()).getResultList();
+ String query = "from " + field.getType().getName();
+ if (field.isAnnotationPresent(OrderBy.class)) {
+ query += (" order by " + field.getAnnotation(OrderBy.class).value());
+ }
+ return getJPAContext().em().createQuery(query).getResultList();
}
};
}
This way you can annotate the City attribute on your User model with @OrderBy("name"), and you will have an ordered drop-down when editing your User object using the CRUD module:
@Entity
public class User{
...
@OneToMany
@OrderBy("name ASC")
public City city;
...
}
Upvotes: 0
Reputation: 16439
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.
Upvotes: 4