要更新的 javascript 函数select
:
function updateSelect(e, selectId) {
// The response comes back as a bunch-o-JSON
var json = eval("(" + e.responseText + ")")
if (json) {
var rselect = document.getElementById(selectId);
// Clear all previous options
var l = rselect.length;
var selectedKey = "undefined";
while (l > 0) {
l--
var value = rselect.options[l].value;
var attr = rselect.options[l].getAttribute("selected");
if(attr != null && attr.trim().length > 0) {
selectedKey = value
}
rselect.remove(l);
}
// Rebuild the select
for ( var i = 0; i < json.length; i++) {
var j = json[i];
var opt = document.createElement('option');
opt.text = j.name;
opt.value = j.id;
if(j.id == selectedKey) {
opt.setAttribute('selected', 'selected');
}
try {
rselect.add(opt, null) // standards compliant; doesn't work in
// IE
} catch (ex) {
rselect.add(opt) // IE only
}
}
}
}
在普惠制上:
<tr>
<td>Teacher:</td>
<td><eb:select name="teacher" from="${ availableTeachers }"
optionKey="id" optionValue="fullName" id="teacher"
onchange="${remoteFunction(
controller:controllerName,
action:'ajaxGetClassesForTeacher',
params:'\'id=\' + escape(this.value)',
onComplete:'updateClasses(arguments[0])')}"
/></td>
</tr>
<tr>
<td>Class:</td>
<td><eb:select name="schoolClass" from="${ availableClasses }"
optionKey="id" optionValue="name"
id="schoolClass" /></td>
</tr>
<r:script disposition="defer">
function updateClasses(e) {
updateSelect(e, "schoolClass");
}
</r:script>
在控制器中:
def ajaxGetClassesForSchool(params) {
School school = School.get(params.id)
def classes = SchoolClass.findAll() {
eq("school", school)
}
classes = classes.collect() {
new NameIdGSP(id:it.id, name:it.name)
}
def json = classes as JSON
return json
}
一个简单的 groovy 类在哪里NameIdGSP
只包含int id
和String name
properites