如何迭代一个HashMap,可以使用原子数组 [英] How to iterate a HashMap with primefaces selectable datatatable
问题描述
< ui:repeat
我的bean :
@ManagedBean
@ViewScoped
public class Chat {
private static Map< String,List< ChatObject>> chat = new LinkedHashMap< String,List< ChatObject>>();
private ChatObject selectedChatObject;
public void onChatRowSelection(){
if(getSelectedChatObject()!= null){
System.out.println(test);
}
}
public List< Map.Entry< String,List< ChatObject>>> getChatList(){
Set< Map.Entry< String,List< ChatObject>>> productSet = chat.entrySet();
return new ArrayList< Map.Entry< String,List< ChatObject>>>(productSet);
}
@PostConstruct
public void postConstructMethod(){
if(chat.isEmpty()){
列表< ChatObject> objectsList1 = new ArrayList< ChatObject>();
objectsList1.add(new ChatObject(3369818,1,1));
objectsList1.add(new ChatObject(3369819,2,2));
objectsList1.add(new ChatObject(3369820,3,3));
chat.put(聊天主题1,objectsList1);
列表< ChatObject> objectsList2 = new ArrayList< ChatObject>();
objectsList2.add(new ChatObject(3369813,4,4));
objectsList2.add(new ChatObject(3369815,5,5));
chat.put(聊天主题2,objectsList2);
}
}
public ChatObject getSelectedChatObject(){
return selectedChatObject;
}
public void setSelectedChatObject(ChatObject selectedChatObject){
this.selectedChatObject = selectedChatObject;
}
}
我的JSF:
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN
http://www.w3。 org / TR / xhtml1 / DTD / xhtml1-transitional.dtd>
< html xmlns =http://www.w3.org/1999/xhtml
xmlns:ui =http://xmlns.jcp.org/jsf/facelets
xmlns:h =http://xmlns.jcp.org/jsf/html
xmlns:f =http://xmlns.jcp.org/jsf/core
xmlns:p =http://primefaces.org/ui
xmlns:fn =http://java.sun.com/jsp/jstl/functions>
< h:form id =formenctype =multipart / form-dataacceptcharset =ISO-8859-1>
< ui:repeat value =#{chat.chatList}var =chatEntry>
< h2>< h:outputText value =#{chatEntry.key}/>< / h2>
< br />
< p:dataTable
value =#{chatEntry.value}
var =chatEntryVar
widgetVar =chatTableWV
styleClass =geralBorderless
style =cursor:pointer
rowKey =#{chatEntryVar.id}
rendered =true
selectionMode =single
selection =#{chat.selectedChatObject}
paginatorAlwaysVisible =false>
< p:ajax事件=rowSelect
listener =#{chat.onChatRowSelection}
oncomplete =chatTableWV.unselectAllRows();>
< / p:ajax>
< p:column>
< h:outputText value =#{chatEntryVar.name}/>
< / p:column>
< / p:dataTable>
< / ui:repeat>
< / h:form>
< / html>
我的地图中的所有5 ChatObject
成功显示在我的页面。但是,当我点击与我添加到的第二个列表相关的行时, c objectList2
。 当我点击第一个列表的行添加 objectList1
,当系统进入 onChatRowSelection
方法, selectedChatObject
将为null。如何解决这个问题?
谢谢!
在这里:
< ui:repeat ...>
< p:dataTable ... widgetVar =chatTableWV>
< p:ajax ... oncomplete =chatTableWV.unselectAllRows();>
多个数据表被分配完全相同的 widgetVar
在JavaScript范围内的名称。生成以下JavaScript代码:
window ['chatTableWV'] = new Widget(tableElement1);
window ['chatTableWV'] = new Widget(tableElement2);
window ['chatTableWV'] = new Widget(tableElement3);
// ...
基本上,每次迭代都会覆盖分配给声明的最后一个对象 widgetVar
名称,直到它最后引用最后一个。最后一个的所有小部件基本都不可用,导致它们不再具有行选择的功能。
通过给它们分配唯一的 widgetVar
。您可以使用< ui:repeat>
的迭代索引。
< ui:repeat ... varStatus =loop>
< p:dataTable ... widgetVar =chatTableWV _#{loop.index}>
< p:ajax ... oncomplete =chatTableWV _#{loop.index} .unselectAllRows();>
生成以下JavaScript代码:
window ['chatTableWV_0'] = new Widget(tableElement1);
window ['chatTableWV_1'] = new Widget(tableElement2);
window ['chatTableWV_2'] = new Widget(tableElement3);
// ...
最后,PrimeFaces小部件管理器可以找到它们。 p>
I have tried different solutions but none is working in my case. I want all the rows in this datatable to be selectable. The problem seems to be the <ui:repeat
that is probably overriding the objects...
My bean:
@ManagedBean
@ViewScoped
public class Chat {
private static Map<String, List<ChatObject>> chat = new LinkedHashMap<String, List<ChatObject>>();
private ChatObject selectedChatObject;
public void onChatRowSelection(){
if(getSelectedChatObject() != null){
System.out.println("test");
}
}
public List<Map.Entry<String, List<ChatObject>>> getChatList() {
Set<Map.Entry<String, List<ChatObject>>> productSet = chat.entrySet();
return new ArrayList<Map.Entry<String, List<ChatObject>>>(productSet);
}
@PostConstruct
public void postConstructMethod() {
if(chat.isEmpty()){
List<ChatObject> objectsList1 = new ArrayList<ChatObject>();
objectsList1.add(new ChatObject("3369818", "1", "1"));
objectsList1.add(new ChatObject("3369819", "2", "2"));
objectsList1.add(new ChatObject("3369820", "3", "3"));
chat.put("Chat Topic 1", objectsList1);
List<ChatObject> objectsList2 = new ArrayList<ChatObject>();
objectsList2.add(new ChatObject("3369813", "4", "4"));
objectsList2.add(new ChatObject("3369815", "5", "5"));
chat.put("Chat Topic 2", objectsList2);
}
}
public ChatObject getSelectedChatObject() {
return selectedChatObject;
}
public void setSelectedChatObject(ChatObject selectedChatObject) {
this.selectedChatObject = selectedChatObject;
}
}
My JSF:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:form id="form" enctype="multipart/form-data" acceptcharset="ISO-8859-1">
<ui:repeat value="#{chat.chatList}" var="chatEntry">
<h2><h:outputText value="#{chatEntry.key}" /></h2>
<br />
<p:dataTable
value="#{chatEntry.value}"
var="chatEntryVar"
widgetVar="chatTableWV"
styleClass="geralBorderless"
style="cursor:pointer"
rowKey="#{chatEntryVar.id}"
rendered="true"
selectionMode="single"
selection="#{chat.selectedChatObject}"
paginatorAlwaysVisible="false">
<p:ajax event="rowSelect"
listener="#{chat.onChatRowSelection}"
oncomplete="chatTableWV.unselectAllRows();">
</p:ajax>
<p:column>
<h:outputText value="#{chatEntryVar.name}" />
</p:column>
</p:dataTable>
</ui:repeat>
</h:form>
</html>
All the 5 ChatObject
in my Map are successfully shown in my page. But onChatRowSelection
method will only print "test" when I click in the rows related to the second list I added to my map: objectList2
. When I click in the lines from the first list I added objectList1
, when the system enters in the onChatRowSelection
method, selectedChatObject
will be null. How can I fix this?
Thanks!
Your problem is here:
<ui:repeat ...>
<p:dataTable ... widgetVar="chatTableWV">
<p:ajax ... oncomplete="chatTableWV.unselectAllRows();">
Multiple data tables are been assigned exactly the same widgetVar
name in JavaScript scope. In effects, the following JavaScript code is generated:
window['chatTableWV'] = new Widget(tableElement1);
window['chatTableWV'] = new Widget(tableElement2);
window['chatTableWV'] = new Widget(tableElement3);
// ...
Basically, every iteration overrides the last object assigned to the declared widgetVar
name until it ends up referencing the last one. All widgets expect of the last one are basically unavailable, causing them to not be functional anymore as to row selection.
Fix it accordingly by giving them each an unique widgetVar
. You could use the iteration index of <ui:repeat>
for this.
<ui:repeat ... varStatus="loop">
<p:dataTable ... widgetVar="chatTableWV_#{loop.index}">
<p:ajax ... oncomplete="chatTableWV_#{loop.index}.unselectAllRows();">
This way the following JavaScript code is generated:
window['chatTableWV_0'] = new Widget(tableElement1);
window['chatTableWV_1'] = new Widget(tableElement2);
window['chatTableWV_2'] = new Widget(tableElement3);
// ...
And finally PrimeFaces widget manager can find them all.
这篇关于如何迭代一个HashMap,可以使用原子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!