0

我的 _form.gsp 上有一个动态下拉列表,它在创建和编辑 gsp 中都呈现。它工作正常,但我希望在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>
4

1 回答 1

0

在下面的示例中,当单击 select 中的国家时,它会在 ajax 中显示国家的位置。以国家代替工厂,以地点代替成本。

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

}
于 2013-10-04T07:04:42.237 回答