使用Jquery填充dropdownlist的Grails远程功能

时间:2012-12-23 13:37:39

标签: jquery grails

我正在使用jquery作为我的grails应用程序的ajax库。我有一个从另一个下拉列表中填充的下拉列表,但没有显示任何值。

GSP:

  <label for="countryddl" >Country:</label>
  <g:select name="countryddl" id="countryddl" from="${locations.country}"  
            keys ="${locations.country}"
            noSelection="['':'Select one...']"
           onChange="${remoteFunction( action:'updateProvince',
                                          params: '\'id=\'+escape(this.value)',
                                          update: [success: 'provinceddl'] )}"

  ></g:select> <br/><br />
    <label for="provinceddl" >Province:</label>
  <g:select name="provinceddl" id ="provinceddl" noSelection="['':'Select one...']" from=""></g:select>

控制器:

def updateProvince = {
        def country = params['id']
        def locations = Location.findAllByCountry(country)
        render locations.province as JSON

    }

3 个答案:

答案 0 :(得分:4)

GSP:

  <label for="countryddl" >Country:</label>
  <g:select name="countryddl" id="countryddl" from="${locations.country}"  
       keys ="${locations.country}"
       noSelection="['':'Select one...']"
       onChange="${remoteFunction( action:'updateProvince',
                     params: '\'id=\'+escape(this.value)',
                     update: [success: 'provinceddl'] )}"

  ></g:select> 
  <br><br>
  <div id="provinceddl">
      <p>Provinces will be loaded here according to country selected</p>
  </div>

控制器:

def updateProvince = {
        def country = params['id']
        def locations = Location.findAllByCountry(country)
        render(template:'result', model:[provinces: locations.collect{it.province}]
    }

_result.gsp

<label for="provinceddl" >Province:</label>
    <g:select name="provinceddl" noSelection="['':'Select one...']" from="${provinces}">
</g:select>

答案 1 :(得分:1)

更新select的javascript函数:

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
        }
    }
}

}

在GSP上:

<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
}

NameIdGSP是一个简单的groovy类,只包含int idString name

答案 2 :(得分:0)

问题是update: [success: 'provinceddl']除了用呈现的内容设置元素的HTML属性外。你可能想要的是创建一个包装div并更新该div。然后使用该数据再次渲染整个<g:select />。如果这不能为您解答,我稍后将通过示例进行编辑。