填充Grails g:从功能中选择

时间:2012-12-28 20:39:25

标签: grails groovy gorm

尝试填充g:从控制器中的某个功能中选择,是否可以?

目前我们有这个:

def run(Long id) {

    def reportInstance = Report.get(id)

    def listPromptValues = populatePrompts(reportInstance)*.values()

    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 each prompt in the report, go out and get it's values
    rp.prompts.collectMany {
        reportService.listDatatypeValues(it.datatype)
    }

}

然后g:选择问题是

<li class="fieldcontain">
                <g:each var="prompt" in="${reportInstance.prompts}">
                    <span id="prompts-label" class="property-label">
                        <g:message code="report.prompts.label" default="${prompt.name}:" />
                    </span>
                    <g:if test="${prompt.datatype.type == 'DropDown'}">                         
                        <g:select id="prompt.name" from="${listPromptValues}" name="prompt.name" value="" noSelection="['':'']"/>
                        <br>                            
                    </g:if>
                </g:each>
            </li>

如果只有一个提示,哪个工作正常,但是如果循环中有多个提示,我们需要能够直接从gsp视图调用populatePrompt函数,可能发送reportId然后返回listPromptValues。我似乎无法让onChange(remoteFunction ...)正常工作,并且在搜索庞大的Google网站时空手而归。

类似于createLink的工作原理

${createLink(controller:'report', action:'runReport', id:"${reportInstance.id}")}

但是,而不是createLink,它将是select标签的from属性,如:

<g:select id="prompt.name" from="{(controller:'report',action:'populatePrompt', id:"${reportInstance.id}")}" name="prompt.name" value="" noSelection="['':'']"/>

有任何想法或方向吗?

1 个答案:

答案 0 :(得分:1)

我认为@JamesKleeh在他的最后评论中提出了一个可行的解决方案。

鉴于您的gsp结构非常静态,获取动态加载的提示选择选项没有意义。只需在控制器的listPromptValues内的List包中返回这些选项,然后直接将其放入gsp中。

关于[prompt1: ['a','b','c'], prompt2: ['d','e','f']]等参数,您可以在populatePrompts方法中获取此地图,并将每个键值对放入gsp选择标记中。像这样:

<强>控制器

{    ....
    def listPromptValues = populatePrompts(reportInstance)
    ....
}

def populatePrompts(Report rp){
    //for each prompt in the report, go out and get it's values
    def promptMap = [:]            //map to be returned
    rp.prompts.each {
        promptMap.put(it.name, reportService.listDatatypeValues(it.datatype))
    }
    return promptMap
}

<强> GSP

                <g:each var="prompt" in="${reportInstance.prompts}">
                    <span id="prompts-label" class="property-label">
                        <g:message code="report.prompts.label" default="${prompt.name}:" />
                    </span>
                    <g:if test="${prompt.datatype.type == 'DropDown'}">                         
                        <g:select id="prompt.name" from="${listPromptValues[prompt.name]}" name="prompt.name" value="" noSelection="['':'']"/>
                        <br>                            
                    </g:if>
                </g:each>
相关问题