2013-10-04 41 views
0

我在創建和編輯gsp中呈現的_form.gsp上有一個動態下拉列表。它工作正常,但我希望選定的值顯示在edit.gsp上。Grails動態下拉列表爲edit.gsp添加選定的值

_form.gsp

<g:select name="plant.id" id="plant" from="${MCM.MGPlant.list(sort:'member')}" value="${MGMatriceInstance?.plant?.id}" optionKey="id" noSelection="${[null: 'Select One...']}" 
      onchange='loadCostCenter(this.value);'/> 
    <g:select id="costCenter" name="costCenter.id" from="${[]}" optionKey="id" noSelection="${[null: 'Select One...']}"/> 


<script type="text/javascript">   
     function loadCostCenter(init) 
      { 
       var root="${resource()}"; 
       var plantid = document.getElementById('plant').value; 
       var url = root+'/MGMatrices/findCostCenterForPlant?plantId='+plantid; 

       jQuery('#costCenter').load(url); 
      }   
</script> 
+0

嘗試在植物選擇,使用價值屬性 – Visme

+0

我添加它和它的工作。我也希望第二個下拉列表也顯示選定的值。但它並沒有在那裏工作。 – Jerika

+0

在我的回答欄中看到更新的代碼 – Visme

回答

0

在下面的例子中,當選擇單擊該國,它顯示在阿賈克斯的國家locaions。替換國家的工廠和代替地點的成本。

在_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] 

} 
+0

它沒有工作。 jQuery函數沒有被調用。我的代碼有什麼解決方法嗎? – Jerika

+0

你必須在創建和編輯頁面中包含jquery。 – Visme

+0

是的,我做到了。沒有必要爲費用ID添加文本框。 – Jerika

相關問題