Search code examples
javascriptgrailsgsp

Grails: How to display data from datasource based on selection in GSP form


I have a pulldown menu generated from a MySQL dataSource.

 <g:select onchange="selected()"  id="name" name="name" from="${listOfNames()}" noSelection="['':'--']"/>

this populates the menu with one column from my table

Now based on the selection I want to display additional columns from the same table as text on the gsp form.

so user selects Name1 from dropdown. I display underneath the dropdown menu

Name1. age 21. degree yes.

I have an action inside my controller called allDetails that queries all values associate with a name and returns it a string. But I am not sure how to pass it parameters

"${remoteFunction(action:'allDetails', params: \'name=\' params.name')}

What is the best way to do this. run a JavaScript remote function inside selected() or a gsp tag that calls the action somehow? how to then display the returned string? Change the innerHTML?

tried http://www.grails.org/AJAX-Driven+SELECTs+in+GSP


Solution

  • You cannot use remoteFunction or other core Grails tags, because its text is fully generated during page rendering, and you only know the selected name during Javascript executing in onchange.

    So use jQuery Ajax call (unless you switched to a different Javascript plugin).

    And I suggest that you select not a name, but an id of that person, like in a second g:select example.