我想在我的 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/