Grails Ajax 实现 联动选择
如果在jsp中添加<g:javascript library="prototype" />再使用g:remoteLink,g:formRemote等可实现异步刷新。
查看生成的html会发现主要实现代码如下
new Ajax.Updater('Bdiv','/Test/test/b', {asynchronous:true,evalScripts:true});return false;
<html><head><title>test</title><g:javascript library="prototype" /><script language="JavaScript">function sel(){ var select = document.getElementById("A"); var index = select.selectedIndex; var a = select.options[index].value; new Ajax.Updater('Bdiv','/Test/test/b?Aid='+a, {asynchronous:true,evalScripts:true});return false; }}</script><body><select id="A" onChange="sel();"><option vaule="1">1</option><option vaule="2">2</option><option vaule="3">3</option></select><div id="Bdiv"></div></body></html>
<select id="B"><g:each in="${BB}" var="bInstance"> <option value="${bInstance.id}">${bInstance.name}</option></g:each></select>
class TestController{ def index = { redirect(action:a,params:params) } def a={[params:params]} def b={ render(action:"b",model:[BB:B.findAllByA(params.Aid)]) }}
class B{String nameInteger A}