简体   繁体   English

Grails:对嵌套域对象进行排序

[英]Grails: Sorting Nested Domain Objects

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. 我正在使用sortableColumn在控制器中调用排序操作。
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) 这是应用程序文档保存器(我将实际文档存储在MongoDB中,将元数据存储在SQLServer中的applicationDocumentHolder对象中,因此为了简单起见,我将文档文件省略了,因为它与该问题无关)

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 这是GSP的一个片段,我在其中使用sortableColumn标记在控制器中调用sort操作

<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. 我可以在控制器中对ApplicationDocumentHolders的集合进行排序,但是我不断收到违规信息,因为这些违规是附加到数据库的对象。
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). 我还可以重新检索排序后的列表,但是我不确定从数据库中检索到动态排序的嵌套对象时该如何指定(我知道必须有一种简单的方法来做到这一点,但是我很陌生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. 您可以创建一个简单的Closure,并接受要排序的属性和文件。 You can then Closure.curry(attributeName) that Closure to get a new Closure which sorts by that attribute. 然后,您可以关闭该Closure的Closure.curry(attributeName),以获取一个按该属性排序的新Closure。 Then, pass the Closure to Collection.toSorted() to sort the list of files. 然后,将Closure传递给Collection.toSorted()以对文件列表进行排序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM