简体   繁体   中英

Paginate doesn't word in grails

I have problem with paginate, I use grails 2.4.4

This my index.gsp:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title></title>
</head>

<body>
<div>
    <g:paginate controller="user" action="index" total="${userTotal}"/>
</div>
</body>
</html>

This is my controller:

class UserController {
    def index() {
        List<User> users = User.findAll()
    
        render(view: "index", model: [users: users, userTotal: 4])
    }
}

In the console I don't have any error and in my page I see nothing. Array users isn't empty, I checked.

render(view: "index", model: [users: users, userTotal: 4])

You have hardcoded userTotal to be 4. The paginate tag defaults to 10 items per page, so pagination is not required.

If you are concerned about pagination then User.findAll() is probably a bad idea because it will return all of the data.

Default scaffolding will do something like this which is a better idea...

Controller action:

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond userService.list(params), model:[userCount: userService.count()]
}

GORM Data Service:

import grails.gorm.services.Service

@Service(User)
interface UserService {

    User get(Serializable id)

    List<User> list(Map args)

    Long count()

    void delete(Serializable id)

    User save(User user)

}

The relevant GSP:

<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main" />
        <g:set var="entityName" value="${message(code: 'user.label', default: 'User')}" />
        <title><g:message code="default.list.label" args="[entityName]" /></title>
    </head>
    <body>
        <a href="#list-user" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
        <div class="nav" role="navigation">
            <ul>
                <li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
                <li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
            </ul>
        </div>
        <div id="list-user" class="content scaffold-list" role="main">
            <h1><g:message code="default.list.label" args="[entityName]" /></h1>
            <g:if test="${flash.message}">
                <div class="message" role="status">${flash.message}</div>
            </g:if>
            <f:table collection="${userList}" />

            <div class="pagination">
                <g:paginate total="${userCount ?: 0}" />
            </div>
        </div>
    </body>
</html>

If you really don't want to use a service (highly recommended that you do) then you could change the controller to do something like this...

params.max = Math.min(max ?: 10, 100)
respond User.list(params), model:[userCount: User.count()]

It is my solution:

gsp:

 <div class="content scaffold-list">
        <g:form controller="user" action="index">
            <label>Input user's name:</label>
    
            <div class="form-group">
                <g:textField name="userName"/>
    
                <g:submitButton name="search"/>
            </div>
    
    
            <g:each var="user" in="${users}">
                <p>User with name: ${user.name}</p>
            </g:each>
        </g:form>
    
        <div class="pagination">
            <g:paginate controller="user" action="index"
                        max="5" total="${userCount}"/>
        </div>
    </div>

controller:

def index(String userName) {
    initUsersAndPokemons()

    PagedResultList users = fourthService.findUsersByName(
            userName,
            params.int('max', 5),
            params.int('offset', 0))

    [users: users, userCount: users.getTotalCount()]
}

service:

PagedResultList findUsersByName(String userName, int max, int offset) {
    BuildableCriteria bc = User.createCriteria()
    bc.list(max: max, offset: offset) {
            like('name',userName)
        }
    }

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