问题
I have created a nested loop using Grails tags but not getting the output that I am expecting. I am expecting a list of links nested within another set of links. I am close but the nested links are being displayed as one big list, not multiple links.
I have two domains with a one-to-many relationship. My controller is currently dynamic.
writen in Grails 2.3.3
Here are my two domains
class Committees {
String committeeName
String description
static belongsTo = [hospital:Hospital]
static constraints = {
committeeName (nullable:true)
description( inList: ["Committee","Board"])
}
}
class Hospital {
String hospitalName
static hasMany = [committees:Committees]
static constraints = {
hospitalName nullable:true
}
}
Here is the nested loop in my .GSP
<g:each in="${hospitalInstanceList}" status="i" var="hospitalInstance">
<tr>
<td>
<g:link action="show" id="${hospitalInstance.id}">${fieldValue(bean: hospitalInstance, field: "hospitalName")}</g:link>
<g:link action="show" id="${hospitalInstance.id}">
<a href="index.jsp?nav=main&hosp=<%=hospGiven %>" target="_top">
<img src="/Trustees/static/images/img/navigate.msh_board.gif" border="0">
</a>
</g:link>
</td>
</tr>
<tr>
<td>
<ul>
<g:each in="${hospitalInstance.id}" status="j" var="committeesInstance">
<p>Current id: ${hospitalInstance.id }</p>
<li>
<%-- <g:link action="show" id="${hospitalInstance}">${fieldValue(bean: hospitalInstance, field: "committees.committeeName")}</g:link>--%>
<g:link controller="Committees" action="show" id="${committeesInstanceList}">${fieldValue(bean: committeesInstance, field: "committeeName")}</g:link>
</li>
</g:each>
</ul>
</td>
</tr>
</g:each>
回答1:
You need to use ${hospitalInstance.committees}
in inner loop.
Try this code
<table border="1">
<g:each in="${hospitalInstanceList}" status="i" var="hospitalInstance">
<tr>
<td>
<g:link action="show" id="${hospitalInstance.id}">${hospitalInstance.hospitalName}</g:link>
<g:link action="show" id="${hospitalInstance.id}">
<a href="index.jsp?nav=main&hosp=<%=hospGiven %>" target="_top">
<img src="/Trustees/static/images/img/navigate.msh_board.gif" border="0">
</a>
</g:link>
</td>
</tr>
<tr>
<td>
<ul>
<g:each in="${hospitalInstance.committees}">
<li> <g:link action="show" id="${it.id}"> ${it.committeeName} </g:link> </li>
<br>
<li> <g:link action="show" id="${it.id}"> ${it.description}</g:link> </li>
</g:each>
</ul>
</td>
</tr>
</g:each>
</table>
来源:https://stackoverflow.com/questions/29353610/grails-navigation-links-nested-loop