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

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;

Related

How to reset the sorting on dynamic colums at PrimeFaces?

I have dynamic columns which are sorted as well as filtered. I managed to clear the filters, but according to Primefaces there is no function to clear the sorting for the client API.
My .xhtml:
<p:dataTable id="tableId" value="#{statisticCreateBean.tableData}"
var="data" widgetVar="dataTbl"
rendered="#{not empty statisticCreateBean.tableData}">
<p:columns id="columnId" value="#{statisticCreateBean.columnModel}"
var="column" sortBy="#{data[column.fieldName]}"
filterBy="#{data[column.fieldName]}">
<f:facet name="header">
#{column.displayName}
</f:facet>
<h:outputText value="#{data[column.fieldName]}" />
</p:columns>
</p:dataTable>
My Java-code:
Columns columns = (Columns)FacesContext.getCurrentInstance().getViewRoot().findComponent("statisticForm:tableId:columnId");
columns.setSortBy(null);
PrimeFaces.current().executeScript("PF('dataTbl').clearFilters();");
Is there a way to clear the sorting on dynamic columns?
To reset sorts and filters properly get the Datatable and call reset.
DataTable datatable= (DataTable )FacesContext.getCurrentInstance().getViewRoot().findComponent("statisticForm:tableId");
datatable.reset();

Primefaces Accordion + Datatable filter / Multiple select not working as expected

I have a search screen that displays data grouped together based on the search value. This is how the display is accomplished. I have a p:accordionpanel that gets dynamically populated from the back end bean. I have a p:datatable with in the p:accordianpanel. There can be more than one accordion panel that gets displayed each containing a datatable. The display works as expected.
Problem: I have implemented filter and multiple selection of rows on the datatables. If there are more than 1 datatable that gets populated, the filter and search only works on the last datatable. The selection and filteredValue attributes of the p:datatable are bound to separate objects. If I click on any of the "Select All" boxes, the rows from the last table gets selected. I would expect the rows on the table that's associated with the "Select All" check box to be selected.
Here's the accordion / datatable:
<p:accordionPanel multiple="true"
value="#{basicSearchResultsVO.sortedMessages}" var="sortedMessages">
<p:tab title="#{sortedEdiMessages.key}">
<p:dataTable id="dataTable"
emptyMessage="No Data found with searched criteria"
filteredValue="#{sortedMessages.value.filteredMessages}"
rowKey="#{message.archiveSequenceI}"
rows="10"
selection="#{sortedMessages.value.selectedMessages}"
sortBy="#{message.msgDateTimeTs}" sortOrder="descending"
value="#{sortedMessages.value.messages}" var="message"
widgetVar="messagesTable">
<f:facet name="header">
<p:outputPanel style="text-align: right;margin:3px;">
<h:outputText value="Search all fields:"/>
<p:inputText id="globalFilter" onkeyup="PF('messagesTable').filter()" style="width:150px;" placeholder="Enter keyword"/>
</p:outputPanel>
</f:facet>
...
</p:dataTable>
</p:tab>
...
</p:accordionPanel>
I am not sure what I am missing. Would appreciate your assistance and feed back.
OK. I found the issue with the code. Had to make the widgetVar attribute on the datatable unique for the filter / multi select to work correctly on each datatable. Here's the modified code. The only change is to the widgetVar attribute of the p:datatable and the onkeyup attribute of p:inputText bound to a unique value provided by the backend bean.
<p:accordionPanel multiple="true"
value="#{basicSearchResultsVO.sortedMessages}" var="sortedMessages">
<p:tab title="#{sortedEdiMessages.key}">
<p:dataTable id="dataTable"
emptyMessage="No Data found with searched criteria"
filteredValue="#{sortedMessages.value.filteredMessages}"
rowKey="#{message.archiveSequenceI}"
rows="10"
selection="#{sortedMessages.value.selectedMessages}"
sortBy="#{message.msgDateTimeTs}" sortOrder="descending"
value="#{sortedMessages.value.messages}" var="message"
widgetVar="#{sortedEdiMessages.key}">
<f:facet name="header">
<p:outputPanel style="text-align: right;margin:3px;">
<h:outputText value="Search all fields:"/>
<p:inputText id="globalFilter" onkeyup="PF('#{sortedEdiMessages.key}').filter()" style="width:150px;" placeholder="Enter keyword"/>
</p:outputPanel>
</f:facet>
...
</p:dataTable>
</p:tab>
...
</p:accordionPanel>

create DataTable with Two dimensional with InputText into cells

I need create an primefaces Datatable with InputText into cells and get de information edited in the inputText
I create the datatable with the next code:
<p:dataTable value="#{busquedaArticulo.attr}" var="rowName" styleClass="table table-bordered table-stripped" rowIndexVar="rowIdx">
<p:column headerText="" styleClass="ui-widget-header">
<h:outputText value="#{rowName}"/>
</p:column>
<p:columns value="#{busquedaArticulo.siz}" var="columnName" headerText="#{columnName}" columnIndexVar="colIdx">
<h:inputText id="siz" value="#" styleClass="form-control" />
</p:columns>
</p:dataTable>
I suggest you to use editable row. you can show value of each entry if it has been set by user or has a default value or leave it empty or "not set" if it's not set.
user can click on pencil icon and edit value of each row. once row has been edited you can get new value in your backing bean.
look at this: http://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml
hope it was useful :)

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 can I rerender a whole row in a datagrid?

I have the following code, using PrimeFaces 3.0.M3, Tomcat 7 and Mojarra 2.1.3.
<p:dataGrid columns="1" var="answer" value="#{answersbysubject}">
<p:column>
<p:panel columns="1">
<h:outputText value="#{answer.question.name}" />
<p:dataGrid columns="5" var="selection" value="#{brain.selections}">
<p:column>
<h:panelGrid columns="1" styleClass="selections" id="mySelection">
<h:outputText value="#{selection.name}" />
<p:commandLink update="mySelection">
<p:graphicImage value="/img/CheckboxFull.png" rendered="#{selection == answer.selection}" />
<p:graphicImage value="/img/CheckboxEmpty.png" rendered="#{selection != answer.selection}" />
<f:setPropertyActionListener value="#{selection}" target="#{answer.selection}" />
</p:commandLink>
</h:panelGrid>
</p:column>
</p:dataGrid>
</p:panel>
</p:column>
</p:dataGrid>
sorry if a bit too verbose...
The point is: I want to realize a nicer 5-way checkbox (working like a set of radio buttons).
The whole works fine serverside, except that when I click another checkbox it rerenders ONLY the checkbox I just clicked. That is, I see two checked checkboxes - the old one doesn't clear until I do the F5 refresh in browser.
I tried to give ids to each tag and then let the commandLink update all, but nothing else than the current checkbox gets updated :(
The ids of a table row get probably mixed up during dynamic generation, is this why no other tags can be found/identified reliably for update? If this is the situation, then what would be the solution for me to update ONLY the current table row (not only the current table cell like now...)?
Many thanks!

Resources