我的视图中有dropdown (g:select)
,显示域类的列表。我想在点击保存按钮时打印出任何值。以下是视图中的脚本
<g:select params="[tablename: storehouseInstance?.tablename]" name="tablename" from="${Warehouse.list()}" optionKey="id" optionValue="${Warehouse.list()}" value="tablename"
noSelection="['':'Choose Table']"
onchange="${remoteFunction (
controller: 'warehouse',
action: 'updateSelect',
params: "'warehouse.id=' + this.value + storehouseInstance?.tablename" ,
param: "this.value",
update: [success:'fieldSelection'] //update: 'fieldselection'
)}"/>
在我的控制器中我有:
def save() {
println("params are "+ params)
println("Saving table name: " + params.tablename)
}
我将tablename设为'1'而不是应该是'MYTESTVALUE'的值
请帮助。
答案 0 :(得分:0)
假设仓库的名称属性为name
,请尝试改为:
<g:select params="[tablename: storehouseInstance?.tablename]"
name="tablename"
from="${Warehouse.list()}"
optionKey="name" // I changed this line
optionValue="name" // I changed this line too
value="tablename"
noSelection="['':'Choose Table']"
onchange="${remoteFunction (
controller: 'warehouse',
action: 'updateSelect',
params: "'warehouse.id=' + this.value + storehouseInstance?.tablename" ,
param: "this.value",
update: [success:'fieldSelection']
)}"/>
在g:select
中,发送到服务器的参数由optionKey
定义,下拉列表中显示的值由optionValue
从MVC纯度的角度来看,你不应该在GSP中进行查询,例如Warehouse.list()
。相反,您应该在服务/控制器中检索所需的数据,并通过模型将其传递给视图(GSP)。