admin管理员组文章数量:1345387
I'm making a web application with Grails. I've got a list with data that must be included on JavaScript to perform some dynamic load on <select>
drop-list. Basically, I'm getting a two level list from the server, then the first level is presented on a drop box. When the user selects an option, the list associated to this option is presented on another drop box.
The (simplified) code on the gsp page for the JavaScript function is the following
function selecTipe() {
var types = ${typeList}
alert('List of types ' + types )
The problem is that, if typeList is defined (in Groovy) as
typeList = [['TYPE1', ['VAR1','VAR2','VAR3']],
['TYPE2', ['VAR1','VAR2','VAR3']]
['TYPE3', ['VAR1','VAR2','VAR3']] ]
when the page is renderized, the JavaScript code appears like
function selecTipe() {
var types = [[ TYPE1, [ VAR1, VAR2, VAR3 ]],
[ TYPE2, [ VAR1, VAR2, VAR3 ]]
[ TYPE3, [ VAR1, VAR2, VAR3 ]] ]
alert('List of types ' + types )
which is erroneous, as JavaScript treats then not as strings, but as references due the lack of quotes.
Is there any way to force Groovy to print a list of arrays with quotes or any other easy way to achieve this?
PD: I can make an specific function to achieve it, but I think it should be an easy way to do that...
EDIT: I've added the plete data structure, as is a little more plex than a simple list
I'm making a web application with Grails. I've got a list with data that must be included on JavaScript to perform some dynamic load on <select>
drop-list. Basically, I'm getting a two level list from the server, then the first level is presented on a drop box. When the user selects an option, the list associated to this option is presented on another drop box.
The (simplified) code on the gsp page for the JavaScript function is the following
function selecTipe() {
var types = ${typeList}
alert('List of types ' + types )
The problem is that, if typeList is defined (in Groovy) as
typeList = [['TYPE1', ['VAR1','VAR2','VAR3']],
['TYPE2', ['VAR1','VAR2','VAR3']]
['TYPE3', ['VAR1','VAR2','VAR3']] ]
when the page is renderized, the JavaScript code appears like
function selecTipe() {
var types = [[ TYPE1, [ VAR1, VAR2, VAR3 ]],
[ TYPE2, [ VAR1, VAR2, VAR3 ]]
[ TYPE3, [ VAR1, VAR2, VAR3 ]] ]
alert('List of types ' + types )
which is erroneous, as JavaScript treats then not as strings, but as references due the lack of quotes.
Is there any way to force Groovy to print a list of arrays with quotes or any other easy way to achieve this?
PD: I can make an specific function to achieve it, but I think it should be an easy way to do that...
EDIT: I've added the plete data structure, as is a little more plex than a simple list
Share Improve this question edited Aug 31, 2012 at 13:58 Zhaph - Ben Duguid 27k5 gold badges85 silver badges122 bronze badges asked Jan 14, 2010 at 9:33 KhelbenKhelben 6,4716 gold badges36 silver badges46 bronze badges2 Answers
Reset to default 9Try this in your grails controller's action :
def types = ['TYPE1', 'TYPE2', 'TYPE3'] as grails.converters.JSON
[typeList : types]
Try this in your grails controller's action :
def types = ['TYPE1', 'TYPE2', 'TYPE3'] as grails.converters.JSON [typeList : types]
If someone has a object, this will work in your gsp:
<g:javascript>
var types = JSON.parse('${typeList}');
alert("Type id:" + types[1].id);
</g:javascript>
本文标签: grailsCorrectly pass a Groovy list to Javascript code in GSPStack Overflow
版权声明:本文标题:grails - Correctly pass a Groovy list to Javascript code in GSP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743756486a2533601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论