我的域类的简化如下所示:

class ScheduleTimeExp{
     Data date
}

class ScheduleAction{
     ScheduleTimeExp scheduleTimeExp
}

我的gsp中有一个选择框:
<g:select id="scheduleTimeExp" name="scheduleTimeExp.id" from="${tao.marketing.ScheduleTimeExp.list()}" optionKey="id" required="" value="${tao.marketing.ScheduleAction?.scheduleTimeExp?.id}" class="many-to-one"/>

而是让用户从所有ScheduleTimeExp中选择
from="${tao.marketing.ScheduleTimeExp.list()}"

我只想显示那些其他任何ScheduleAction都不存在ScheduleTimeExp和ScheduleAction之间的关系的ScheduleTimeExp。换句话说,只有那些尚未在另一个ScheduleAction中选择的时间表达式。

最佳答案

为了从表示分离中强制执行逻辑,您想在 Controller Action 中填充列表:

def dataSource

def someAction() {
  final Sql sql = new Sql( dataSource )
  def scheduleTimeExps = sql.rows( 'select ste.id, ste.date from Schedule_Time_Exp ste where ste.id not in (select sa.Schedule_Time_Exp_id from Schedule_Action sa)' ).collect{ it as ScheduleTimeExp }
  [ ..., scheduleTimeExps:scheduleTimeExps ]
}

注意:确保表和列的大小写和命名正确

然后在gsp中:
<g:select .. from="${scheduleTimeExps}"/>

10-06 09:08