Displaying Hashmap keys and values in a primefaces DataTable(在 primefaces DataTable 中显示 Hashmap 键和值)
问题描述
我正在尝试在 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 键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 primefaces DataTable 中显示 Hashmap 键和值


基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01