Grails 动态下拉列表为 edit.gsp 添加所选值

Grails dynamic dropdown add selected value for edit.gsp

本文关键字:添加 gsp edit 动态 下拉列表 Grails      更新时间:2023-09-26

我的 _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>

在以下示例中,当单击 select 中的国家/地区时,它会显示 ajax 中国家/地区的位置。在国家/地区更换工厂,在位置更换成本。

在_temp1模板中,仅放置工厂和div(用于成本下拉列表)和一个文本框(用于存储来自编辑的成本ID)。

为成本创建另一个模板。这将在"动态"中选择工厂时显示成本。

在编辑操作中,您必须发送工厂实例和成本实例才能查看。

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