• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Grails Random

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" />
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you type like this
${property.images[0]}

i think it should work ...
[ October 04, 2008: Message edited by: adwin wijaya ]
 
Gareth Millward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget the .imageURL part!
 
reply
    Bookmark Topic Watch Topic
  • New Topic