【问题标题】:Grails navigation links nested loopGrails 导航链接嵌套循环
【发布时间】:2015-03-31 03:38:37
【问题描述】:

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

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

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

【问题讨论】:

    标签: grails one-to-many


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,帮了大忙。我被困了几个小时。
    • 我知道这是事后的事,但我想知道您能否解释一下为什么此代码更改有效。只是希望能学到新东西。
    • @Daniel 看,在您的代码中,您执行了第二个循环,例如 &lt;g:each in="${hospitalInstance.id}"。现在看看你的 Domain 类 Hospital ,一个 Hospital 只有一个 id 。如果你在循环中使用id 作为计数变量,循环只会执行一次。但你真正需要的是,你必须打印所有属于医院的委员会,对吗?所以,对于一个特定的医院对象,有一个或多个委员会实例相关联,所以你应该将它设置为循环结构。所以,如果医院有 3 个委员会,&lt;g:each&gt; 将迭代 3 个。现在清楚了吗?
    • 所以在我的代码中,&lt;g:each in="${hospitalInstance.committees}"&gt; 是循环结构,它属于与当前选定的医院实例关联的委员会集。如果当前医院有 3 个委员会,循环将执行 3 次,如果有 10 个委员会,循环将执行 10 次。在每次迭代中,${ it } 属于当前委员会。这就是为什么我要打印 ${it.committeeName}${it.description}
    猜你喜欢
    • 1970-01-01
    • 2014-07-09
    • 2020-07-19
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多