cybertextron
cybertextron

Reputation: 10961

grouping array elements in a gsp page grails groovy

I have the following code snippet:

<div id="bookmarks" style="height:150px;width:250px; padding-left: 100px;margin-top: -54px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">
    <g:each in="${params['bookmarks'] }" var="bookmark">
        <p><a onclick="seek('${bookmark}');">${bookmark}</a></p>
    </g:each>
</div>

However, when I look at the viewer.gsp, I see:

0
0
:
0
4
:
3
5
,
0
0
:
1
6
:
0
0
,
0
0
:
2
5
:
0
1

Instead of the usual 00:04:35 as supposed to be ... How can I format the array $params['bookmars'] to return me the correct format?

Upvotes: 0

Views: 1006

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

params['bookmarks'] will give you a list for a multi-valued parameter but a String if there's only a single value, and null if there's no values at all, and each on a String iterates over the characters in the string. So it looks like your bookmarks parameter is a single-valued parameter whose value is a comma-separated string 00:04:35,00:16:00,..., not a multi-valued parameter (i.e. a form submission like bookmarks=00:04:35&bookmarks=00:16:00&...).

For parameters that may be multi-valued you can use params.list('...'), which guarantees you a list (with zero, one or more than one item as appropriate).

<g:each in="${params.list('bookmarks') }" var="bookmark">

But if you're stuck with the comma-separated single value then you'll have to split it yourself:

<g:each in="${params.bookmarks?.split(/,/)}" var="bookmark">

In addition, you probably need to use the relevant encodeAs... calls to ensure you get valid JavaScript and HTML:

<a onclick="seek('${bookmark.encodeAsJavaScript()}');">${bookmark.encodeAsHTML()}</a>

Upvotes: 3

Related Questions