2015-03-30 37 views
0

我创建了一个使用Grails标签的嵌套循环,但没有得到我期待的输出。我期待在另一组链接中嵌套的链接列表。我很近,但嵌套链接显示为一个大列表,而不是多个链接。Grails导航链接嵌套循环

我有两个具有一对多关系的域。我的控制器目前是动态的。

writen Grails的2.3.3

这里是我的两个域

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 
    } 
} 

这里是我的.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

您需要在内部循环使用${hospitalInstance.committees} 。 试试这个代码

<table border="1"> 
 
<g:each in="${hospitalInstanceList}" status="i" var="hospitalInstance"> 
 
         <tr> 
 
\t \t \t      \t <td> 
 
\t \t \t  
 
\t \t \t \t \t \t \t \t \t \t <g:link action="show" id="${hospitalInstance.id}">${hospitalInstance.hospitalName}</g:link> 
 

 
\t \t \t \t \t \t \t \t \t \t <g:link action="show" id="${hospitalInstance.id}"> 
 
\t \t \t \t \t \t \t \t \t \t <a href="index.jsp?nav=main&hosp=<%=hospGiven %>" target="_top"> 
 
\t \t \t \t \t \t \t \t \t \t <img src="/Trustees/static/images/img/navigate.msh_board.gif" border="0"> 
 
\t \t \t \t \t \t \t \t \t \t </a> 
 
\t \t \t \t \t \t \t \t \t \t </g:link> 
 

 
\t \t \t      \t </td> 
 
         </tr>  
 

 
\t \t \t \t \t <tr> \t \t \t \t \t 
 
\t \t \t \t \t \t    <td> 
 
\t \t \t \t \t \t       <ul> 
 
\t \t \t \t \t \t \t \t \t \t   <g:each in="${hospitalInstance.committees}"> 
 
\t \t \t \t          <li> <g:link action="show" id="${it.id}"> ${it.committeeName} </g:link> </li> 
 

 
\t \t \t \t          <br> 
 
\t \t \t \t          
 
\t \t \t \t          <li> <g:link action="show" id="${it.id}"> ${it.description}</g:link> </li> 
 
\t \t \t \t          
 
\t \t \t \t          
 
\t \t \t \t         </g:each> 
 
               </ul> 
 
\t \t \t \t \t \t    </td> \t \t \t \t \t \t \t \t \t \t 
 
\t \t \t \t \t </tr> 
 
</g:each> \t \t \t \t 
 
</table>

+0

谢谢,这帮助了这么多。我被困在那里几个小时。 – Daniel 2015-03-31 17:49:47

+0

我知道这是事实,但我想知道如果你能解释为什么这个代码更改工作。只是希望学习新东西。 – Daniel 2015-04-07 19:47:19

+0

@Daniel在你的代码中看到,你完成了第二个循环,比如'”将迭代3 ..现在清楚了吗? – 2015-04-11 07:55:48