Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails 3 Fields Plugin - display property values in place of hasMany Object:id using f:display tag

Tags:

grails

I have an domain called Project with a hasMany relationship like

class Project {
    static hasMany = [prefixes: Prefix] 
}

In the Project show.gsp using <f:display bean="project" /> the list of Prefix displays the id's like com.example.Prefix:1

I want to replace this with something more meaningful by concatenating two properties of Prefix like 'number' and 'name' with a hyphen in between. In Grails 2 without Fields plugin I would have done something like this:

<ol>
...
  <li class="fieldcontain">
  <span id="prefixes-label" class="property-label">
  <g:message code="prefixes.label" default="Prefixes" /></span>

  <g:each in="${project.prefixes}" var="p">
    <span class="property-value" aria-labelledby="prefixes-label">
    <g:link controller="prefix" action="show" id="${p.id}">${p.number} - ${p.name}</g:link></span>
   </g:each>
  </li>
...
</ol>

I've tried creating a grails-app/views/project/show/_displayWrapper.gsp with the above code except replacing 'project' with 'bean' and the <f:display bean="project"> in show.gsp but I still got the default page.

How do I use the <f:display bean="project"> style tag to achieve this?

Thanks, Carl

like image 348
Carl Marcum Avatar asked Jan 22 '26 12:01

Carl Marcum


1 Answers

I got it working by creating a default views/project/show/_displayWrapper.gsp for my regular properties:

<li class="fieldcontain">
    <span id="${label}" class="${label}"><g:message code="${label}" default="${label}" /></span>
    <span class="property-value" aria-labelledby="${label}"><g:fieldValue bean="${project}" field="${property}"/></span>
</li>

And a specific one for my hasMany prefixes in views/project/show/prefixes/_displayWrapper.gsp

<li class="fieldcontain">
    <span id="${label}" class="${label}"><g:message code="${label}" default="${label}" /></span>
    <g:each in="${value}" var="p">
        <span class="property-value" aria-labelledby="${label}"><g:link controller="prefix" action="show" id="${p.id}">${p.number} - ${p.name}</g:link></span>
    </g:each>
</li>

And then in my show.gsp I replaced

<f:display bean="project" />

With

<ol class="property-list">
    <f:display bean="project" property="number"/>
    <f:display bean="project" property="name"/>
    ...
    <f:display bean="project" property="prefixes"/>
</ol>
like image 194
Carl Marcum Avatar answered Jan 26 '26 21:01

Carl Marcum