0

grailsアプリケーションのajaxライブラリとしてjqueryを使用しています。別のドロップダウンから入力されたドロップダウンリストを取得しましたが、値が表示されません。

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

    }
4

3 に答える 3

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>
于 2012-12-23T17:46:04.990 に答える
1

更新する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
        }
    }
}

}

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
}

とproperiteNameIdGSPのみを含む単純なGroovyクラスはどこにありますかint idString name

于 2012-12-23T15:31:40.457 に答える
0

問題はupdate: [success: 'provinceddl']、レンダリングされる要素で要素のHTML属性を設定することを除いていることです。おそらく必要なのは、ラッピングdivを作成し、そのdivを更新することです。<g:select />次に、そのデータを使用してその全体を再度レンダリングします。これで答えられない場合は、後で例を挙げて編集します。

于 2012-12-23T15:17:16.843 に答える