Reputation: 835
I have a list which i am getting from database.I have 6 elements in the list
List<String> list=new ArrayList<String>();
list.add("No Connection");
list.add("sep 24 ,2009");
list.add("no issues are there");
list.add("dec 30,2012");
list.add("no meter");
list.add("april 12,2013");
map.put("list",list);
I am using Velocity template. In that i am using foreach loop to get the list items.I have to display in the table with two cells in the first cell three elements in the even indexes should display and in second cell odd indexes should display.How can do that.
#foreach($mylist in $list)
<td>even indexed list elements in my list</td>
<td align="center">odd indexed elements in the list.</td>
#end
Update:
I tried like this but it doesn't work.Is this correct way of getting indexes in velocity.
#set ($counter = 0)
#foreach ($i in $descList)
#set ($counter = $counter + 1)
#if ( $couter % 2 == 0)
<td>$descList[$i]</td>
<td align="center"></td>
#else
<td></td>
<td align="center">$descList[$i+1]</td>
#end
#end
I am getting following error.
07:53:31,952 ERROR VelocityEngine:43 - Left side ($couter) of modulus operation has null value. Operation not possible. /emailtemplates/diis_nem_issues_email.vm [line 47, column 20]
07:53:31,953 ERROR VelocityEngine:43 - Left side ( 2 ) of '==' operation has null value. If a reference, it may not be in the context. Operation not possible. /emailtemplates/diis_nem_issues_email.vm [line 47, column 25]
Upvotes: 1
Views: 34883
Reputation: 617
I'm using this snippet that works for me:
#set( $count = 1 )
<p>User details:</p>
<table>
#foreach( $user in $users)
<tr>
<td>$count</td>
<td>$user.username</td>
<td>$user.age</td>
</tr>
#set( $count = $count + 1 )
#end
</table>
where '$users' is a List that I'm setting in java, like this:
List<User> users = ...
params.put("users", users);
I found these solution here: http://thinkinginsoftware.blogspot.com.ar/2010/03/velocity-templates-for-email.html
Upvotes: 4
Reputation: 1
There is an small mistake in the code, as follows:
if ( $**couter** % 2 == 0)
**couter** ->>>> **counter**
Upvotes: 0
Reputation: 11601
The problem is that you have a typo: couter
instead of counter
.
Upvotes: 2
Reputation: 835
I resolved this issue by using hashtable in my application instead of List in my application
#foreach( $key in $hashtab.keySet() )
<tr><td>$key</td><td align="center">$hashtab.get($key)</td></tr>
#end
and in my java class i have like this
HashTable<String,Date> hashtab=new HashTable<String,Date>();
hashtab.put(key,value);
Thanks guys for your Ideas.
Upvotes: 4
Reputation: 943
As far as I remember you could do something like this:
#set ($counter = 0)
#foreach ($mylist in $list)
#set ($counter = $counter + 1)
#if ( $couter % 2 == 0)
<td>even indexed result</td>
<td align="center"></td>
#else
<td></td>
<td align="center">odd index result</td>
#end
#end
Upvotes: 1