1
votes

I need some help by Primefaces dataTable.

I try to implement dataTable with single selection mode. When I select any row in my table it is selected normally. But I see in debug mode that selectedUser field always have the same value (of the first table's item) regardless the real UI selection.

My question is what is wrong in my code?

Unfortunately google request "primefaces datatable wrong selection" did not return anything that would have helped me and I dared ask the community.

Also I found the following examples:
primefaces datatable selected row always the same value But it is also not my case.

I thought (according the google examples and PF documentation) when we select a row this action automatically triggers the "selection" object setter. Is it wrong?

My PF version is 5.3

The table has the following definition:

<p:dataTable scrollable="true" id="SearchResultsTable" value="#{searchFormBean.users}" var="item" scrollHeight="300"
     widgetVar="SearchResultsTable" selectionMode="single" selection="#{searchFormBean.selectedUser}" 
     rowKey="#{not empty item.usersListId ? item.usersListId : item.hashCode()}" emptyMessage="No rows found.">

     <p:ajax event="rowSelect" listener="#{searchFormBean.onResultTableRowSelect}" update=":searchPage:j_idt68:summaryTable" />

     // Columns

I used the following example as template

My "rowSelect" event method is the following:

public void onResultTableRowSelect(SelectEvent event) {

        summaryObjects.clear();

        summaryObjects.add(selectedUser);

    }

where summaryObjects is the List which I pass into another table.

Mentioned SearchFormBean is ViewScoped.

Could you please help me find my mistake. I'll provide any additional information if it necessary.

Thank you.

1
When using ajax rowSelect, I always retrieve the selected object via the event and implement an empty setter of the selection attribute, in your case setSelectedUserKukeltje
Hi @Kukeltje, thank you for your response It sounds good. I tried your method, but it did not work for me. I changed rowSelect method to selectedUser = (User) event.getObject(); , started debuger and found that event.getObject() returns the same object.Alex L.

1 Answers

0
votes

I found my mistake and solved the issue. It turned out that item.usersListId which I used as a rowKey for some item was null. It means that that value at least not unique. I supposed the construction not empty item.usersListId ? item.usersListId : item.hashCode() guaranteed the uniqueness. But in my case it was incorrect using. I just changed rowKey value by another unique and selection functionality became correct.

Thank you.