Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show HashMap in JSF Datatable. UIColumn with embedded Datatable

Status
Not open for further replies.
Sep 5, 2006
37
US
Hi,

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"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="[URL unfurl="true"]http://java.sun.com/jsf/html"[/URL] prefix="h"%>
<%@ taglib uri="[URL unfurl="true"]http://java.sun.com/jsf/core"[/URL] 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">
              <h:outputText value="Keys"/>
            </f:facet>
            <h:outputText value="#{var1.key}"/>
          </h:column>
          
          <h:column>
              <f:facet name="header">
                  <h:outputText value="Values"/>
                </f:facet>
              <h:dataTable value="#{backing_index2.entrySet}" var="var2"
                         binding="#{backing_index2.dataTable2}" id="dataTable2">              
                  <h:column id="col2">
                    <h:outputText 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.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top