Data Table row selection(single) in Primefaces 3.5 selects all rows - jsf

I need to add and remove rows in DataTable and using buttons to add and remove the rows respectively by single selection.
My html/jsf code is :
<p:dataTable id="creditTable" rowIndexVar="sn"
value="#{cpeTrainingMB.cpeTrainingObj.cpeTrainigSchDetail}"
var="cpeDetail" scrollable="true" scrollHeight="100"
selectionMode="single"
rowKey="#{cpeDetail.id}">
<p:ajax event="rowSelect" update=":cpe:creditTable" listener="#{cpeTrainingMB.onRowSelect}"/>
<p:column headerText="Sn">
<h:outputText value="#{sn+1}"></h:outputText>
</p:column>
<p:column headerText="Date">
<h:outputText value="#{cpeDetail.fromDtBS}"></h:outputText>
</p:column>
<p:column headerText="Credit HR">
<h:outputText value="#{cpeDetail.creditHr}"></h:outputText>
</p:column>
</p:dataTable>
The problem is that when I add more than one data, and then select a row to delete , it selects all the rows at once which is not required.
It works well for only one row.
The backing methods/functions are listed below :
public void onRowSelect(SelectEvent event){
cpeScheSelected = null;
this.cpeScheSelected = (CPETrainingScheduleModel)event.getObject();
cpeTrainingScheObj = null;
cpeTrainingScheObj = this.cpeScheSelected;
}

All elements listed in the dataTable must have a not-null value in the rowKey property (in this case - id field). Nulls will lead to the behaviour described in the question. An alternative to specifying rowKey is implementing SelectableDataModel with custom method public Object getRowKey(RowClass rc).

Related

Primefaces dataTable multiple selection same object

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!

using hashmap to retrieve datatable column value

I have a dataTable displaying data of type Action. I have a column named "Status" which will be set by the user. After setting this column I want to retrieve the value from a HashMap with Action class as key and the status as value.
How could I do it directly in the dataTable status column? I attempted the following:
<p:dataTable var="currentExecutedTestAction" value="#{projectCampaignManagementMB.currentExecutedTestActionList}">
<p:column>
<f:facet name="header">
<h:outputText value="Status" />
</f:facet>
<h:outputText value="#{projectCampaignManagementMB.actionMap(currentExecutedTestAction,statusValue)}" />
</p:column>
</p:dataTable>
Just use the action as a key (I suppose that the action is some String/Integer etc that can be used as a key).
<h:outputText value="#{projectCampaignManagementMB.actionMap[action]}"/>
JSF will use the action as a key to get the value, and than after you will submit some new value it will use action again to set the new value.

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" />

How to have checkbox selection in datatable with instant row selection?

I use primefaces 3.0.1 and I have a problem with my datatable.
I use my datatable with the instant row selection. That works very well.
But I wish use in the same time the checkbox selection (for exemple, for select some rows and delete the selected rows)
And when I use the
<p:column selectionMode="multiple" />
the checkbox are display, but I can't check any checkbox....
Anyone have a solution ?
Thanks.
P.S. :
my code
<p:dataTable id="rows" var="row" value="#{myBean.row}" selection="#{myBean.selectedRow}" selectionMode="single">
<p:ajax event="rowSelect" listener="#{myBean.onRowSelect}" update="#form"/>
<p:column selectionMode="multiple" style="width:18px" />
<p:column>
<h:outputText value="#{row.subject}" />
</p:column>
</p:dataTable>
Checkboxes are to be used for multiple row selection, but you've still declared your <p:dataTable> for single row selection. Remove selectionMode="single" from the <p:dataTable> and make sure that the #{myBean.selectedRow} is changed to be an array instead.
E.g.
<p:dataTable ... selection="#{myBean.selectedRows}">
with
private Row[] selectedRows;

JSF Rich scroallable Datatable Issue

In a scrollable datatable, which displays a collection of objects , I have to add one manual row as first row. That row contains inputText, through which user can able to enter some values and while hitting enter, those values will save and displays in bottom rows. I couldnt able to add this manual row as first row. Below is the current code.
<rich:scrollableDataTable value="{resultList}" var="result">
<rich:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{result.name}" />
</rich:column>
<rich:column>
<f:facet name="header">Category</f:facet>
<h:outputText value="#{result.category}" />
</rich:column>
</rich:scrollableDataTable>
The above code is for just displaying the values from the backend.
1) For that you have to use bind rich:scrollableDataTable with the instance of HtmlScrollableDataTable in the backing bean.
In backing bean, create instance of it with accessor methods & then you can initialize it accordingly by adding the inputText components. Add actionListeners to these input components & then in listeners, you can add these inputText values as outputText to the table again as rows accordingly.
2) Else you can use inputText rather then outputText & disabling the subsequent rows other then 1st, so only data may get displayed - preventing input.
<rich:scrollableDataTable value="{resultList}" var="result">
<rich:column>
<f:facet name="header">Name</f:facet>
<h:inputText value="#{result.name}" disabled ="#{!result.isFirstRow}"/>
</rich:column>
<rich:column>
<f:facet name="header">Category</f:facet>
<h:inputText value="#{result.category}" disabled ="#{!result.isFirstRow}"/>
</rich:column>
</rich:scrollableDataTable>
Backing Bean :
//---
public void initialize(){
resultList.add(new Result("", "", true)); // Setting 1st input row enabled
}
public void inputListener(ActionEvent event){
// appending object based on input to the resultList
resultList.add(new Result(inputName, inputValue, false));
// added a boolean field to identify rows added later & to make them enable/disable accordingly
}
//---
I am not familiar with Richfaces, but tried to achieve it, as I was doing it with IceFaces.

Resources