Search code examples
grailsgrails-controller

Grails - sorting list of entities on gsp using sortableColumn


I have the following entity:

class User {

    String name
    String contactName
    String primaryEmail
    String url
    String phoneNumber
    String address
    //..
}

I have gsp that display list of all users and I want to be able to sort users by contactName, name and phoneNumber, in the controller I have list() method that just returns list of all users:

class UserController {
     ...

    def list() {
        [users: User.all]
    }
}

My gsp is like this:

<body>
<table>
    <thead>
    <tr>
        <g:sortableColumn property="contactName" title="Bookmaker"/>
        <g:sortableColumn property="name" title="Name"/>
        <g:sortableColumn property="phoneNumber" title="Phone"/>
    </tr>
    </thead>
    <tbody>
    <g:each in="${users}" status="i" var="user">
        <tr>
            <td>${user.contactName}</td>
            <td>${user.name}</td>
            <td>${user.phoneNumber}</td>
        </tr>
    </g:each>
    </tbody>
</table>
</body>

But when I click on one of the sortable columns request is sent (something like this: "list?sort=phoneNumber&order=asc") but response is always the same. Now it seems to me like code that actually sorts the list should be implemented by myself. Or am I doing something wrong? Thanks!


Solution

  • you again :)

    def list() {
        [users: User.list( params )]
    }