FrancescoDS
FrancescoDS

Reputation: 995

grails: remoteFunction with multiple select

I need to use the remoteFunction directive with a multiple select. The select is as follows:

<g:select name="receiptItems" from="${myproject.ReceiptItem.list()}"
     multiple="multiple" optionKey="id" optionValue="description" size="5"
     value="${receiptInstance?.receiptItems*.id}" class="many-to-many"
         onchange="${remoteFunction(
            controller: 'Receipt',
            action: 'sumReceiptItems',
            params: '\'receiptItemsSelected=\' + this.value',
            onSuccess: 'updateTotalAmount(\'totalAmount\', data, \'00000\')')}"/>

I have the sumReceiptItems action in the Receipt controller that takes the parameter receiptItemsSelected and use it to update another text field.

The problem is that this.value gives me only one selected value, that is the last one selected. I need to pass to controller all the selected values in the select. How can I do it?

Thanks for your precious help

Upvotes: 0

Views: 756

Answers (2)

Michael
Michael

Reputation: 33297

You can use JQuery to get the values:

var selections = new Array();

$("#receiptItems").change(function() {
   var value = $(this).val();
   selections[selections.length] = value;
});

You can add the value to a global defined list. In this way you get all the selections.

Upvotes: 0

Elias Dorneles
Elias Dorneles

Reputation: 23806

Just use jQuery's val() instead of this.value, that will get all the selected items:

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

Note that you have to import jQuery if you haven't used it in your project yet. You can do that simply using <r:require module='jquery' /> in the <head> section if you are using an up-to-date Grails version.

Upvotes: 1

Related Questions