Refresh a combobox options using ajax

D

droid

Guest
If I have a person controller which gets a list of people in the database passes that list to the view as listOfPeople and my view has a combobox defined like so:

<g:select name="somePerson" from="${listOfPeople}" value="${personInstance?.name}" valueMessagePrefix="person.name" noSelection="['': '']"/>

I just want to have a button next to this combobox labelled refresh, which goes back to the database and gets a fresh list of people and put them in the combobox option list and done using ajax because I don't want the rest of the form to refresh, how do I do that?

My main problem is to allow a user to add a person that hasn't been captured in the database. So I was thinking I would provide a button to say add person which opens a dialogbox. on submit and closing of the dialogbox, refresh the combobox and set the new person added as selected.

thanks
 

dave

Administrator
Staff member
The task here is to update the html contents of the select control.

You can easily use Grails ajax tags to generate the necessary ajax enabled link to perform the server request, and refresh the contents of the select box.

Look at g:remoteLink tag. You need to specify the controller, the action and the target element you wish to update, in your case update='somePerson' .

In your controller action, simply gather your data and finally return a String containing all your "<option>whatever</option>" elements.

As you might be using this ajax call to update the select from several places, look at creating a javascript function called refreshSomePerson() and instead of using remoteLink, use g:remoteFunction with the same parameters.
 
D

droid

Guest
Thanks, all sorted - learnt something

Thanks very much. Your suggestion to user refreshSomePerson() together with g:remoteFunction made be realise something that I was not aware of - being newbie and all i.e. use these grails functions inside of javascript. I saw all these functions and wasn't sure how to use them in my situation. All sorted now.


The task here is to update the html contents of the select control.

You can easily use Grails ajax tags to generate the necessary ajax enabled link to perform the server request, and refresh the contents of the select box.

Look at g:remoteLink tag. You need to specify the controller, the action and the target element you wish to update, in your case update='somePerson' .

In your controller action, simply gather your data and finally return a String containing all your "<option>whatever</option>" elements.

As you might be using this ajax call to update the select from several places, look at creating a javascript function called refreshSomePerson() and instead of using remoteLink, use g:remoteFunction with the same parameters.
 
Top