grails - 使用g:select with service中的查询的Grails

标签 grails groovy gorm

这对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/

相关文章:

groovy - 如何从 Gradle 0.6 引用类路径

groovy - EachWithIndex groovy语句

hibernate - Grails GORM-查找表

grails - 用枚举类型查询时,GRAILS GORM显示错误结果

database - 使用 executeUpdate() 进行 Grails 悲观锁定

grails - 对空关联使用Grails GORM标准

ajax - Grails,删除按钮与编辑 View 中的“更新”具有相同的形式

session - 如果之前未访问 session ,则Grails 2.3.0无法访问 View 中的 session

scripting - Windows 批处理脚本的良好替代方案?

hibernate - 如何编写此Grails GORM Hibernate查询