Primefaces dataTable multiple selection same object - jsf

I'm having a problem with the dataTable multiple selection with checkbox (like the second one: http://primefaces.org/showcase/ui/datatableRowSelectionRadioCheckbox.jsf).
When I try to execute a method in my backingBean, the selected items always comes with the right size, but, with the same object.
Example: I select in my dataTable three Messages: Message 1, Message 2 and Message 3. When I execute my method in the JSF page my selected items comes like this:
Messages[0] = Message 1
Messages[1] = Message 1
Messages[2] = Message 1
Here is my JSF Page:
<p:dataTable id="mensagensLidas" var="msg" value="#{mensagensBean.msgGrupoModel}"
paginator="true" rows="10" selection="#{mensagensBean.selectedMsgsGrupo}">
<p:column selectionMode="multiple" style="width:2%"/>
<p:column headerText="Titulo:" style="width:49%">
#{msg.titulo}
</p:column>
<f:facet name="footer">
<p:commandButton id="multiViewButton" value="#{msgs.delete}" icon="ui-icon-trash" actionListener="#{mensagensBean.excluirMsgsGrupo}" update=":tabMain" ajax="true"/>
</f:facet>
</p:dataTable>

Your above code is working fine for me except that I'm using a rowKey.
Try using rowKey attribute in p:dataTable
<p:dataTable id="mensagensLidas" var="msg" value="#{mensagensBean.msgGrupoModel}"
paginator="true" rows="10" selection="#{mensagensBean.selectedMsgsGrupo}"
rowKey="#{msg.titulo}">
....
</p:dataTable>
Using rowKey is must and best practice when you have selection in Datables, it helps to map the selected <tr> to the POJOs that you're filling up with.
Usually its even better if your rowKey is unique propety, you can even put an integer variable in your #{msg} POJO as best practice.

I discovered the problem. My DataModel was with the getRowData returning always the same object. I was doing a wrong comparison. Thank you all!

Related

PrimeFaces dataTable convert values for showing in a view

I have a question based in the following situation.
I have a backing bean that create a matrix with ints, in rows, columns and cells (tablaBean). I have no problem displaying it in a jsf view. Each one of those ints, it´s an ID of differents objects. How can I do, if I want to show in my JSF view, instead of ints, some attributes of the objects?
This is my dataTable:
<p:dataTable var="rowName" value="#{tablaBean.rowNames}" rowIndexVar="rowIdx">
<p:column headerText="Alumnos:">
<h:outputText value="#{rowName}"/>
</p:column>
<p:columns var="colName" value="#{tablaBean.colNames}"
headerText="#{colName}" columnIndexVar="colIdx">
<p:panel>
<h:outputText value="#{tablaBean.data2D[rowIdx][colIdx]}"/>
</p:panel>
</p:columns>
</p:dataTable>
So for example:
tablaBean.data2D[1][1]=3
But I don´t want to show the number "3" in my view.
I want to take that number and show the Person.name of my object Person.id=3;
Hope my question is clear enough.
Create in your backing bean method returning person name:
public String getPersonNameById(int idPerson) {
return myDao.findPersonById(idPerson).getName();
}
Use it in xhtml:
<h:outputText value="#{tableBean.getPersonNameById(tablaBean.data2D[rowIdx][colIdx])}"/>

Primefaces DataTable and button invocation issue

I have a DataTable that presnt a list of product :
<p:dataTable var="product" value="#{myModel.products}"
scrollable="true" id="productsTableId" liveResize="true"
resizableColumns="true">
<f:facet name="header">
List of products
</f:facet>
<p:column headerText="Id" sortBy="id">
<h:outputText value="#{product.id}" >
</h:outputText>
</p:column>
<p:column headerText="Name" sortBy="name">
<h:outputText value="#{product.name}" title="#{product.name}" />
</p:column>
<p:column headerText="Remove product">
<p:commandButton icon="ui-icon-close" title="delete product"
value="Disconnect"
action="#{controller.delete(product)}"
update="productTableId">
<p:confirm header="Confirmation"
message="Are you sure you want to delete this product"
icon="ui-icon-alert" />
</p:commandButton>
</p:column>
</p:dataTable>
but the problem comes when i try to sort the datatable, the rows change their diplay order but the buttons functionalities keep the initial order ,i.e. if i have for example two products :
Id Name Remove product
1 B Delete B
2 A Delete A
after sorting by name
Id Name Remove product
2 A Delete A
1 B Delete B
If i click 'Delete A' then the product B is removed , is this a bug in PrimeFaces or i'm missing something .
i'm using PrimeFaces 4.0 ,JSF Mojarra 2.1.20 on jetty-9.1.2.
I've found the source of problem, the bean myModel was set to a request scope, i've change it to view scope.
Explanation :
When we load the page for the first time one managed bean myModel is created and used, when we sort data , a second managed bean is created , when we click on delete button we ask server to delete the product that has to index order of the pressed button , the server try first to create instance of the managed bean myModel , then it look for the product with the index order posted and remove it. this senario cause the issue. to avoid this we use the view scope to keep using the sapce instance of myModel created when the page is loaded. with the view scope , only one instance will be created and used in the same senario specified here.

How to render the p:commandButton at last row of the p:datatable?

I need to render the <p:commandButton> at last entry of the <p:datatable> only .
consider if p:datatable having n no.of rows means i have render the p:commandButton at nth row only.
I think that you better use <f:facet name="footer">
<f:facet name="footer">
<p:commandButton/>
</f:facet>
But if you really insist on last row...
Try the rowIndexVar, something like this :
<p:dataTable value="#{myBean.myList}" rowIndexVar="rowIndex" var="item">
<p:column>
<p:commandButton rendered="#{(rowIndex+1) eq myBean.myList.size()}"/>
</p:column>
</p:dataTable>
I don't know if I got the functional requirement right, but from what I understand you want to add a single commandButton at the bottom of the table, for that you might use the following inside datatable tags:
<f:facet name="footer">
<p:commandButton/>
</f:facet>

JSF filterBy stalls and everything else in the page gets blocked

I have a dataTable and in a column a filterBy parameter.
When I type something on the filter box, it indeed filters the table but when I delete or change what I wrote on that field nothing happens and every other action in the page is blocked.
I mean, after that if I click on a button nothing happens, no event-action is performed.
I'm using JSF 2.1, on Tomcat 7, with EE5.
What are your suggestions? Thanks a lot in advance.
XHTML code fragment:
<p:dataTable value="#{consolidacionAslBean.listaMunicipios}"
rowKey="#{municipio}" var="municipio" paginator="true"
rows="5" id="tableMunicipio"
selection="#{consolidacionAslBean.municipios}" style="font-size: 13px"
filteredValue="#{consolidacionAslBean.filteredMunicipios}"
>
<p:column
filterBy="#{municipio.catMunicipios.id.cdgMunicipio}"
headerText="Clave">
<h:outputText value="#{municipio.catMunicipios.id.cdgMunicipio}" />
</p:column>
And bean the proper objects:
private List<AslMunicipios> filteredMunicipios;
private List<AslMunicipios> listaMunicipios;
Thanks!

Value of <h:selectBooleanCheckbox> inside <p:dataTable> remains false on submit

I have a primefaces datatable and inside the primefaces datatable, I have a column, which contains the . The issue is,I have set the default value for the as false. When I click/check the , its still retrieving the value as false. I tried it multiple times but not sure why its returning false. Please find the sample code below.
<p:dataTable id="review-table" var="item" value="#{demandBean.filterVOList}">
<p:column id="SelectallID" style="text-align: left; width:40px;" rendered="#{demandBean.screeRenderVo.selectAllRenderer}">
<f:facet name="header" >
<h:outputText id="selectId" value="#{demandBean.dmdScreenLabelVO.selectAll}" />
<div></div>
<h:selectBooleanCheckbox id="checkbox1" value="Select All" onclick="checkAll(this)"/>
</f:facet>
<h:selectBooleanCheckbox id="checkbox2" value="#{item.selected}"/>
</p:column>
Im getting the value as false, when I check the and click on the save button. I have written an Action listerner, below is the code corresponding to the actionListener
public void saveData(ActionEvent event)
{
System.out.println("Entering the Save :");
selected = isSelected();
System.out.println("value of Selected"+selected);
}
I have tried debugging the code as well, but not sure why the value for is getting displayed as false. Please Assist. Thanks in Advance
You seem to be binding the value of all checkboxes in the column to the one and same bean property. This way the value will ultimately end up to be the one of the last row in the column.
This is not how it's supposed to be used.
You basically need to bind the value of the checkbox to the property of the currently iterated row object (the one behind the var attribute of the datatable).
<p:dataTable value="#{bean.items}" var="item">
<p:column>
<p:selectBooleanCheckbox value="#{item.selected}" />
Alternatively, you could use the <p:column selectionMode="multiple" /> to use builtin multiple selection support of the PrimeFaces datatable (see also the showcase example).
<p:dataTable value="#{bean.items}" var="item" rowKey="#{item.id}" selection="#{bean.selectedItems}">
<p:column selectionMode="multiple" />

Resources