Grails: Sorting Nested Domain Objects -
i've got 1 many relationship in.
have application has multiple files can uploaded it. application view contains uploaded file list (the files in list contain meta data file such file name, file size etc.). having trouble ability sort nested list.
using sortablecolumn call sort action in controller.
how can retrieve application , specify nested list of uploaded files sorted requested attribute (ex. file name, file size etc.)
edit:
here snippets:
here 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 application document holder (i storing actual documents in mongodb , meta data in sqlserver in applicationdocumentholder object, sake of simplicity leaving out document file not pertain 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 null until file 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 snippet gsp using sortablecolumn tag call sort action in 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 wondering best way is.
can sort collection of applicationdocumentholders in controller keep getting violations because these attached objects db.
can re-retrieve list sorted not sure how specify want dynamically sorted nested object when retrieved db (i know there must simple way pretty new grails).
i know how both way future reference.
you can create simple closure takes attribute want sort , file. can closure.curry(attributename) closure new closure sorts attribute. then, pass closure collection.tosorted() sort 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 size files.tosorted sorter.curry('name') // sort name
Comments
Post a Comment