betweenGo

ATG ForEach Output with Separators Between Items

by on Jul.24, 2009, under Page Development

Pez Collection

Sometimes you want to list each item in the collection with a separator in between like a comma or slash.  However the trick is to have the separator only in between items.

In ATG DSP you can use the ForEach droplet to iterate through each item in a collection.  However to put a separator between each item is not straight-forward with this droplet.  To do this I used JSTL to test if we are at the end of the collection.  If we are then I don’t add the separator, otherwise I do.

Here is an example.

<dspel:droplet name="/atg/dynamo/droplet/ForEach">
 <dspel:param name="array" param="dvds"/>
 <dspel:setvalue param="dvd" paramvalue="element" />
 <dspel:oparam name="outputStart">
  <dspel:getvalueof var="size" param="size" />
 </dspel:oparam>
 <dspel:oparam name="output">
  <dspel:valueof param="dvd.title" valueishtml="true"/>
  <dspel:getvalueof var="count" param="count" />
  <c:if test="${size != count}"> , </c:if>
 </dspel:oparam>
</dspel:droplet>

And here is the output for this example.

The Incredibles, Ratatouille, Cars

Do you have a better or more elegant solution?  Please feel free to let me know in the comments?

Share

Related posts:

  1. How to Debug No Output for ATG ForEach
  2. Accessing RepositoryItems with JSTL
  3. Comparison of DSP and DSPEL
  4. Jumping to different parts of a page using a dropdown
  5. Size of collection in a JSP/DSP page

:, ,

2 Comments for this entry

  • Piran

    I can remove 3 lines:

    <dspel:droplet name="/atg/dynamo/droplet/ForEach">
    <dspel:param name="array" param="dvds"/>
    <dspel:param name="elementName" value="dvd" />
    <dspel:oparam name="output">
    <dspel:getvalueof var="count" param="count" />
    <c:if test="${1 != count}"> , </c:if>
    <dspel:valueof param="dvd.title" valueishtml="true"/>
    </dspel:oparam>
    </dspel:droplet>

    I just do the test before the text as opposed to after, so you don’t need to know how long the list is.

    I also use the elementName param. I’ve not see then setvalue used in the way you’ve done before.

    And finally, I’ve often thought of subclassing ForEach to allow an inbetween oparam. That’s left as an exercise for the reader.

  • Frank Kim

    Piran, that’s a great solution! I especially like the idea of subclassing ForEach to allow an inbetween oparam.

    Thanks!

Leave a Reply

 

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!