Group radio buttons inside a datatable - Primefaces - jsf

I want to create webpage having table with radio buttons in primefaces using datatable component like below.
I found one solution to implement custom component as mentioned in http://www.javaworld.com/article/2077687/enterprise-java/group-radio-buttons-inside-a-jsf-datatable-component.html for JSF.
But it is time taking and a lot of code.

Primefaces provides the custom layout for selectOneRadio. I implemented the table using the custom layout example of selectOneRadio as below. In this first column has width zero which contains the radio button.
<p:dataTable id ="employeeTable" rowIndexVar="rowId" var ="emp" value ="#{employeeList.employeeData}" widgetvar ="employeeTable" resizableColumns="true">
<p:column headerText="" style="width:0px;">
<p:selectOneRadio id="action" value="#{emp.status}" layout ="custom">
<f:selectItem itemLabel="Yes" itemValue="Yes" />
<f:selectItem itemLabel="No" itemValue="No" />
<f:selectItem itemLabel="Amendment" itemValue="Amendment" />
<f:selectItem itemLabel="KIV" itemValue="KIV" />
</p:selectOneRadio>
</p:column>
<p:column headerText="Personnel No">
<h:outputText value="#{emp.perNum}" />
</p:column>
<p:column headerText="Empl Name">
<h:outputText value="#{emp.name}" />
</p:column>
<p:column headerText="Yes">
<p:radioButton for="action" itemIndex="0" />
</p:column>
<p:column headerText="No">
<p:radioButton for="action" itemIndex="1" />
</p:column>
<p:column headerText="Amendment">
<p:radioButton for="action" itemIndex="2" />
</p:column>
<p:column headerText="KIV">
<p:radioButton for="action" itemIndex="3" />
</p:column>
</datatable>

Use p:selectOneRadio outside of your dataTable with layout attribute set to "custom". Fill this radio with f:selectItems same as data provided to dataTable via "value" parameter.
<p:selectOneRadio id="customRadio"
value="#{beanMB.selectedItem}"
layout="custom">
<f:selectItems value="#{beanMB.tableItems}"
var="item"
itemValue="#{item.radioValue}"
itemLabel="#{item.radioLabel}"/>
</p:selectOneRadio>
In dataTable column use p:radioButton with itemIndex matching current rowIndex.
<p:dataTable var="item"
rowIndexVar="rowIndex"
value="#{beanMB.tableItems}">
<p:column headerText="Choice">
<p:radioButton for="#form:customRadio"
itemIndex="#{rowIndex}"
rendered="#{item.radioRendered}"/>
</p:column>
</p:dataTable>

Related

How to add radio button in the row JSF 2

Please refer the following image:
and my code is as follows:
<h:selectOneRadio group="result" value="#{sampleCOntroller.testRslt}">
<p:column>
<f:selectItem itemValue="PS" />
</p:column>
<p:column>
<f:selectItem itemValue="NE" />
</p:column>
<p:column>
<f:selectItem itemValue="NA" />
</p:column>
</h:selectOneRadio>
but I don't even get the radio buttons and just blank is displayed.
How can I do this?

Tooltip in the Column Header of a Primefaces Datatable

In an application based on JSF 2.1 and Primefaces 6.0, I am trying to add a tooltip to the header of a datatable.
In my current solution, the tooltip appears only when pointing the mouse precisely on the text title ("Projekttyp"). I need the tooltip to appear whenever the mouse pointer is in the column header. Unfortunately, it is not possible to assign an id to the facet header.
I understand that the global tooltip as described here Primefaces Showcase Tooltip can only be used in higher Versions of JSF (JSF 2.2).
Here is my code:
<p:column sortBy="#{d.auftraggeber.typ}" filterBy="#{d.auftraggeber.typ}" field="auftraggeber.typ"
filterFunction="#{filterController.filterByString}" filterable="true" sortable="true"
width="6%" styleClass="#{Constants.STRING_COL_STYLE_CLASS}">
<f:facet name="filter">
<p:inputText onkeyup="clearTimeout(window.customFilterDelay);window.customFilterDelay=setTimeout(function() {PrimeFaces.getWidgetById('#{component.parent.parent.clientId}').filter();},1500)"
value="#{sucheForm.filter.typFilter}"
tabindex="1"
styleClass="input-filter #{Constants.NO_DIRTY_CHECK_STYLE_CLASS}">
<p:ajax event="keyup" update="#(.updateableFromTableFilter)" delay="1500" />
</p:inputText>
</f:facet>
<f:facet name="header">
<h:outputText id="typColumntitle" title="Projekttyp" value="Projekttyp"/>
<p:tooltip id="typTooltip"
for="typColumntitle"
rendered="true"
myPosition="left bottom" atPosition="right bottom"
hideDelay="500">
<p:scrollPanel style="width: 300px;height:200px">
<p:dataTable id="projektTypTable" var="typ" value="#{projekttypListProducer.typAndDescList}">
<p:column headerText="Projekttyp" width="30%">
<h:outputText value="#{typ.inhalt}"/>
</p:column>
<p:column headerText="Bemerkung" width="70%">
<h:outputText value="#{typ.bemerkung}"/>
</p:column>
</p:dataTable>
</p:scrollPanel>
</p:tooltip>
</f:facet>
<h:outputText value="#{d.auftraggeber.typ}"/>
</p:column>
Add the f:facet tag like the above and it will work fine (I mean tooltip value will be displayed); if you hover your mouse anywhere inside the column header.
Even outside the text in the column header.
<p:column id= "keyColumnId">
<f:facet name="header">
<h:outputText value="YOUR COLUMN HEADER" />
<p:tooltip value="TOOLTIP VALUE TO SHOW" for="keyColumnId" />
</f:facet>
</p:column>
PrimeFaces supports 'jquery' selectors in the for attribute of the p:tooltip. You effectively need to select the 'parent' of the element with id typColumntitle, so this will (most likely) work.
<p:tooltip id="typTooltip"
for="#(#typColumntitle:parent)"
rendered="true"
myPosition="left bottom" atPosition="right bottom"
hideDelay="500">
Solving this problem turned out to be easy in the end. I just had to assign an id to the column <p:column id="columnwithtooltip" and adjust the for="columnwithtooltip" in the tooltip accordingly:
<p:column id="projekttypColumn" sortBy="#{d.auftraggeber.typ}" filterBy="#{d.auftraggeber.typ}" field="auftraggeber.typ"
filterFunction="#{filterController.filterByString}" filterable="true" sortable="true"
width="6%" styleClass="#{Constants.STRING_COL_STYLE_CLASS}">
<f:facet name="filter">
<p:inputText onkeyup="clearTimeout(window.customFilterDelay);window.customFilterDelay=setTimeout(function() {PrimeFaces.getWidgetById('#{component.parent.parent.clientId}').filter();},1500)"
value="#{sucheForm.filter.typFilter}"
tabindex="1"
styleClass="input-filter #{Constants.NO_DIRTY_CHECK_STYLE_CLASS}">
<p:ajax event="keyup" update="#(.updateableFromTableFilter)" delay="1500" />
</p:inputText>
</f:facet>
<f:facet name="header">
<h:outputText id="typColumntitle" title="Projekttyp" value="Projekttyp"/>
<p:tooltip id="typTooltip"
for="projekttypColumn"
rendered="true"
myPosition="left bottom" atPosition="right bottom"
hideDelay="500">
<p:scrollPanel style="width: 300px;height:200px">
<p:dataTable id="projektTypTable" var="typ" value="#{projekttypListProducer.typAndDescList}">
<p:column headerText="Projekttyp" width="30%">
<h:outputText value="#{typ.inhalt}"/>
</p:column>
<p:column headerText="Bemerkung" width="70%">
<h:outputText value="#{typ.bemerkung}"/>
</p:column>
</p:dataTable>
</p:scrollPanel>
</p:tooltip>
</f:facet>
<h:outputText value="#{d.auftraggeber.typ}"/>
</p:column>

Can't select checkbox in primefaces datatable using selection

I have a dataTable in primefaces and wish to select multiple rows, but the checkbox doesn't highlight when I hover over it and can't be selected when I click.
The checkbox is definitely enabled. Here is the code:
<p:dataTable id="deferDatatable2"
value="#{deferMaintenanceTasksBean.tasksList}"
var="tasks"
rowKey="tasks.jobId"
selection="#{deferMaintenanceTasksBean.maintenanceTaskDatas}"
styleClass="editable-datatable" >
<p:ajax event="toggleSelect"
listener="#{deferMaintenanceTasksBean.toggleSelect()}" />
<p:ajax event="rowSelectCheckbox"
update="deferDatatable2" />
<p:ajax event="rowUnselectCheckbox"
update="deferDatatable2" />
<p:ajax event="rowSelect"
update="deferDatatable2" />
<p:ajax event="rowUnselect"
update="deferDatatable2" />
<p:column styleClass="select-column"
rendered="#{tasks.task.deferrable == 'true'}"
selectionMode="multiple" >
</p:column>
<p:column headerText="Task Code"
rendered="#{tasks.task.deferrable == 'true'}">
<h:outputText value="#{tasks.task.taskCode}" />
</p:column>
<p:column headerText="Job Code"
rendered="#{tasks.task.deferrable == 'true'}">
<h:outputText value="#{tasks.jobCode.code}" />
</p:column>
<p:column headerText="Description"
rendered="#{tasks.task.deferrable == 'true'}">
<h:outputText value="#{tasks.task.description}" />
</p:column>
<p:column headerText="Safety Critical"
rendered="#{tasks.task.deferrable == 'true'}">
<p:selectBooleanCheckbox value="#{tasks.task.safetyCritical}"
disabled="true" />
</p:column>
<p:column headerText="Deferrable?"
rendered="#{tasks.task.deferrable == 'true'}">
<p:selectBooleanCheckbox value="#{tasks.task.deferrable}"
disabled="true" />
</p:column>
<p:column headerText="Att."
rendered="#{tasks.task.deferrable == 'true'}">
<h:outputText id="i4AttachmentOutLbl"
value="#{tasks.task.attachmentCount}" />
</p:column>
</p:dataTable>
Inside the toggleSelect method there is just a log so I could see if the method is being called even though the checkboxes aren't being checked, but it isn't.
Like I say, the checkboxes all render as does everything else, and they aren't faded as if they were disabled, but they behave as if they were disabled. I have tried removing the styleClasses to see if they were somehow affecting things, but they aren't.
I have other working examples of this within my project and from what I can see I have set this dataTable up exactly the same, but for some reason it isn't working. The xhtml has the same ui:compposition and same outputStylesheets and outputScripts as these other pages and same basic layout, so I am convinced there is something I have missed from the dataTable, but no clue what.
I have added a little test table as follows with a list of strings called testList and this has worked. Not sure why this could be...
<p:dataTable id="testTable"
value="#{deferMaintenanceTasksBean.testList}"
var="test"
rowKey="test"
selection="#{deferMaintenanceTasksBean.testListSelect}">
<p:ajax event="toggleSelect"
listener="#{deferMaintenanceTasksBean.toggleSelect(1)}" />
<p:ajax event="rowSelectCheckbox"
update="testTable" />
<p:ajax event="rowUnselectCheckbox"
update="testTable" />
<p:ajax event="rowSelect"
update="testTable" />
<p:ajax event="rowUnselect"
update="testTable" />
<p:column selectionMode="multiple" />
<p:column>
<h:outputText value="#{test}" />
</p:column>
</p:dataTable>
Any suggestions appreciated.
Thanks
The removal of the rendered tags in your columns should then allow you to select the check boxes. I presume this is because it removes the top of your table and the toggle all check box causing the rest to fail silently

Primefaces datatable row expansion exporting

I need to export datatable row expansion in primefaces but i cannot figure it out. Is there anyway to do that ?
Thanks in advance.
You can export PrimeFaces DataTable with rowExpansion data without an other Java API or you can use custom export method on ManagedBean class with Apache POI API help.
Here trick is, add the columns which to be exporting to the file and append the visible=”false” attribute to the these columns.
Then append the exportable=”false” attribute to the p:rowToggler column.
Thus, you won’t see datatable columns but you will see these columns on the exported file.
Obtained from here
<h:form id="myDtTblFrm">
<h:commandLink>
<img src="../ims/excel.png"/>
<p:dataExporter type="xlsx" target="myTbl" fileName="myExcelFile"/>
</h:commandLink>
<p:dataTable id="myTbl" var="item" value="#{myBean.list}">
<p:rowExpansion>
<p:panelGrid columns="2" columnClasses="label, value" style="width: 50%">
<h:outputText value="Column Header 04" />
<h:outputText value="#{item.property04}" />
<h:outputText value="Column Header 05" />
<h:outputText value="#{item.property05}" />
</p:panelGrid>
</p:rowExpansion>
<p:column exportable="false">
<p:rowToggler />
</p:column>
<p:column headerText="Colum01">
<p:outputLabel value="#{item.property01}" />
</p:column>
<p:column headerText="Column02" visible="false" >
<p:outputLabel value="#{item.property02}" />
</p:column>
<p:column headerText="colum03" >
<p:outputLabel value="#{item.property03}" />
</p:column>
<p:column headerText="colum04" style="display: none">
<p:outputLabel value="#{item.property04}" />
</p:column>
<p:column headerText="colum05" style="display: none">
<p:outputLabel value="#{item.property05}" />
</p:column>
</p:dataTable>

Unable to get the selected values on Dialog while using ContextMenu with Datatable

I have a requirement where I want the user to move the datatable rows up and down.I thought Context Menu would come handy as there would be a Move up and Move down menu item .For this I tried implementing the context Menu for datatable example from Primefaces Showcase. However I am not getting the selected values from the datatable rows to be displayed on the dialog.
Please find the below code:
<p:contextMenu for="availableCars">
<p:menuitem value="View" update="display" icon="ui-icon-search" oncomplete="carDialog.show()"/>
</p:contextMenu>
<p:dataTable id="availableCars" var="car" value="#{RequestBean.formFields}" rowKey="#{car.car_group}" selectionMode="single" selection="#{RequestBean.selectedField}">
<p:column headerText="Field Label">
<h:outputText value="#{car.car_label}" />
</p:column>
<p:column headerText="Field Group">
<h:outputText value="#{car.car_group}" />
</p:column>
<p:column headerText="Field Name">
<h:outputText value="#{car.car_name}" />
</p:column>
</p:dataTable>
<p:dialog header="Car Detail" widgetVar="carDialog" resizable="false"
width="200" showEffect="clip" hideEffect="fold" id="dialog">
<h:panelGrid id="display" columns="2" cellpadding="4">
<h:outputText value="Car label:" />
<h:outputText value="#{RequestBean.selectedField.car_label}" style="font-weight:bold"/>
<h:outputText value="car Group" />
<h:outputText value="#{RequestBean.selectedField.car_group}" style="font-weight:bold"/>
<h:outputText value="Car Name:" />
<h:outputText value="#{RequestBean.selectedField.car_name}" style="font-weight:bold"/>
</h:panelGrid>
</p:dialog>
Any help would be appreciated.
N.B: I am using PF 2.2.1
It looks like the selection doesn't get sent using ajax - you need to set the onRowSelectUpdate attribute on the data table.
From Primefaces documentation:
In both single and multiple selection options described before, enclosing form needs to be submitted
by user so that the selections would be processed and set to the selection value reference. If you’d
like to execute custom logic whenever the row is selected instantly bind a rowSelectListeneror or define onRowSelectUpdate option.

Resources