限制某些域类项目的选择

时间:2016-11-08 20:23:51

标签: grails

My Domain Classes简化如下:

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()}"

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

1 个答案:

答案 0 :(得分:0)

要从表示分离强制执行逻辑,您需要在控制器操作中填充列表:

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}"/>