在Grails中,有没有更好的方法可以将多个列表返回并显示到一个视图中?

在Grails中,有没有更好的方法可以将多个列表返回并显示到一个视图中?,grails,grails-2.0,gsp,Grails,Grails 2.0,Gsp,我感兴趣的是将多个列表返回到一个视图中,以便最终在一个页面上显示来自完全不同查询的信息行。我已经想出了如何做到以下几点: <g:if test="${returnMap.size() > 0}"> <table border="1"> <tbody> <g:each in="${returnMap}" status="i" var="mapNum"> <g:if t

我感兴趣的是将多个列表返回到一个视图中,以便最终在一个页面上显示来自完全不同查询的信息行。我已经想出了如何做到以下几点:

<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>
以下是我在控制器中的操作:

 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]
}
def processMultipleLists()
{
def斯特利斯特=[]
Person stirling=新人('stirling','Crow',47)
Person lady=新人('lady','McShaver',4)

stirList返回到视图的对象已经是一个贴图,因此无需创建另一个贴图。您可以执行以下操作:

return [stirList: stirList, kathieList: kathieList]
然后,在您的视图中,您可以分别迭代它们中的每一个:

<g:each in="${stirList}" var="stir">
    ...
</g:each>
<g:each in="${kathieList}" var="kathie">
    ...
</g:each>

...
...

在您的示例中,两个列表似乎包含相同的类型,并且显示方式完全相同,因此可能不需要进行区分。

返回到视图的对象已经是一个映射,因此无需创建另一个映射。您可以执行以下操作:

return [stirList: stirList, kathieList: kathieList]
然后,在您的视图中,您可以分别迭代它们中的每一个:

<g:each in="${stirList}" var="stir">
    ...
</g:each>
<g:each in="${kathieList}" var="kathie">
    ...
</g:each>

...
...

在您的示例中,两个列表似乎包含相同的类型,并且显示方式完全相同,因此可能根本不需要进行区分。

Perfect。谢谢!!这比我以前做的好多了。Perfect。谢谢!!这比我以前做的好多了。