这对Grails模型来说还很新,在使用服务进行数据库事务时遇到了一些麻烦。
服务:
class ReportService {
def dataSource
def listDatatypeValues(Datatype dt) {
def sql = new Sql(dataSource)
def list = sql.rows (dt.statement)
return list
}
}
Controller :
def run(Long id) {
def reportInstance = Report.get(id)
def listPromptValues = populatePrompts(reportInstance)
if (!reportInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'report.label', default: 'Report'), id])
return
}
[reportInstance: reportInstance, listPromptValues: listPromptValues]
}
def populatePrompts(Report rp){
//for a prompt in the report, go out and get it's values
rp.prompts.each {
List list = reportService.listDatatypeValues(it.datatype)
}
}
查看摘要:
<g:if test="${reportInstance?.prompts}">
<li class="fieldcontain">
<g:each var="prompt" in="${reportInstance.prompts}">
<g:if test="${prompt.datatype.type == 'DropDown'}">
<g:select id="prompt.name" from="${listPromptValues}" name="prompt.name" value="" noSelection="['':'']"/>
</g:if>
</g:each>
</li>
</g:if>
我们有一个报表对象,其中包含提示,而提示又包含数据类型。对于任何给定的报告,将其放在UI上时,它将提供报告详细信息,然后列出给定提示的提示值。问题是当前设置将对象引用列为提示值,而不是服务返回的值列表。
例如,报告1具有2个提示,开始术语代码和结束术语代码。由于这是相同的SQL查询,因此它们都使用术语代码作为数据类型,并且从listDataTypeValues返回的列表将是存储在数据库中的70多个术语代码的列表。
有什么想法或方向吗?
我尝试了跟this一起使用,但无法正常工作。
谢谢!
最佳答案
您的populatePrompts
函数未返回有意义的值。如果使用collectMany
而不是each
进行迭代,则表达式的值将是查询中所有结果的串联。尝试这样的事情:
def populatePrompts(Report rp){
rp.prompts.collectMany {
reportService.listDatatypeValues(it.datatype)
} //.unique()
}
您可能还希望对结果调用唯一,以避免
g:select
输入中重复。
关于grails - 使用g:select with service中的查询的Grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14061315/