How to iterate a HashMap with default selectable faces

I have tried different solutions, but none works in my case. I want all rows in this datatable to be selectable. The problem seems to <ui:repeat

be which is probably overriding 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 5 ChatObject

on my map are shown successfully on my page. But the onChatRowSelection

method will only print a "test", when I press the strings associated with the list of the second , which I added to my card: objectList2

. When I click the lines from the first list, I added objectList1

when the system enters the method onChatRowSelection

, it selectedChatObject

will be blank. How can I fix this?

+3


source to share


1 answer


Your problem is here:

<ui:repeat ...>
    <p:dataTable ... widgetVar="chatTableWV">
       <p:ajax ... oncomplete="chatTableWV.unselectAllRows();">

      

Many data tables are assigned the exact same name widgetVar

in the JavaScript scope. The following JavaScript code is generated in the effects:

window['chatTableWV'] = new Widget(tableElement1);
window['chatTableWV'] = new Widget(tableElement2);
window['chatTableWV'] = new Widget(tableElement3);
// ...

      

Basically, each iteration overrides the last object assigned to the declared name widgetVar

until it finishes referencing the last one. All widgets expect the last one to be mostly unavailable, with the result that they will no longer function like row selection.



Fix it by making each one unique widgetVar

. The iteration index can be used for this <ui:repeat>

.

<ui:repeat ... varStatus="loop">
    <p:dataTable ... widgetVar="chatTableWV_#{loop.index}">
       <p:ajax ... oncomplete="chatTableWV_#{loop.index}.unselectAllRows();">

      

This generates the following JavaScript code:

window['chatTableWV_0'] = new Widget(tableElement1);
window['chatTableWV_1'] = new Widget(tableElement2);
window['chatTableWV_2'] = new Widget(tableElement3);
// ...

      

Finally, the PrimeFaces widget manager can find everything.

+3


source







All Articles