grails在远程函数调用ajax之后在select中生成一个列表

时间:2014-06-04 10:43:07

标签: grails grails-2.0

我正在使用grails 2.1.1。我有一个选择框,在那里我正在通过remothFunction调用一个动作来更新另一个选择。该列表填充在控制器中,但它没有在所需的选择框中呈现。请有人帮帮我吗?以下是我的代码:

我的视图页面有两个选择和远程功能>>>

 <html>
<head>
    <title>Render District & Thana</title>

    <g:javascript plugin="jquery" library="jquery" src="jquery/jquery-1.8.0.js" />

    <script>
        function loadThana(districtId) {
            <g:remoteFunction controller="greeting" action="loadThanaByDistrict" update="thanaId" params="'districtId='+districtId"/>
        }
    </script>

</head>

<body>
<g:form controller="greeting" action="renderDistrictThana">
   Select District : <g:select
            name="districtId"
            id="districtId"
            from="${district}"
            optionKey="id"
            optionValue="districtName"
            noSelection="['null':'Select District']"
            onchange="loadThana(this.value)"/><br/>


   Select Thana : <g:select
            name="thanaId"
            id="thanaId"
            from="${thana}"
            optionKey="id"
            optionValue="thanaName"
            noSelection="['null':'Select Thana']"/><br/>

    <g:submitButton name="submit" id="submit" value="Render Distric & Thana"/>
</g:form>
</body>
</html>

我在第二个组合&gt;&gt;&gt;

中渲染列表的操作
def loadThanaByDistrict() {
    def district = District.get(params.districtId?.toLong())
    def thana = Thana.findAllByDistrict(district)

    render thana
}

1 个答案:

答案 0 :(得分:1)

您的控制器可能如下所示,

def loadThanaByDistrict(){
    def district = District.get(params.districtId?.toLong())
    def thana = Thana.createCriteria().list{eq('district',district)}
    String options
    thana.each{ options += "<option value='"+[replace it with your expected field]+"'>"+[replace it with your expected field]+"</option>"
    render options
}

我希望它能正常运作。