Displaying Hashmap keys and values in a primefaces DataTable

50,825

You create Class like this:

public class Product{
    private int id;
    private String productName;
    private int quantitiy;

    // add getters setters here
}

// add product id to map key
Map<Integer,Product> myMap = new HashMap<Integer,Product>();

public Map<Integer,Product> getProductMap() {
   return myMap;
}


public List<Product> getProducts() {
   return new ArrayList<Product>(myMap.values()_;
}

Add datatable value to getProducts() List

Otherwise, product as a map key then,

Map<Product,Integer> myMap = new HashMap<Product,Integer>();

public List<Map.Entry<Product, Integer>> getProducts() {
    Set<Map.Entry<Product, Integer>> productSet = 
                     myMap.entrySet();
    return new ArrayList<Map.Entry<Product, Integer>>(productSet);
}

write primeface page like this way,

<p:dataTable value="#{productBean.products}" var="productEntry">
   <p:column>
      <h:outputText value="#{productEntry.key.productName}" />
   </p:column>
   <p:column>
       <h:outputText value="#{productEntry.value}" />
   </p:column>
</p:dataTable>
Share:
50,825

Related videos on Youtube

snajahi
Author by

snajahi

Updated on October 08, 2020

Comments

  • snajahi
    snajahi over 3 years

    I'm trying to display a Hashmap in a DataTable, here's what i'm trying to do : I'll have a select menu of some products, and an input text for quantity, an "ajaxified" add button that adds the product and its quantity to the map, and a submit button that displays a summary dialog containing a DataTable with two columns : Product Name and Quantitiy. my Hashmap is

    Map<Product,Integer> myMap = new HashMap<Product,Integer>();
    

    for the ajaxified button and all those first steps, they're working for me, i have everything set and the map filled correctly all what's left is showing the data.

    Thanks in advance.

    • Kushan
      Kushan over 12 years
      map key is Product or integer?
    • snajahi
      snajahi over 12 years
      it's in fact Product, the Integer is for the Quantity
  • snajahi
    snajahi over 12 years
    Thank You!! the second one is more convenient for me and it's working like a charm, thanks again :)
  • Miloš Rašić
    Miloš Rašić about 12 years
    For the first one to work, you would have to do products = new ArrayList<Product>(myMap.values()) as HashMap.values() returns Collection<V>. Then you could also do away with the initial definition of products and make it a simple declaration.
  • jcomouth
    jcomouth over 7 years
    Your map is named "myMap", how can you define "#{productBean. products }" as your collection to iterate through?
  • Eric
    Eric over 7 years
    @JeromeC I came here looking for the same thing. Found this other thread that helped me work through what roadblock I ran into. stackoverflow.com/questions/25834329/…
  • Newbie
    Newbie almost 7 years
    after implemented this technique, the row selection (using checkbox one) of datatable not working anymore, only I faced this issue?
  • Nestoter
    Nestoter over 2 years
    This does work but IDE is complaining about productEntry.key and productEntry.value cannot be resolved. Does anybody know how to fix this?