You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.
| Online: | 25 Members of 4546 |
| Forum: Seam Users |
06. Mar 2008, 11:52 CET | Link |
Hi All, I want to iterate two arraylist in one dataTable or forEach or ui:repeat etc. Is it possible with seam?
Something like this(Just an example). Ex:-
for (int i = 0; i < this.arr1.size(); i++){
Test test = arr1.get(i);
Test2 test2 = arr2.get(i);
}
Ant help would be appreciated. Thanks in advance.
-Vikram
Seems like you're inviting an indexOutOfBoundsException. If you're keen to do it you could always use the rowKeyVar from rich:dataTable; roughly something like this:
<rich:dataTable value="#{bean.arr1}" var="one" rowKeyVar="row"> <h:outputText value="#{one.property1}"/> <h:outputText value="#{bean.arr2[row -1]}"/>Why don't you do some pre-processing in a Bean and merge the two lists into one logical list?
Cheers,
Damian.
<rich:dataTable value="#{bean.arr1}" var="one" rowKeyVar="row"> <h:outputText value="#{one.property1}"/> <h:outputText value="#{bean.arr2[row -1]}"/>I tried this but it didnt work. Can anyone help me out on this?
I am badly stuck.
Thanks in advance.
-Vikram
Why don't you do it in the following way:
Merge the 2 list in the bean together like this:
public List getList(){ List newList = new ArrayList(); newList.addAll(arr1); newList.addAll(arr2); }and in the view (xhtml page):
<rich:datatable value=#{bean.list} var="item"> <h:column> #{item.value} </h:column> </rich:datatable>greetz
both my arraylist is of same size but contains diffrent entity objects.
arr1 contains objects of entity A
arr2 contains objects of entity B
is it still possible to merge it into 1?
how will the resulting arraylist look like if i merge them into 1?
Thanks for your reply
-Vikram
One thing more
i want to iterate both simultaneously.
Which is not possible if I merge them into 1 i suppose.
-Vikram
store an index field in one of the list in addition to the other information (extend the entity with another class adding a field for every entity object). inject both list so that their components can be reference directly from jsf. then in the datatable, iterate through the extended component list and reference any components in the second list by simply passing it the index.
that should get your simultaneous iterating working. i think i have done this a couple places in the past.
'Why don't you try using logical logic this time. That's a plan that should work.'
one more thing..
you better test the data set logically to make sure you have the correct number of objects in each before you go to iterating... or very bad things could happen,,, as damian said..
'Why don't you try using logical logic this time. That's a plan that should work.'
i just had a better idea still...
create an int array the size of the two list if they are the same...
reference that array as the var. then reference the components of each list directly. no extending the entity..
i dont think i have done this,, but it should work.
'Why don't you try using logical logic this time. That's a plan that should work.'
How do i do this?
I don't know the exact syntax for this.
Thanks for your replies.
-Vikram
so you have your two result sets...
move them the two you want to reference directly to ArrayList
in the jsf jsp whatever your poisen
<h:datatable value="#{yourSessionBean.resultlength}" var="r"> ... <h:outputText value="#{yourSessionBean.list1.get(r.index).yourFieldFromEntityToDisplay}"/>same for list2 in the same iteration. HTH
'Why don't you try using logical logic this time. That's a plan that should work.'
Here's another suggestion for the pile
I'd just create a simple Wrapper objects... probably.
public List<WrapperObject> getTheList(){ if(theList == null){ //check list lengths //iterate over both list creating "theList" //with WrapperObjects } return theList; } //could be an inner class public class WrapperObject private A entityA; private B entityB; public WrapperObject(A a, B b){ entityA =a; entityB =b; } //gettters