grails 2.1.0 链式选择关系

标签 grails grails-2.0 grails-domain-class grails-controller

我想在我的 View 页面中制作链选择框。我有国家选择框和城市选择框。在城市域中,我有国家 ID。现在我想在选择国家时显示特定国家的城市。但我不知道。我正在使用 grails 2.1.0。我已经对此进行了谷歌搜索并尝试了一些代码。但没有结果。我正在提供我的域、 Controller 和 View 。如何使事件发生变化,使用 country_id 创建城市列表并将其显示在城市选择框中?任何人都可以请帮我吗?

我的国家域名>>>

    package com

class Country {

     String name
    String abbr
    String language

static hasMany = [cities:City]

    static constraints = {
    }
}

我的城市域名>>>
    package com

class City {

   String name 
   String timezone

static belongsTo = [country:Country]

    static constraints = {
    }
}

我的国家控制员>>>
package com

import com.City
class CountryController {

    def index = { }

}

我的浏览页面>>>
<%@ page import="com.Country; com.City" %>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="layout" content="country" />
</head>
<body>
  <form>
    <g:select
      optionKey="id" optionValue="name" id="countryname" name="countryname"  from="${Country.list()}">
    </g:select>
    <g:select optionKey="id" optionValue="name" id="cityname" name="cityname" from="${City.list()}"></g:select>
  </form>
</body>
</html>

最佳答案

尝试进行 Ajax 调用以获取给定国家/地区的城市。

看到这个类似的问题:Populate dropdown list using ajax In grails

关于grails 2.1.0 链式选择关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16684526/

相关文章:

Grails 2.x createCriteria 'or' 不适用于嵌套关联

grails - 在grails中保存域对象

data-binding - Grails 自动将字符串强制转换为我的域类之一

java - Grails Spring 安全认证提供程序和自定义过滤器

rest - 有在Grails 2.3中使用REST-ful服务的经验吗?

rest - Grails 的内容协商如何处理相反的类型?

grails - authenticatedUser 为 null 但原则不为 null

elasticsearch - 如何编写 elasticsearchservice.search(Closure query, params, filter)?

grails - 使用JQuery UI的Grails中的日期选择器:找不到资源错误

hibernate - Grails排除了加载子属性或映射的可能性吗?