Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tag to loop a List

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking for a tag to loop a List, which can take two items in each loop, coding will like this



Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use JSTL's forEach tag. But don't use var="item". Use the varStatus attribute instead, to access the list directly via the current loop index. I let you figure out how to write it.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:You can use JSTL's forEach tag. But don't use var="item". Use the varStatus attribute instead, to access the list directly via the current loop index. I let you figure out how to write it.


is it like this ?



Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why "2*" ? status.index will return you the proper index.

Also, you'll have to be careful that the list contains pairs. If the array ends with a single element, put your helmet on, this is going to blow.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Why "2*" ? status.index will return you the proper index.

I need to display a List into two columns in a table, each loop, I need to take two items and put into a <tr>.

Christophe Verré wrote:put your helmet on.

yes, I need this protection.

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64826
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why do you not have 2 arrays, or an array whose elements are an array with 2 elements?
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Then why do you not have 2 arrays, or an array whose elements are an array with 2 elements?


don't understand...



I am trying to get this output with forEach, what you will do , has another better way ?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64826
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than skipping every other item, I'd make the structure represent the data better. A 2D array, if you will.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic