嗨,我是Grails的新手,我在grails中有一个非常简单的问题,希望有人可以帮助我。
我有一个简单的域类Person,如下所示:
class Person {
String name // name of the person
Date dob // date of birth
} // end of class
我的数据库中已经有一些Person条目。
在另一种形式中,我想让用户从下拉列表中选择年龄在21岁以上的人的姓名。现在看起来像这样:
<fieldset class="form">
<f:field bean="Person" property="name" />
</fieldset>
如何从数据库中过滤所有其他人员,仅显示21岁以上人员的姓名?
提前非常感谢您!
最佳答案
在 Controller 中搜索年龄超过21岁的人员,然后将其传递给gsp进行显示。像这样:
import groovy.time.TimeCategory
def create() {
// all your other codes
def adults = Person.findAllByDobGreaterThanEquals(new Date() - 21.year)
// other codes....
respond new Person(params), model:[adults :adults]
}
然后将其与结果集一起显示在gsp中。
<fieldset class="form">
<f:field bean="Person" property="name" >
<g:select name="name" from="${adults}" optionKey="id" />
</f:field>
</fieldset>
关于select - Grails f:字段显示选定下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46030862/