grailsgsp

How to filter in a g:select?


I am new to Grails and gsp. I tried to find documentation on if this was possible - I found a similar question but was not able to successfully apply it.

I have this g:select statement:

<g:select name="users"  multiple="multiple"
     optionKey="id"
     optionValue="name"
     value="${user?.users*.id}"
     from="${user.getUsers()}"
/>

I was wondering if it was possible to add a filter to this, or a condition. I can only select users that are in a certain country (users.country).


Solution

  • I was wondering if it was possible to add a filter to this, or a condition.

    It is.

    It looks like you have a user property in the model and that object is an instance of some class that has a collection named users.

    Something in your app is putting user in the model, likely a controller action. Best practice is to not embed much logic in a GSP so I would recommend either the controller do the filtering and put the qualifying objects in a separate model variable that the GSP can reference instead of user.getUsers(), or you could create a custom GSP tag which accepts a user and generates the select.