Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterate Array List

 
Oliver Moore
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently passing an array list of beans created with the following code to a jsp:
while(rs.next()) {
Item_Desc item_desc = new Item_Desc();
item_desc.setItem_id(rs.getString(1));
item_desc.setItem(rs.getString(2));
item_desc.setsubCat(rs.getString(3));
item_desc.setManufacturer(rs.getString(4));
item_desc.setDescription(rs.getString(5));
list.add(item_desc);
} return list;
Iterating it using:
<logic:iterate name="item_descList"
id="item_desc"
scope="request"
type="com.ops.entity.Item_Desc">
gives me a table with columns for each property of the bean "item_desc", however many of the items in the list have the same subcat.
Could someone sugest a way to write the subcat once, then rows consisting of the other bean properties until the subcat changes, at which point the new subcat is changed again. i.e.
<subcat1>
<bean1 properties (has subcat1)>
<bean2 properties (has subcat1)>
<subcat2>
<bean1 properties (has subcat2)>
<bean2 properties (has subcat2)>
The list is already ordered by subcat from the sql querey which populates the list.
Any help is much aprieciated.
 
Geeta Ramasami
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If i have understood the problem correctly,for displaying each replicant SubCat only once u can build a logic which checks the previous SubCat value with the current Subcat value by setting the previous SubCat value to the Current value at the end of each iteration of the loop.If the previous value and the current value are different then display the SubCat value.Is this the one u r looking for or something else.
Regards
Geeta
[ March 10, 2003: Message edited by: Geeta Ramasami ]
[ March 10, 2003: Message edited by: Geeta Ramasami ]
 
Oliver Moore
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Geeta, that idea worked fine...

I could cut down some of the code if I could do a "notequals" for the strings, but I can't seem to find a way of doing that...
 
Geeta Ramasami
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Oliver,
You can check for notEquals and optimise the code as follows:
<% String prevsubCat = "checking"; %>
<logic:iterate name="item_descList" id="item_desc" scope="request" type="com.ops.entity.Item_Desc">
<% String currentsubCat = item_desc.getsubCat();
if (prevCat != null && !prevsubCat.equals(currentsubCat))
{ %>
<td colspan="4"> <b><bean:write name="item_desc" property="subCat"/></b> </td>
<%
}
%>
<td> <bean:write name="item_desc" property="item_id"/> </td>
<td> <bean:write name="item_desc" property="item"/> </td>
<td> <bean:write name="item_desc" property="Description"/> </td>
<td> <bean:write name="item_desc" property="manufacturer"/> </td>
<% prevsubCat = item_desc.getsubCat(); %>
</logic:iterate>
</table>
Hope this is useful
Cheers
Geeta
 
Oliver Moore
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant, that's much better, thanks...
Ols
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic