Grails动态选择列表javascript

时间:2014-04-16 22:22:44

标签: javascript ajax grails

我在主模板表单上有一个动态选择列表,它将读取前一个选择列表中的值并相应地填充选项。此选择列表在创建屏幕上运行良好,但在编辑屏幕上,选择列表将显示列表中的所有选项,而不是与上面的选择列表关联的选项。在我的主布局页面中,我有一个onload函数,它根据警报触发并显示正确的顾问ID。

我的onload功能是

// This is called when the page loads to initialize contracts
var zselect = document.getElementById('consultant')
var zopt = zselect.options[zselect.selectedIndex]
window.alert("consultant value is" + zopt.value)
${remoteFunction(controller:'cdeEvaluation', action:'ajaxGetContracts', params:"/'id='/    + zopt.value", onSuccess:'updateContract(e)')}}

这是正确运行的onchange事件代码

function updateContract(e) {
    // The response comes back as a bunch-o-JSON
    var contracts = eval("(" + e.responseText + ")")    // evaluate JSON

    if (contracts) {
        var rselect = document.getElementById('contract')

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

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

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

控制器代码是

def ajaxGetContracts = {
     def cdeConsultant = CdeConsultant.get(params.id)
     render cdeConsultant?.contracts as JSON
}

这两个选项的gsp代码是

<g:select id="consultant" class="form-control" label="Consultant" required="" name="consultant"  from="${CdeConsultant.list()}" optionValue="ccf_consultant_firm"  value="${cdeEvaluationInstance?.consultant}" optionKey="id" noSelection="['':'Select Consultant']"
    onchange="${remoteFunction(
        controller:'cdeEvaluation', 
        action:'ajaxGetContracts', 
        params:'\'id=\' + escape(this.value)', 
        onSuccess:'updateContract(e)')}"
></g:select>

<g:select class="form-control"  label= "Contract*"  name="contract" id="contract" from="${Contract.list()}" optionValue="contractName" optionKey="id" noSelection="['':'Select Contract']" value="${cdeEvaluationInstance?.contract}"></g:select>

我在from =和window.onload函数中尝试了各种选项,似乎无法使其正常运行。谢谢!

0 个答案:

没有答案