问题描述
让我给一点背景.
我有一个域模型来存储用户的说说数据,我有一个单独的域模型来处理用户的电话号码,因为他们可以拥有超过1个,因此一个用户可以有多个电话号码.
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.
我在系统中存储了一个用户的数据,并为该用户说了2个电话号码.然后在页面上,我具有选择"列表,该列表旨在调用控制器上的操作,然后将该用户的电话号码数据传递回要呈现的视图.
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.
每个元素的代码如下:
控制器功能:
def getNumbers = {
def user = User.get(params.phoneId.toInteger())
[phones: user.phones]
}
视图:
<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>
运行此代码时,确实会调用Action getNumbers,但是由于某些原因,我收到以下错误消息:
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"
奇怪的是,传递的参数为null,因为我拥有此列表中的数据,还通过下面的JavaScript在页面上的文本框中显示,并且该值不为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.
谢谢.
推荐答案
尝试以下步骤:
1-创建一个模板:_phones.gsp
1 - create a template : _phones.gsp
<g:each in="${phones?}" var="a" status="i">
<option value="${phones?.data}">${phones?.data}</option>
</g:each>
2-第一次选择:
<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-在您的第二选择中,输入以下内容:
3 - In your second select put this :
<select id="great">
</select>
4-在您的控制器中: def getNumbers = {
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 ;-)
这篇关于Grails选择RemoteFunction不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!