Reputation: 1017
Let me give a little background.
I have a domain model that stores say data on users, I have a separate domain model for the suers phone numbers as they can have more that 1 so one user could have many phone numbers.
I have the data for a user stored in the system and say 2 phone numbers for that user. Then on a page I have Select list that is designed to call an action on the controller and then pass that users phone numbers data back to the view to be rendered.
The code for each of the elements are below:
The Controller Function:
def getNumbers = {
def user = User.get(params.phoneId.toInteger())
[phones: user.phones]
}
The View:
<g:select id="users" name="user_field" from="${Users}"
optionKey="id"
optionValue="name"
noSelection="['':'-Choose a Name-']"
onchange="${remoteFunction(action: 'getNumbers',
update: [success: 'great', failure: 'ohno'],
params: [userId: this.value ],
options: '[asynchronous: false]')}" />
<g:each in="${phones?}" var="a" status="i">
<div>
<g:textField name="number" required="" value="${phones?.data}"/>
</div>
</g:each>
When running this code the call does get made to the Action getNumbers however for some reason I get the following error message:
| Error 2013-07-03 14:25:37,700 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver - NumberFormatException occurred when processing request: [POST] /app/user/getNumbers - parameters:
userId: null
For input string: "null". Stacktrace follows:
User: For input string: "null"
This is strange that the param passed would be null as I have the data from this list also be displayed in a text box on the page via the JavaScript below and the value is not null and displays correctly:
document.getElementById("verification").innerHTML = "<br />" + this.value;
I hope someone can help me to correct this issue or even tell me another way to achieve what I want.
Thanks in advance.
Upvotes: 0
Views: 1305
Reputation: 166
Try thi steps :
1 - create a template : _phones.gsp
<g:each in="${phones?}" var="a" status="i">
<option value="${phones?.data}">${phones?.data}</option>
</g:each>
2 - In your first select :
<g:select id="users" name="user_field" from="${Users}"
optionKey="id"
optionValue="name"
noSelection="['':'-Choose a Name-']"
onchange="${remoteFunction(action: 'getNumbers',
update: [success: 'great', failure: 'ohno'],
params: '\'userId=\'+this.value',
options: '[asynchronous: false]')}" />
3 - In your second select put this :
<select id="great">
</select>
4 - In your controller : def getNumbers = {
// You get the phones by User.id, i thik so :-) 'users' is a first select and contains the id selected, and provide the find in phones.
def user = User.get(params.users.toInteger())
[phones: user.phones]
}
So sorry for my bad english ;-)
Upvotes: 1