FrancescoDS
FrancescoDS

Reputation: 995

grails controller: access to parameter that has a list of elements

I need to access to items stored in a parameter that represents selected elements in a multiselect. I pass selected items from gsp to controller with the following code into the remoteFunction:

params: '\'receiptItemsSelected=\' + jQuery(this).val()'

Now, following the code found in discussion here, I use the closure to get each value, but if I perform a multiselect, the size of receiptItemsSelected is always 1, but value is, for example, 1,2. To get values as a list I've done the following in the controller

params.list("receiptItemsSelected")

but it does not give me two elements if I select two items in the multiselect, but always one element. The question is: if I select two elements, how can I get each element and use it in the controller? And how can I have that elemnts as Long and not as String? Thanks

Upvotes: 2

Views: 4403

Answers (2)

codelark
codelark

Reputation: 12334

If you're parameters are being passed with string representation of a list, e.g.:

http://yoursite.com/?receiptItemsSelected=1,2,3

You have to split the value using normal Groovy string manipulation and perform the type conversion yourself:

def receiptsAsLongs = params.receiptItemsSelected.split(',')*.toLong()

If your parameters are passed with the convention of repeated parameters makes a list, e.g.:

http://yoursite.com/?receiptItemsSelected=1&receiptItemsSelected=2

Then grails can convert this to a list for you using params.list(), but you must do the final String to Long conversion:

def receiptsAsLongs = params.list('receiptItemsSelected')*.toLong()

Upvotes: 8

Ian Roberts
Ian Roberts

Reputation: 122364

params.list() is intended for multi-valued parameters, i.e. it will work if you have

receiptItemsSelected=1&receiptItemsSelected=2

You may have more luck using serialize() rather than val() to build the request body.

Upvotes: 0

Related Questions