This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSF and the fly likes Using of nested Datatables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Using of nested Datatables" Watch "Using of nested Datatables" New topic
Author

Using of nested Datatables

Pratap Kale
Greenhorn

Joined: Sep 24, 2008
Posts: 3
Hi All,
I am trying to display a HashMap in a datatable where the values for each key is an ArrayList e.g.

code:


ArrayList al = new ArrayList();
al.add("AA");
al.add("BB");
al.add("CC");
HashMap hm = new HashMap();
hm.put("ONE", al);

ArrayList al2 = new ArrayList();
al2.add("AA2");
al2.add("BB2");
al2.add("CC2");
hm.put("TWO", al2);




Now I have a Datatable with two columns. First column shows the key and the second column I again have
a datatable to show the ArrayList. So I will have one row with a key and the values again shown in a
datatable. So far I no luck and need some help on how to achieve this. Here is the index2.jsp code I have:

code:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=windows-1252"/>
<title>index2</title>
</head>
<body><h:form binding="#{backing_index2.form1}" id="form1">

<h:dataTable value="#{backing_index2.entrySet}" var="var1"
binding="#{backing_index2.dataTable1}" id="dataTable1" border="1">
<h:column>
<f:facet name="header">
<hutputText value="Keys"/>
</f:facet>
<hutputText value="#{var1.key}"/>
</h:column>

<h:column>
<f:facet name="header">
<hutputText value="Values"/>
</f:facet>
<h:dataTable value="#{backing_index2.entrySet}" var="var2"
binding="#{backing_index2.dataTable2}" id="dataTable2">
<h:column id="col2">
<hutputText id="out3" value="#{var2.value}"/>
</h:column>
</h:dataTable>

</h:column>
</h:dataTable>

</h:form></body>
</html>
</f:view>




And here is the backing bean Index2.java I have:

code:



package project1.backing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlForm;


public class Index2 {
private HtmlForm form1;
private HtmlDataTable dataTable1;
private HtmlDataTable dataTable2;
private List entrySet;
private HashMap hm;

public Index2() {

ArrayList al = new ArrayList();
al.add("AA");
al.add("BB");
al.add("CC");
hm = new HashMap();
hm.put("ONE", al);

ArrayList al2 = new ArrayList();
al2.add("AA2");
al2.add("BB2");
al2.add("CC2");
hm.put("TWO", al2);
entrySet = new java.util.ArrayList(hm.entrySet());

//here it only prints two rows as i have two keys but on the output it
//does not come up fine
for (int i = 0; i < entrySet.size(); i++) {
System.out.println("==== " + entrySet.get(i));
}
}

public void setForm1(HtmlForm form1) {
this.form1 = form1;
}

public HtmlForm getForm1() {
return form1;
}

public void setDataTable1(HtmlDataTable dataTable1) {
this.dataTable1 = dataTable1;
}

public HtmlDataTable getDataTable1() {
return dataTable1;
}

public void setDataTable2(HtmlDataTable dataTable2) {
this.dataTable2 = dataTable2;
}

public HtmlDataTable getDataTable2() {
return dataTable2;
}

public void setEntrySet(List entrySet) {
this.entrySet = entrySet;
}

public List getEntrySet() {
return entrySet;
}

public void setHm(HashMap hm) {
this.hm = hm;
}

public HashMap getHm() {
return hm;
}
}




Now when I run this application the keys are shown in the respective column but values are coming up as

[AA2, BB2, CC2] in one row rather in a separate Row as I am using a Datatable inside UIColumn to show the values.

Here is what I see in the output:

code:


keys Values
TWO [AA2, BB2, CC2]
[AA, BB, CC]
ONE [AA2, BB2, CC2]
[AA, BB, CC]




As above output is not correct as it shows both list values in front of each key rather than what those
keys are tied to. I wanted the following output:

code:

keys Values
ONE AA
BB
CC
TWO AA2
BB2
CC2



So I can do sorting pagination on the values datatable. But currently the output is not shown correctly. Any help
is really appreciated. My hashmap will be populated dynamically so wants to show the key and values in separate column
but also need to show the values in an embedded datatable as the list can be huge.

Otherwise ,if we have someother type example like this.please help me in this,needful to me.
Thanks in advance
Thanks
Rahul Santha
Greenhorn

Joined: Apr 17, 2008
Posts: 12
Did you find an answer for this?

I have a need to bind a textbox to an object living inside HashMap. Share your thoughts, if you have found the answer.


Thanks!<br />Rahul, IOWA
Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
This might work :



Here bean.getEntrySet method is written like this :



Let me know if this helps .
Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
This seems even better :


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using of nested Datatables
 
Similar Threads
[JSF 2.0] re-render a dataTable with Ajax ?
problem with datatable binding attribute
Eliminating Duplicate in Arraylist and moving the dups to another arraylist.
Show HashMap in Datatable. UIColumn with embedded Datatable. Help Needed
HashMap entries with duplicate keys...