Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
344 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...