2

你好我回来了另一个乏味的问题!

试图让我的表格分页。表中有 12 个用户。这是我的控制器功能

    def listDuplicates(params) {
       def result = User.getAllWithDuplicateIDs()
       def totalDupCount = result.size()

         /*sout for troubleshooting */  
        System.out.println("Duplicate:" + result.ID + " " + result.username)

       params.max = Math.min(params.max ? params.int('max') : 10, 100)
       return [resultList: result, totalDupCount: totalDupCount, params:params ]

}

这是我的看法

        <div>
        <fieldset class="warningFieldSet">
            <h1 style="color: red" align="center">
                <g:message code="Duplicate IDs" />

            </h1>
            <p style="color: red; margin-left: 20px;">Duplicate IDs Found!</p>
                <table>
                    <thead>
                        <tr>

                            <g:sortableColumn property="Username" title="Username" />
                            <g:sortableColumn property="ID" title="ID" />
                            <g:sortableColumn property="Status" title="Status" />
                        </tr>
                    </thead>

                    <tbody>
                        <g:each in="${resultList}" status="i" var="resultDuplicate">
                            <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                                <td>
                                    ${resultDuplicate.username}
                                </td>

                                <td style="color: red; font-weight: bold">
                                    ${resultDuplicate.id}
                                </td>

                                <td>
                                    ${resultDuplicate.accountStatus }

                            </tr>
                        </g:each>
                    </tbody>
                    <tfoot>
                        <g:if test="${totalDupCount >10 }">
                            <div class="paginateButtons">
                                <g:paginate action= "listDuplicates" total="${totalDupCount}" />
                            </div>
                        </g:if>
                    </tfoot>
                </table>

            </fieldset>

    </div>

用于查找重复 ID 的域函数

  static List<User> getAllWithDuplicateIDs() {

    findAll("FROM User WHERE id IN (SELECT id FROM User group by id having count(*) > 1) AND id != ''   ", [])

}

按钮出现。并在 URL 中显示偏移量和最大值。该表只是将所有 12 个显示而不是 10 个放在一页上,2 个放在另一页上。2 页码显示,所以它知道它只假设每页只显示 10 个。它只是没有在表本身中执行此操作。我假设它在传递参数等方面存在某种问题。

非常感谢任何建议/意见/帮助!

4

3 回答 3

4

Grails 分页基于两个参数:maxoffset. max确定页面大小,并offset确定当前页面的开始位置。控制器接收这些参数并通常将它们传递给数据库查询。由 grails 添加到域对象的list方法处理这些参数,而 finder 方法采用queryParams. 通常的模式是将params对象直接传递给查找器list或作为queryParams参数传递给查找器。这将返回一个从给定偏移量开始的结果集,并具有一页长度。

在您的示例中,您在getAllWithDuplicateIDs不使用这些参数的情况下进行调用。更新您的查询以获取它们,如下所示:

static List<User> getAllWithDuplicateIDs(params) {
    findAll("FROM User WHERE id IN (SELECT id FROM User group by id having count(*) > 1) AND id != ''   ", [], params)
}

或者,用类似的东西在内存中分页

results = results.drop(params.offset).take(params.max)

直接在查询中分页更可取,因为它可以更好地处理整个列表不适合内存的情况。

于 2013-05-08T16:25:58.543 回答
2

提供maxoffset函数参数:

def result = User.getAllWithDuplicateIDs([max:params.max, offset:params.offset])

并在查询数据库中使用它们。或在此处
查看答案如何从列表中获取最大和偏移量的结果

于 2013-05-08T16:22:42.320 回答
2

看看这个例子。

领域类..

类工作{

static belongsTo = [company:Company]
String jobtitle
String jobdescription
String jobskills
String joblocation
String  experience
String jobtype
String salary

}

控制器代码..

    def uijobs () {

        [res:Job.list(params),jobcount:Job.count()]

    }

视图就在这里。

         <div class="container" id="main">
        <div class="row">
            <g:each in="${res}">
            <div class="col-sm-4">
                <div class="panel panel-warning">
                    <div class="panel-heading">
                        <h4 class="panel-title"><g:link action="infopagejob" controller="Job" id="${it.id}">${it.jobtitle}</g:link></h4>

                    </div>
                    <div class="panel-body">
                       <table class="table">
                           <tr  class="info" >
                               <td > Job Location</td>
                               <td >${it.joblocation}</td>
                           </tr>
                           <tr  class="info">
                               <td>Description</td>
                               <td>${it.jobdescription}</td>
                           </tr>
                       </table>
                    </div>
                </div>
            </div>
            </g:each>
        </div>
        <g:paginate next="Forward" prev="Back"  maxsteps="10" controller="Job" action="uijobs" total="${jobcount}" params="${params}"/>
</div></div>
于 2015-09-01T10:50:53.660 回答