问题描述
我正在尝试在 DataTable 中显示 Hashmap,这就是我正在尝试做的事情:我将有一些产品的选择菜单,以及数量的输入文本,一个ajaxified"添加按钮添加产品及其数量到地图,以及一个提交按钮,该按钮显示一个包含数据表的摘要对话框,其中包含两列:产品名称和数量.我的哈希图是
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>();
对于 ajaxified 按钮和所有这些第一步,它们正在为我工作,我已经设置好所有东西并且正确填充了地图,剩下的所有内容都在显示数据.
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.
提前致谢.
推荐答案
你这样创建类:
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()_;
}
将数据表值添加到 getProducts() 列表
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);
}
这样写primeface页面,
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 键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!