You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Nathan Rogers <nr...@library.wisc.edu> on 2003/09/09 22:11:29 UTC

Iterating over a nested collection

I am trying to build a JavaScript array using a Map of Collections of 
Beans.  Is there an easy way to get the list of keys and iterate over 
the values for each of those? (In other words, first get the list of 
keys.  For each key, get the Collection associated with it.  Iterate 
over this Collection to populate the page).

The Map contains Collection keyed to a specific character, a-z.  Each 
Collection contains a list of Beans containing the actual information 
that needs to put into the array.

Here's the Velocity snippet that I am using currently that outputs nothing

#foreach ($departmentLetter in $departments)
  department["$!departmentLetter"] = [
  #set ($departmentCounter = 0);
  #foreach ($currentDepartment in $departments.get($departmentLetter))
    ["$currentDepartment.name", "$currentDepartment.number"],
    $departmentCounter++;
  #end
    #if ($departmentCounter == 0)
      ["There are no departments for the letter $departmentLetter", "0"]
    #end
  ];
#end

Any suggestions would be helpful
-- 
Nathan Rogers
Library Technology Group
312F Memorial Library
728 State Street
261-1409



RE: Iterating over a nested collection

Posted by Mike Curwen <gb...@gb-im.com>.
Velocity doesn't wrap iterating Maps in the syntactic sugar of #foreach

so you need to use some java.


<select name="foo">
#foreach( $key in $my_map.keySet() )
  <option value="$key" >$!my_map.get($key)</option>
#end
</select>

that's generic code I use to build dropdowns where my_map is a Map, the
key of the map is the submitted value, and the value in the map is the
displayed value.

Hopefully you can modify this for your own code.



> -----Original Message-----
> From: Nathan Rogers [mailto:nrogers@library.wisc.edu] 
> Sent: Tuesday, September 09, 2003 3:11 PM
> To: Velocity Users List
> Subject: Iterating over a nested collection
> 
> 
> I am trying to build a JavaScript array using a Map of Collections of 
> Beans.  Is there an easy way to get the list of keys and iterate over 
> the values for each of those? (In other words, first get the list of 
> keys.  For each key, get the Collection associated with it.  Iterate 
> over this Collection to populate the page).
> 
> The Map contains Collection keyed to a specific character, a-z.  Each 
> Collection contains a list of Beans containing the actual information 
> that needs to put into the array.
> 
> Here's the Velocity snippet that I am using currently that 
> outputs nothing
> 
> #foreach ($departmentLetter in $departments)
>   department["$!departmentLetter"] = [
>   #set ($departmentCounter = 0);
>   #foreach ($currentDepartment in $departments.get($departmentLetter))
>     ["$currentDepartment.name", "$currentDepartment.number"],
>     $departmentCounter++;
>   #end
>     #if ($departmentCounter == 0)
>       ["There are no departments for the letter 
> $departmentLetter", "0"]
>     #end
>   ];
> #end
> 
> Any suggestions would be helpful
> -- 
> Nathan Rogers
> Library Technology Group
> 312F Memorial Library
> 728 State Street
> 261-1409
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>