You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Mike Kienenberger <mk...@alaska.net> on 2003/10/01 17:59:57 UTC

"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',$anAccount)}">
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',$velocityCount)}">
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


Re: "safe" way to get index of current item in a foreach loop?

Posted by Mike Kienenberger <mk...@alaska.net>.
Robert Nocera <rn...@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


RE: "safe" way to get index of current item in a foreach loop?

Posted by Robert Nocera <rn...@neosllc.com>.
Mike,

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.

Robert Nocera
[NEOS]
www.neosllc.com

  


-----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