我有域类:
class Template { String name; String prop1; String prop2; }
并使用 g:select 查看:
<g:select name="template.id" from="${Template.list()}" optionKey="id" onclick = "updateEditText(this.value);"/>
我需要将选定的对象设置为 g:select 值(但不是字符串),我认为它应该是这样的(但它不起作用):
<g:select name="template.id" from="${Template.list()}" optionKey="id" optionValue = "${it}" onclick = "updateEditText(this.value);"/>
*it - template.list() 中的对象
我的程序 updateEditText 需要从所选对象中获取所有属性:
<g: javascript> updateEditText(obj) { document.getElementByID("prop1").value = obj.prop1; document.getElementByID("prop2").value = obj.prop2; //etc } </g:javascript>
如果我使用“prop1”或“prop2”作为选项值,它可以正常工作,但这不是我需要的。 谁能帮我吗?
最佳答案
您无法将对象从 html 输入传递到 javascript。但是您可以使用 RemoteFunction 标记将选定的 id 传递到服务器,在服务器中检索对象并返回属性。像这样:
<g:select name="template.id" from="${Template.list()}" optionKey="id"
onchange="${remoteFunction(action: 'getProps',
params:'\'id=\' + escape(this.value)',
update: 'props')}"/>
<div id="props">Properties here</div>
在 Controller 中,您获取实例并呈现如下属性:
def getProps() {
def obj = Template.get(params.id)
render("""<div id="prop1">prop1 = ${obj.prop1}</div>
<div id="prop2">prop2 = ${obj.prop2}</div>
""")
}
关于javascript - grails g :select item as optionValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13873163/