在下面的示例中,当单击 select 中的国家时,它会在 ajax 中显示国家的位置。以国家代替工厂,以地点代替成本。
在 _temp1 模板中,仅放置植物和 div(用于成本下拉菜单)和一个文本框(用于存储来自编辑的成本 ID)。
为成本创建另一个模板。这将在动态选择工厂时显示成本。
在编辑操作中,您必须发送 plantInstance 和 costInstance 才能查看。
在 create.gsp 和 edit.gsp 中
...........
<script type="text/javascript">
jQuery(function() {
getSelectedCountry();
});
function getSelectedCountry(){
var selectedCountry = jQuery("#country").val();
var locationId = jQuery("#locationId").val();
alert(locationId)
if(selectedCountry != ''){
if(locationId!=''){
${remoteFunction (controller: 'country', action: 'locations', params: '\'countryId=\' + selectedCountry+\'&locationId=\'+locationId', update: 'updateLocation')}
}
else
${remoteFunction (controller: 'country', action: 'locations', params: '\'countryId=\' + selectedCountry', update: 'updateLocation')}
}
}
</script>
......
<g:render template="form" />
......
在 _form.gsp 中:
<g:select name="country" id="country" from="${com.ard.Country.list()}"
optionKey="id" noSelection="${['': 'Select']}" value="${countryInstance?.id}"
onchange='getSelectedCountry();'/>
<div id="updateLocation"></div>
<g:textField name="locationId" value="${locationInstance.id}"/>
在 _locations.gsp 中
<g:select id="location" name="location" from="${locations}" value="${locationInstance.id}"optionKey="id" noSelection="${[null: 'Select One...']}"/>
在控制器动作中,
def locations(){
def country = Country.get(params.countryId)
def locations = country.locations
def loc
if(params.locationId)
loc = Location.get(params.locationId)
render(template:"locations",model[locations:locations,locationInstance:loc])
}
在编辑动作中:
edit(){
......
render(view:"edit" ,model:[countryInstance:country,locationInstance:locationInstance]
}