這裏是我的黑客:
你要做的第一件事情就是要實現對所需的類可比進行排序,並進一步創建一個compareTo方法。
接下來,在您的項目中輸入crud模塊。在app/views/tags.crud中,您會找到一個名爲relationField.html的文件,它處理添加關係的字段。該文件分爲兩部分,一部分用於創建具有multiple = true的選擇框,另一部分用於創建下拉選擇框。如果你想要這兩種排序,你將不得不在這兩種情況下進行編輯。
替代%{ _field.choices.each() { }
與%{ _field.choices.sort().each() { }%
(基本上增加了排序集合的groovy語法),並且輸入字段將被排序。
的Java類的
完整的示例:
引用類:
@Entity public class Book extends Model {
@Required
public String title;
@Required
@ManyToMany(cascade=CascadeType.PERSIST)
public List<Author> authors;
@Required
@ManyToOne
public Publisher publisher;
//omitted
}
引用的類:
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);
}
}
//omitted
}
這種結構與relationField.html黑客相比會使選擇中的可能性出現排序。
謝謝,這工作。 – vegardoj