Search code examples
grails

Grails Field Plugin: Select tag for one-to-many relationships


I'm using Grails 2.2.3 and Fields plugin 1.3. I want to customize fields to manage one-to-many relationships using select tag.

In views/_fields/oneToMany/_input.gsp I have:

<g:select name="${property}.id" from="${type.list()}" optionKey="id" value="${value}" class="form-control one-to-many" noSelection="['null': "${label}"]" />

But type is a set, so I can't use list function.

How can I retrieve target domain class?


Solution

  • As long as you use a Map to declare the relationship, for example:

    static hasMany = [ books: Book ]
    

    You can get the list of the referenced domain, which is the key from the hasMany property of the bean, so the from attribute should change to

    from="${bean.hasMany[property].list()}"
    

    Alternatively you can pass the list to the _input.gsp template prefixing the variable name with input-, e.g.

    <f:field property="books" input-domainList="${bookInstaceList}" />
    

    In the _input.gsp template you can use the variable as follows:

    from="${domainList}"
    

    or mixing both methods:

    from"${domainList ?: bean.hasMany[property].list()}"