velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Kienenberger <mkien...@alaska.net>
Subject Re: "safe" way to get index of current item in a foreach loop?
Date Mon, 06 Oct 2003 21:33:18 GMT
Robert Nocera <rnocera@neosllc.com> wrote:
> I have used $velocityCount for that type of thing without any problems.
> Maybe I just assumed it would match the index in my collection, but it
> always has.

Thanks, Robert.

I'm surprised no one else has commented on this.
I've also been using $velocityCount so far without problem as well, but I 
think maybe I should create a velocity ViewTool subclass that guarantees the 
index of objects.

Maybe return a List of Map.Entry-like instances with getIndex() and 
getObject() methods.  It could be use like the following:  

#foreach $anEntry in $indexTool.forArray($list)
	
<FORM method="POST" 
action="$link.setAction($action).addQueryData('parameter',${anEntry.getIndex()})">

	display ${anEntry.getObject()}
	<INPUT type="submit" name="${anEntry.getObject().getName()}Button" 
value="${anEntry.getObject().getValueName()}" class="submit" />
</FORM>

#end

-Mike

> -----Original Message-----
> From: Mike Kienenberger [mailto:mkienenb@alaska.net] 
> Sent: Wednesday, October 01, 2003 12:00 PM
> To: velocity-user@jakarta.apache.org
> Subject: "safe" way to get index of current item in a foreach loop?
> 
> I have velocity code that looks similar to this.
> 
> #foreach ($anAccount in $accountsList)
> 
> 	<a 
> href="${link.setAction('ViewAccountBill').addQueryData('account',$anAcco
> unt)}">
> History</A>
> 
> #end ## foreach anAccount
> 
> However, I don't want to attach a raw java object to the account
> parameter.  
> Instead I want to attach the current index of the item in $accountsList,
> 
> which is an ArrayList object. 
> 
> [$accountsList is persistent across the request/response loop, so please
> 
> don't get sidetracked by that.]
> 
> I know I can do something like the following with $velocityCount, but I 
> don't know if $velocityCount is guaranteed to match the indexes of my 
> ArrayList (and lacking documentation to that effect, I assume it is
> not.)
> 
> #foreach ($anAccount in $accountsList)
> 
> 	<a 
> href="${link.setAction('ViewAccountBill').addQueryData('account',$veloci
> tyCount)}">
> History</A>
> 
> #end ## foreach anAccount
> 
> [I also know that I would have to set counter.initial.value = 0 in the 
> velocity.properties file or otherwise account for the starting value of 
> $velocityCount.]
> 
> Is this a safe way to go?   Is this the best way to go?  How do people 
> generally identify a specific java object inside a #foreach,
> particularly as 
> html href or form action?
> 
> Thanks,
> 
> -Mike
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message