| Author |
Grails Random
|
Gareth Millward
Greenhorn
Joined: Jun 28, 2008
Posts: 5
|
|
Hi, I have this code at that outputs the images for a property in my list. <g:each in="${property.images}" var="g"> <img src="/agp/images/${g?.imageURL}"alt="" width="150px" height="100px" /> </g:each> However I just want to display the first available object in the for each not all of them. I tried this way.. im not sure if this is the right way to go about it... <g:set var="randImg" value="${new Random(property.images.size())}" /> <img src="/agp/images/${g?.imageURL[randImg}"alt="" width="150px" height="100px" />
|
 |
adwin wijaya
Greenhorn
Joined: Oct 01, 2008
Posts: 4
|
|
can you type like this ${property.images[0]} i think it should work ...  [ October 04, 2008: Message edited by: adwin wijaya ]
|
 |
Gareth Millward
Greenhorn
Joined: Jun 28, 2008
Posts: 5
|
|
Doesn't seem to like that <g:each in="${property.images[0]}" var="g"> <img src="/agp/images/${g?.imageURL}"alt="" width="150px" height="100px" /> </g:each> Message: No signature of method: org.hibernate.collection.PersistentSet.getAt() is applicable for argument types: (java.lang.Integer) values: {0} Caused by: No signature of method: org.hibernate.collection.PersistentSet.getAt() is applicable for argument types: (java.lang.Integer) values: {0} Class: Unknown At Line: [-1] Code Snippet:
|
 |
Matthew Taylor
Rancher
Joined: Jun 13, 2004
Posts: 110
|
|
Originally posted by Gareth Millward: However I just want to display the first available object in the for each not all of them.
There are a couple of things you might need to know here. Firstly, that if if you declare a "[]" in Grails, it creates a Set by default. So if you are concerned about the order in which your images are in, you'll want to declare it like this: This is the reason you were getting the exception, because there is no way to access a Set element with the subscript operator [0], because the Groovy Set object has no getAt() method. Secondly, if you only want the first element of the List, you don't need to loop through with <g:each/>. If it is a List, you can just do this: If the collection is a Set, however, and you really don't care which image you use, you can cast it to a List and use the subscript operator. This is essentially a random image from the collection. Hope that helps you out. [ October 07, 2008: Message edited by: Matthew Taylor ]
|
Grails Consultant
http://dangertree.net
|
 |
Marc Peabody
pie sneak
Sheriff
Joined: Feb 05, 2003
Posts: 4727
|
|
Don't forget the .imageURL part!
|
A good workman is known by his tools.
|
 |
 |
|
|
subject: Grails Random
|
|
|