2012-07-10 35 views
0

我在我的視圖區域有這個問題,其中我有此<g:select>標記具有onChange屬性來檢索引用到所述標記的選定值的數據。不幸的是,我們在<g:formRemote>中包含<g:select>標籤,以便通過<g:formRemote>的URI檢索數據,而無需刷新頁面。Grails g:formRemote自動檢索

<!-- The g:formRemote of the view --> 
<g:formRemote name="formRemote" uri:[controller:'test', action:'functionCall']> 
    <g:select name="selectedValue" from="${['AA','BB']}"/> 
    ${returnData} 
</g:formRemote> 

//The closure where the formRemote will be calling [TestController.functionCall] 
def functionCall(){ 
    println 'entered' 
    def returnData = '' 
    if(params.value == 'AA') 
     returnData = 'aaa' 
    else if(params.value == 'BB') 
     returnData = 'bbb'  

    [data:returnData] 
} 

問題是,我們不能找到一種方法,以檢索來自控制器的模型數據回視圖[參考與<g:formRemote>]時的<g:select>值變化。

回答

0

我們最終知道我們並不需要<g:formRemote>標籤來讓理論發揮作用。我們缺乏onChange="${remoteFunction ...}"參數到<g:select tag>以下格式:

<g:select 
    name="selectedValue" 
    from="${['AA','BB']}" 
    optionKey="<the id of the model with reference to line#9>" 
    onChange="${remoteFunction(
    controller:'test', 
    action:'functionCall', 
    update:[success:'updateDiv'], 
    params:'\'id=\' + escape(this.value)', 
    onComplete='displayVal(this)')}"/> 

updateDiv,也位於.gsp,負責持有的觀點從控制器渲染封閉functionCall

<div id="updateDiv"></div> 

而且TestController.functionCall有此內容:

def functionCall(){ 
    println 'entered' 
    def returnData = '' 
    if(params.value == 'AA') 
     returnData = 'aaa' 
    else if(params.value == 'BB') 
     returnData = 'bbb'  

    render(view:'<can be a template or an html page>', model:[data:returnData]) 
} 
0

您的選擇標記不必位於formRemote標記內,具有remoteFunction調用的常規窗體標記將執行該操作。

http://grails.org/AJAX-Driven+SELECTs+in+GSP

在GSP選擇標記:

<form> 
    <g:select 
     optionKey="id" optionValue="name" name="country.name" id="country.name" from="${Country.list()}" 
     onchange="${remoteFunction(
      controller:'country', 
      action:'ajaxGetCities', 
      params:'\\'id=\\' + escape(this.value)', 
      onComplete:'updateCity(e)')}" 
    ></g:select> 
    <g:select name="city" id="city"></g:select> 
</form> 

javascript函數在GSP將數據發送到的grails控制器:

<g:javascript> 
    function updateCity(e) { 
     // The response comes back as a bunch-o-JSON 
     var cities = eval("(" + e.responseText + ")") // evaluate JSON 

     if (cities) { 
      var rselect = document.getElementById('city') 

      // Clear all previous options 
      var l = rselect.length 

      while (l > 0) { 
       l-- 
       rselect.remove(l) 
      } 

      // Rebuild the select 
      for (var i=0; i < cities.length; i++) { 
       var city = cities[i] 
       var opt = document.createElement('option'); 
       opt.text = city.name 
       opt.value = city.id 
       try { 
        rselect.add(opt, null) // standards compliant; doesn't work in IE 
       } 
       catch(ex) { 
        rselect.add(opt) // IE only 
       } 
      } 
     } 
    } 


    // This is called when the page loads to initialize city 
    var zselect = document.getElementById('country.name') 
    var zopt = zselect.options[zselect.selectedIndex] 
    ${remoteFunction(controller:"country", action:"ajaxGetCities", params:"'id=' + zopt.value", onComplete:"updateCity(e)")} 

</g:javascript> 

Grails的控制器:

import grails.converters.* 
class CountryController { 

    def ajaxGetCities = { 
     def country = Country.get(params.id) 
     render country?.cities as JSON 
    } 
}