John Little
John Little

Reputation: 12343

How to sort columns in Grails view

In the index method, I list records for a particular player.

e.g.

def index() {
    Player player = Player.get( params.playerId)
    def records = Record.findAllByPlayer(player, params:params)

    [records: records,  playerInstance: player, params: params]
}

This works, but none of the sortable columns work in the view.

How do I look up which column I need to sort, and how to I then use this information in findAllByXXX? I have scoured Google but have not found any examples which don't involve the simple "Books.list(params)" pattern.

In the view, I have sortableColumns:

<g:sortableColumn property="createdAt" title="${message(code: 'paymentRecord.created.label', default: 'Created at')} "
                  params="${params}" style="width: 15%;"/>

and I see the parameters at the end of the URL, e.g.:

 /index/1?sort=credit&order=desc

Upvotes: 0

Views: 1176

Answers (2)

Sudhir N
Sudhir N

Reputation: 4096

Pagination and sorting parameters which are available on list method can also be used with dynamic finders.

So you can do it like this, if you have sort and order available in params.

Record.findAllByPlayer(player, params)

Or

Record.findAllByPlayer(player, [max: xx, offset: xx, sort: "name", order: "desc"])

Upvotes: 2

Ramsharan
Ramsharan

Reputation: 2064

You need to do Record.findAllByPlayer(player, params) instead of Record.findAllByPlayer(player, params:params).

You can see examples here in grails documentation. One example from the documentation is

def results = Book.findAllByTitle("The Shining",
              [max: 10, sort: "title", order: "desc", offset: 100])

Upvotes: 2

Related Questions