<head>
<g:javascript library='jquery' />
<r:layoutResources />
<script type="text/javascript">
function getGroupsDetails(){
var selectedGroup = jQuery("#group").val();
if(selectedGroup != "")
${remoteFunction (controller: 'groups', action: 'details', params: '\'groupId=\' + selectedGroup', update: 'details')}
}
</script>
</head>
<body>
<div class="col-xs-12 col-sm-4">
<strong>By Group Name :</strong>
<g:select id="groups" name="groupName" from="${Groups.list()}" value="${groupsInstance?.id}" noSelection="['':'-Select-']" optionKey="id" onChange="getGroupsDetails();" />
<div id="details"></div>
</div>
</body>
</html>
详情行动如下
def details(){
println "in details"
def groupIns = Groups.get(params.id)
println groupIns.id
render(template: "details", model: [groupIns: groupIns])
}
现在它进入javascript以及细节控制器。在控制器中我也打印了id。但在gsp中它没有显示任何内容......
答案 0 :(得分:1)
在您的课程中加入toString。当对象打印我们的tostring返回gname
class Groups {
String gowner
String gname
int devicenum
static constraints = {
}
String toString() {
"$gname"
}
}
first.gsp:
在select中不需要list.gname(),因为我们包含toString()。在下面,当我们选择group时,它会调用javascript函数来调用更新特定div的动作。我正在做AJAX。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<g:javascript library='jquery' />
<r:layoutResources />
<script type="text/javascript">
function getGroupDetails(){
alert("Function calling");
var selectedGroup = jQuery("#group").val();
alert(selectedGroup);
if(selectedGroup != "")
${remoteFunction (controller: 'group', action: 'details', params: '\'groupId=\' + selectedGroup', update: 'details')}
}
</script>
</head>
<g:select id="group" name="groupName" from="${Groups.list()}"
value="${groupInstance?.id}" noSelection="['':'-Select-']" optionKey="id"
onChange="getGroupDetails();" />
<div id="details"></div>
控制器动作:
def details(){
println params.groupId
def groupIns = Group.get(params.groupId)
render(template: "details", model: [groupIns: groupIns])
}
_details.gsp:
Name:${groupIns.gname}
Owner:${groupIns.gowner}
Devicenum:${groupIns.devicenum}