Grails GORM:可以查询州和县

Grails GORM:可以查询州和县,grails,gorm,Grails,Gorm,目前正在使用Grails2.43。我有两个域类(State和country),以及State和country的两个Select下拉列表 GORM finder有没有一种方法可以在选择某个州时,动态查询返回该州的特定县。从而排除不在所选州的所有其他县 表单元素: <g:select name="locationState" class="form-control" from="${....State.list().sort{it.orderNumber}}"> <g:selec

目前正在使用Grails2.43。我有两个域类(State和country),以及State和country的两个Select下拉列表

GORM finder有没有一种方法可以在选择某个州时,动态查询返回该州的特定县。从而排除不在所选州的所有其他县

表单元素:

<g:select name="locationState" class="form-control" from="${....State.list().sort{it.orderNumber}}">

<g:select name="locationCounty" class="form-control" from="${...State.FindByName(it.orderNumber).counties}">

这取决于您如何关联域类。如果州有多个县和/或县属于州,则可以使用动态查找器。例如:

// All Counties 
County.list()
// All States
State.list()
//All Counties in State Vermont
State.findByName("Vermont").counties
//State for a county
County.findByName("Chittenden").state
如果你想制作一个动态的表单,你可以先显示你的州下拉列表,然后让县下拉列表处于非活动状态,直到选择了州。然后,您可以进行异步调用以获取该州的县,或者如果已加载完整的对象,您可以使用
selectedState.countries
填充县选择框


请参阅GORM对象关系映射:

谢谢。County vs Country大写背后的原因/用法是什么?我在提到域名类名时是大写的。“Countries”将是State类中的属性名称。我添加了State和Country类,以便您可以从结构上查看它们。我的hasMany语句是否正确?它是否有效才是真正的考验!看起来不错。你可能需要在County Classis中添加一个belongsTo state,我也提供了表单元素(不得不删除一些带有组织特定内容的标签,我无法显示)。特别是,根据你所提到的,我有第二个权利吗?
// All Counties 
County.list()
// All States
State.list()
//All Counties in State Vermont
State.findByName("Vermont").counties
//State for a county
County.findByName("Chittenden").state