I've got a one to many relationship in.
I have an Application which has multiple files which can be uploaded into it. The Application view contains the Uploaded file list (the files in the list just contain meta data about the file such as file name, file size etc.). I am having trouble with the ability to sort the nested list.
I am using the sortableColumn to call a sort action in my controller.
How can I retrieve an application and specify that the nested list of uploaded files be sorted by the requested attribute (ex. File Name, File Size etc.)
EDIT:
here are some snippets:
Here is the Application Domain Object
class Application implements Serializable {
private static final long serialVersionUID = 1
Firm firm
static hasMany = [applicationDocumentHolders:ApplicationDocumentHolder]
static belongsTo = [firm:Firm]
static mapping = {firm lazy: false}
}
And here is the Application Document Holder (I am storing the actual documents in MongoDB and the meta data in SQLServer in an applicationDocumentHolder object, so for the sake of simplicity I am leaving out the document file as it does not pertain to this question)
import java.sql.Time
class ApplicationDocumentHolder implements Serializable {
private static final long serialVersionUID = 1
static belongsTo = [application:Application]
static mapping = {application lazy: false}
Application application
ApplicationDocumentType type
ApplicationDocumentStatus status
Date verfiedDate
Long uploadFileId
//Following fields will be null until a file is uploaded
String fileName
Date uploadDate
String uploadUserId
long fileSize
static constraints = {
type blank: false
status blank: false
verfiedDate nullable: true
uploadFileId nullable: true
uploadFile nullable: true
fileName nullable: true
uploadDate nullable: true
uploadUserId nullable: true
fileSize nullable: true
}
}
Here is a snippet from the GSP where I am using the sortableColumn tag to call the sort action in my controller
<g:sortableColumn action="sort" property="type" title="${message(code: 'applicationDocumentHolder.type.label', default: 'Type')}" />
<g:sortableColumn action="sort" property="status" title="${message(code: 'applicationDocumentHolder.status.label', default: 'Status')}" />
<g:sortableColumn action="sort" property="verfiedDate" title="${message(code: 'applicationDocumentHolder.verfiedDate.label', default: 'Verfied Date')}" />
<g:sortableColumn action="sort" property="fileName" title="${message(code: 'applicationDocumentHolder.uploadFileName.label', default: 'Upload File Name')}" />
I am wondering what the best way to do this is.
I can sort the collection of ApplicationDocumentHolders in the controller but I keep getting violations because these are attached objects to the DB.
I can also re-retrieve the list sorted but I am not sure how you specify that you want a dynamically sorted nested object when it is retrieved from the DB (I know there must be a simple way to do this but I am pretty new to grails).
I would like to know how to do it both way just for future reference.
You can create a simple Closure takes the attribute you want to sort by and the file. You can then Closure.curry(attributeName) that Closure to get a new Closure which sorts by that attribute. Then, pass the Closure to Collection.toSorted() to sort the list of files.
def files = [
[name: 'foo.txt', size: 10],
[name: 'bar.txt', size: 30],
[name: 'hello.txt', size: 20]
]
def sorter = { attr, file -> file[attr] }
files.toSorted sorter.curry('size') // sort by size
files.toSorted sorter.curry('name') // sort by name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.