Sti*_*row 0 grails gsp grails-2.0
我有兴趣将多个列表返回到视图,这样我最终可以在一个页面上显示来自完全不同查询的信息行.我已经想出如何做到以下几点:
这是我在我的控制器中的动作:
def processMultipleLists()
{
def stirList = []
Person stirling = new Person('Stirling','Crow', 47)
Person lady = new Person('Lady','McShavers', 4)
stirList << stirling
stirList << lady
def kathieList = []
Person kathie = new Person('Kathie','Esquibel', 47)
Person milagro = new Person('Milagro','Muffin', 4)
Person meeko = new Person('Meeko','Muffin', 4)
kathieList << kathie
kathieList << milagro
kathieList << meeko
def returnThisMap = [:]
returnThisMap.put('One', kathieList)
returnThisMap.put('Two', stirList)
return [returnMap : returnThisMap]
}
Run Code Online (Sandbox Code Playgroud)
然后Grails将"returnMap"(包含"returnThisMap",以下称为"mapNum")返回到我的视图,其中包含以下内容:
<g:if test="${returnMap.size() > 0}">
<table border="1">
<tbody>
<g:each in="${returnMap}" status="i" var="mapNum">
<g:if test="${mapNum.getKey() == 'One'}">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Favorite Number</th>
</tr>
<g:each in="${mapNum.getValue()}" status="c" var="listVar">
<tr>
<td>${listVar.firstName}</td>
<td>${listVar.lastName}</td>
<td>${listVar.favNumber}</td>
</tr>
</g:each>
</g:if>
<g:elseif test="${mapNum.getKey() == 'Two'}">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Favorite Number</th>
</tr>
<g:each in="${mapNum.getValue()}" status="c" var="listVar">
<tr>
<td>${listVar.firstName}</td>
<td>${listVar.lastName}</td>
<td>${listVar.favNumber}</td>
</tr>
</g:each>
</g:elseif>
</g:each>
</tbody>
</table>
</g:if>
<g:else>
No records were found to display.
</g:else>
Run Code Online (Sandbox Code Playgroud)
这实际上有效.它会发布两个列表中的信息.但是......感觉有点"hacky",因为我必须使用groovy标签迭代returnMap中的键/对值.有没有更好的方法在Grails中显示多个列表?
返回到视图的对象已经是一个地图,因此无需创建另一个地图.你可以这样做:
return [stirList: stirList, kathieList: kathieList]
Run Code Online (Sandbox Code Playgroud)
然后在您的视图中,您可以分别迭代它们中的每一个:
<g:each in="${stirList}" var="stir">
...
</g:each>
<g:each in="${kathieList}" var="kathie">
...
</g:each>
Run Code Online (Sandbox Code Playgroud)
在您的示例中,看起来两个列表包含相同的类型并且以完全相同的方式显示,因此甚至可能不需要区分.
归档时间: |
|
查看次数: |
327 次 |
最近记录: |