【发布时间】:2012-12-28 20:39:24
【问题描述】:
尝试从我的控制器中的函数中填充 g:select,这可能吗?
目前我们有这个:
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:select 是
<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然后取回列表提示值。我似乎无法让 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="['':'']"/>
有什么想法或方向吗?
【问题讨论】:
-
我无法回答您的问题,可能是因为您的数据对我来说毫无意义。
-
好的@JamesKleeh。我们有一个报表对象,它又具有提示。提示是存储在数据库中的值,我需要通过控制器在 UI 上填充它们。提示可以有不同的值,比如说一个是一年选择,另一个是一个月。我将如何使用该数据填充 g:select?
-
listPromptValues 已经被发送到视图。当前的 g:select 工作正常吗?里面还需要哪些其他数据?
-
假设此报告有两个提示,当在控制器中调用 run 时,它将返回 listPromptValues 以及两个提示的值在一个大列表中。我需要根据提示的数量取回 2 个列表或 n 个列表。每个提示都应该从控制器返回它自己的值列表。
-
你可以发送一张包含所有数据的地图,你不需要用ajax来做。前
[prompt1: ['a','b','c'], prompt2: ['d','e','f']]
标签: grails groovy grails-orm