Details
-
Type:
Improvement
-
Status:
Open
-
Priority:
Minor
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: None
-
Labels:None
-
Patch Submitted:Yes
Description
Currently if you have a 1-1 or 1-many association, the default scaffolding renders a <select> with the appropriate domain objects in it. But they are always sorted by id, and that is often not what you want. I would suggest that, for example, if there is a toString() representation you would probably want to see them sorted alphabetically by toString() value.
Obvious candidate is to use Comparable. If the domain implements Comparable, you can easily sort it before rendering.
i.e. change renderEditor.template.renderManyToOne from
private renderManyToOne(domainClass,property) {
if(property.association) {
return "<g:select optionKey=\"id\" from=\"\${$
}
}
to
private renderManyToOne(domainClass,property) {
if(property.association) {
if(Comparable.isAssignableFrom(property.type))
{
return "<g:select optionKey=\"id\" from=\"\${${property.type.name}
.list().sort{a,b->(a<b ? -1 : 1)}}\" name=\"$
{property.name}.id\" value=\"\${${domainClass.propertyName}?.${property.name}?.id}\" $
{renderNoSelection(property)}></g:select>"}
else
{
return "<g:select optionKey=\"id\" from=\"\${${property.type.name}.list()}\" name=\"${property.name}.id\" value=\"\${${domainClass.propertyName}?.${property.name}?.id}\" ${renderNoSelection(property)}
></g:select>"
}
}
}
i.e. add the .sort() if domain class is Comparable
Ahem...that should of course be more like
.sort
{a,b->a.comparesTo(b)}i.e. actually use the Comparable implementation that the user supplies directly.
D'oh!