简体   繁体   中英

Grails Pagination

Hello im back with another tedious question!

Trying to get my table to paginate. There are 12 users in the table. Here is my controller function

    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 ]

}

Here is my view

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

Domain function for finding the duplicate IDs

  static List<User> getAllWithDuplicateIDs() {

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

}

The buttons show up. And in the URL the offset and max is displayed. The table just puts all 12 displayed instead of 10 on one page and 2 on the other. 2 Page numbers show up so It knows that it is only suppose to display only 10 per page. It just isn't doing it in the table itself. Im assuming its some kind of issue with passing params and such.

Any Suggestions/Opinions/Help are/is greatly appreciated!

Grails pagination is based on two parameters: max and offset . max determines the page size, and offset determines where the current page starts. The controller receives these parameters and generally passes them to a database query. The list method added to domain objects by grails handles these parameters, and the finder methods take a queryParams . The usual pattern is to pass the params object directly to list or as the queryParams parameter to the finders. This returns a result set starting at the given offset, with one page length.

In your example, you're calling getAllWithDuplicateIDs without making use of these parameters. Update your query to take them, like this:

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

Alternatively, page it in memory with something like

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

Paging directly in the query is preferable, since it will perform better handle cases where the entire list doesn't fit in memory.

Provide max and offset function params this:

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

And use them in in query to database.
Or check the answer how to get results from list with max and offset in answer here

Look at this example .

Domain class ..

class Job {

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

}

Controller Code..

    def uijobs () {

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

    }

and view is here.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM