Ajax 级联操作

view层:
<script language="javascript">
function callListByDp()
{
var tdid = document.getElementById("tempDpSelect").value;
${remoteFunction(controller:'users',action:'remoteListUsersByDp',params:'\'tempdpid=\'+tdid',update:'tempUserSelecgt')
}
</script>
<g:select id="tempDpSelect" onchange="callListByDp();" optionKey="id" optionValue="dp_name" from="${tbsShelfInstanceList}" value="${tempdpid}" name="dp"
<span id="tempUserSelect">
<select><option>用户名</option></select>
</span>
主要由一个javascript和一个g:select 组成,另有一个返回值的区域span id="xxx"。
g:select 中的 onchange="callListByDp();" 使你每选一个选项,就会去执行javascript中的 callListByDp()
而此function 的功能就是获取g:select的当前值,传递给controller.此例中的controller为users,action为remoteListUsersByDp ,params为tempdpid=g:select中的当前值。
controller中要注意,返回一个templete.此处为直接返回了一个g:select
def remoteListUsersByDp = {
if (!params."tempdpid" || params."tempdpid" == "null"){
render(g.select(optionKey:"id",from:null,name="tempUserSelect")
}
else{
def users=Users.findAllByDp(Dp.get(params.int("tempdpid")))
render(g.select(optionKey:"id",optionValue:"emp_name",from:users, name="users", id="uid"))

本文选自金丝阁

posted on 2017-03-11 12:46  哈哈哒  阅读(150)  评论(0编辑  收藏  举报

导航