在primefaces DataTable中显示Hashmap键和值 [英] Displaying Hashmap keys and values in a primefaces DataTable

查看:217
本文介绍了在primefaces DataTable中显示Hashmap键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在DataTable中显示一个Hashmap,下面是我想要做的事情:我将有一些产品的选择菜单,以及一个用于数量的输入文本,一个ajaxified添加按钮,它添加该产品及其数量,以及一个提交按钮,该按钮显示包含具有两列的DataTable的摘要对话框:Product Name和Quantitiy。
我的Hashmap是

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

为ajaxified按钮和所有这些第一步,他们为我工作,我有一切设置并且地图填写正确,所有剩下的就是显示数据。



预先致谢。

解决方案

  public class Product {
private int id;
私人字符串productName;
private int quantitiy;

//在此添加getters setters
}

//将产品id添加到map key
Map&Integer,Product> myMap = new HashMap< Integer,Product>();

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


公共清单<产品> getProducts(){
return new ArrayList< Product>(myMap.values()_;
}

将数据表值添加到getProducts()列表中

否则,产品作为映射键然后,

  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);
}

像这样编写primeface页面,

 < p:dataTable value =#{productBean.products}var =productEntry> 
< p: >
< h:outputText value =#{productEntry.key.productName}/>
< / p:列>
< p:列>
< h:outputText value =#{productEntry.value}/>
< / p:col UMN>
< / p:dataTable>


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.

解决方案

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>

这篇关于在primefaces DataTable中显示Hashmap键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆