NanoNi
NanoNi

Reputation: 335

Render a Map of lists as a table in grails

I have a controller which is passing on a hashmap to a view. The map has 3 elements, one of which is as below:

-- A list of lists (A) in which list3 is a list of lists

Now I want to display them on the gsp as follows:

-----------------------------------------------------------------------------
A.list1     |       A.list2     |       A.list3[0]      |       A.list4     |
            |                   |       A.list3[1]      |                   |
            |                   |       A.list3[2]      |                   |
----------------------------------------------------------------------------

I have very little knowledge about generating views and I need nothing more than the tabular format here. Nothing more fancy. I just want the table because the data makes sense only in that format. Thank you.

Upvotes: 0

Views: 650

Answers (1)

V H
V H

Reputation: 8587

welcome to the Grails world. Hope you have an awesome experience. Unsure if you are aware but by generating a view i.e. the default scaffolding CRUD elements auto generated and most specifically the list.gsp has most of that logic in place. None the less I reviewed your question over again and can see your stuck on map that has a list which is really simply stuff. So here goes.:

<g:each in="${A}" var="myMap">
<tr>
 <td>${myMap.list1}</td>
 <td>${myMap.list2}</td>
 <td>
  <g:each in="${myMap.list3}"  status="i" var="myList3">
   ${myList3} <!-- this should be what you want I have added below -->
   <!-- ${myList3?.name} --> <!-- explained further below -->
   <!-- ${i} : ${myList3} --> <!--where i is this is the iterator -->
  </g:each>
  </td>
 <td>${myMap.list4}</td>
 </tr>
 </g:each>

I put in comments myList3?.name simply because if the list is actually a binding of domain objects then you could display the element from the domainclass that is that being returned.

so:

class Country {
  String name
  static hasMany=[cities:Cities]
}

Class Cities {
  String name
  static belongsTo=[Country:country]
}

Then if A was ${country} and list3 was cities then .name would actually show the cities.name which is the binded value...

In other examples of raw maps that are not domainClass binded being returned to a gsp you could use the key value definition to parse:

raw map being passed to gsp

private Map menuMap = ['index':'Socket method', 'ajaxpoll':'Ajax poll',
        'socketremote':'RemoteForm Websocket', 'scsocket':'NEW: Websocket Client/Server']

navbar gsp

<g:each in="${menuMap }" var="cmenu">
            <li <g:if test="${actionName == cmenu.key}">class="active"</g:if>>
                <g:link action="${cmenu.key}">
                    ${cmenu.value}
                </g:link>
            </li>
        </g:each>

Upvotes: 1

Related Questions