PrimeFaces 12 DataExporter decimal numbers xls - primefaces

When updating to PrimeFaces 12, DataExporter presented an error in formatting decimal numbers in xls. Example: 50.00 is being printed 5,000
Note: the attempt below solved the problem locally (win10), but the problem persists on the server.
<f:facet name="header">
<div align="center">
<p:outputPanel style="float: right;">
<h:commandLink id="gerarXls" styleClass="table-button generateXls">
<p:graphicImage name="/images/table-icons/file-xls.svg"/>
<p:dataExporter type="xls" target="datatableOrdemServico" fileName="Ordens de Servico"
options="#{dataExporterCustomizedView.excelOpt}"/>
</h:commandLink>
</p:outputPanel>
</div>
</f:facet>
<p:column width="50" style="text-align: right"
sortBy="#{ordemServico.totalGeral}" filterMatchMode="contains" filterBy="#{ordemServico.totalGeral}">
<f:facet name="header">
<h:outputText value="Total OS"/>
</f:facet>
<h:outputText value="#{ordemServico.totalGeral}" >
<f:convertNumber minFractionDigits="2" locale="pt-BR"/>
</h:outputText>
</p:column>
public class DataExporterCustomizedView implements Serializable {
private ExcelOptions excelOpt;
#PostConstruct
public void init() {
excelOpt = new ExcelOptions();
excelOpt.setFacetBgColor("#F88017");
excelOpt.setFacetFontSize("10");
excelOpt.setFacetFontColor("#0000ff");
excelOpt.setFacetFontStyle("BOLD");
excelOpt.setCellFontColor("#00ff00");
excelOpt.setCellFontSize("8");
excelOpt.setStronglyTypedCells(true);
excelOpt.setNumberFormat(new DecimalFormat("#,##0.00"));
excelOpt.setCurrencyFormat((DecimalFormat) DecimalFormat.getCurrencyInstance(new Locale("pt", "BR")));
}
public ExcelOptions getExcelOpt() {
return excelOpt;
}
}

From this ticket: https://github.com/primefaces/primefaces/issues/8961
Setting a custom per cell basis is not supported and is out of scope
of this issue.
Currently, DataExporter detection feature only considers the property
"CurrencyFormat" or "DecimalFormat" in ExcelOptions or, if it's not
defined, in faces-config.xml.
Or by setting it in faces-config.xml
faces-config.xml
<locale-config>
<default-locale>pt_BR</default-locale>
</locale-config>
My Guess: Your server is in a differnet locale so that is what Faces is defaulting to because you are missing the explicit locale setting in your faces-config.xml.

Related

JSF Datatable - how to select row on button push in row

I would like a button in a Primefaces DataTable row to show a dialog showing more information about the object in the row. When I click anywhere in the row not in the button, the row is selected. However, when I press the button, the row is not selected. How may I make the row that the button is in the selected row?
This example from the Primefaces showcase sets selectedCar in the backing bean and displays a dialog containing data from the row on clicking a button in the row but leaves the row unselected:
<p:dataTable id="basicDT" var="car" value="#{dtSelectionView.cars1}">
<f:facet name="header">
Basic
</f:facet>
<p:column headerText="Id">
<h:outputText value="#{car.id}" />
</p:column>
<p:column headerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Brand">
<h:outputText value="#{car.brand}" />
</p:column>
<p:column headerText="Color">
<h:outputText value="#{car.color}" />
</p:column>
<p:column style="width:32px;text-align: center">
<p:commandButton update=":form:carDetail" oncomplete="PF('carDialog').show()" icon="ui-icon-search" title="View">
<f:setPropertyActionListener value="#{car}" target="#{dtSelectionView.selectedCar}" />
</p:commandButton>
</p:column>
</p:dataTable>
.. and this example from the same page selects a row in the table and the backing bean but an subsequent button click to display a dialog:
<p:dataTable id="singleDT" var="car" value="#{dtSelectionView.cars2}" selectionMode="single" selection="#{dtSelectionView.selectedCar}" rowKey="#{car.id}">
<f:facet name="header">
Single with Row Click
</f:facet>
<p:column headerText="Id">
<h:outputText value="#{car.id}" />
</p:column>
<p:column headerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Brand">
<h:outputText value="#{car.brand}" />
</p:column>
<p:column headerText="Color">
<h:outputText value="#{car.color}" />
</p:column>
<f:facet name="footer">
<p:commandButton process="singleDT" update=":form:carDetail" icon="ui-icon-search" value="View" oncomplete="PF('carDialog').show()" />
</f:facet>
</p:dataTable>
I'm looking for a graceful solution where you can click any of multiple buttons in a row and select the row at the same time. Here's a use case where multiple buttons are useful - the data for the row contains two richtext fields of arbitrary size which are not easily shown in the table:
Use the var value of the primefaces dataTable attribute, to create a commandLink (or button) inside each row of the dataTable:
If the commandLink is clicked, an actionListener is invoked and sets the rows object as the selectedElement inside the dataTableDialog bean.
Once the ajax request as finished successfully, the update attribute of the commandLink forces the dialog to request the current data from the bean.
Now the JavaScript code of the oncomplete attribute shows up the dialog.
Take a look at the actionListener of the commandLink.
The rows object is stored inside member variable selectedElement. The data of this selected element is shown by the dialog.
Here you've got a nearly complete example:
<h:form id="form">
<p:dialog widgetVar="dlg" modal="true" id="dialog">
<h:outputText value="#{dataTableDialog.selectedElement.key} / #{dataTableDialog.selectedElement.val}" />
</p:dialog>
<p:dataTable
var="cur"
tableStyle="width: auto !important;"
value="#{dataTableDialog.elements}">
<p:column>
<h:outputText value="#{cur.key}" />
</p:column>
<p:column>
<h:outputText value="#{cur.val}" />
</p:column>
<p:column>
<p:commandLink
value="Read more ..."
actionListener="#{dataTableDialog.setSelectedElement(cur)}"
update="form:dialog"
oncomplete="PF('dlg').show()" />
</p:column>
</p:dataTable>
</h:form>
The bean:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
#javax.inject.Named
#javax.enterprise.context.SessionScoped
public class DataTableDialog implements Serializable {
private List<Data> elements;
private Data selectedElement;
#PostConstruct
public void init() {
elements = new ArrayList<>();
elements.add(new Data("Elem 1 Key", "Elem 1 Value"));
elements.add(new Data("Elem 2 Key", "Elem 2 Value"));
}
public List<Data> getElements() {
return elements;
}
public Data getSelectedElement() {
return selectedElement;
}
public void setSelectedElement(Data selectedElement) {
this.selectedElement = selectedElement;
}
}
The data class:
public class Data implements Serializable {
private String key, val; // +getter/+setter
public Data(String key, String value) {
this.key = key;
this.value = value;
}
}
Inspired by the primefaces show case for dataTable Selection:
if it is an option to ommit the button, this example opens a dialog on row click, including row selection.
add an ID to your DataModel
add the attributes selection, selectionMode and rowKey to the dataTable
insert <p:ajax ... /> tag inside dataTable to show the dialog on rowSelectEvent
The facelet:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form id="form">
<p:dialog
widgetVar="elementDialog" modal="true">
<p:outputPanel id="elementDetail">
<p:panelGrid
columns="2"
rendered="#{not empty bean.selectedElement}"
columnClasses="label,value">
<h:outputText value="Key: #{bean.selectedElement.key}" />
<h:outputText value="Val: #{bean.selectedElement.val}" />
</p:panelGrid>
</p:outputPanel>
</p:dialog>
<p:dataTable
var="element"
value="#{bean.elements}"
selection="#{bean.selectedElement}"
selectionMode="single"
rowKey="#{element.id}"
tableStyle="width: auto !important;">
<p:ajax event="rowSelect" oncomplete="PF('elementDialog').show();" />
<p:column headerText="Key">#{element.key}"</p:column>
<p:column headerText="Val">#{element.val}"</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
<p:dataTable
var="element"
value="#{bean.elements}"
selection="#{bean.selectedElement}"
selectionMode="single"
rowKey="#{element.id}"
tableStyle="width: auto !important;">
<p:ajax
event="rowSelect"
oncomplete="PF('elementDialog').show();" />
...
</p:dataTable>
The Data class:
public class Data implements Serializable {
private int id; // + getter/setter
private String key, val; // + getter/setter
public Data(int id, String key, String value) {
super();
this.setId(id);
this.key = key;
this.value = value;
}
}
The bean:
public class Bean implements Serializable {
private List<Data> elements; // + getter
private Data selectedElement; // + getter/setter
#PostConstruct
public void init() {
elements = new ArrayList<>();
elements.add(new Data(0, "Elem 1 Key", "Elem 1 Value"));
elements.add(new Data(1, "Elem 2 Key", "Elem 2 Value"));
}
}
Hopefully this example leads you to archive your goal ... ;)

PrimeFaces <p:cellEditor> not changing value of variable

I've done some research and haven't been able to find anything that directly addresses the problem I've been having.
I have a DataTable column that is editable (by cell) and I have the ability to click on the cell, enter a new number in the cell editor box, but when I hit the cell editor closes but does not save the new value (same thing happens if you just click off and don't hit enter).
Here is the Code Snippet
<h:form>
<c:forEach items="#{extensionsBean.getPhases()}" var="phase">
<p:fieldset legend="#{phase.getPhaseName()}">
<p:dataTable value="#{extensionsBean.getActivities(phase)}" var="activity" editable="true" editMode="cell">
<p:column>
<f:facet name="header">
<h:outputText value="Actual Hours"/>
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{activity.getEstimateDetail().actualHours}"/>
</f:facet>
<f:facet name="input">
<h:inputText value="#{activity.getEstimateDetail().actualHours}" />
</f:facet>
</p:cellEditor>
//the rest of the closing tags are present
Any suggestions would be great! For the record, I have basically the exact same setup in a different xhtml page and I'm able to edit each of the cells without a problem. Not sure what's causing this one to bug out on me.
Add Ajax event cellEdit and store the values
Add ajax event celledit and where do you want save the value you can save it.
Code is below in my project
Xhtml page(Primefaces):
<p:dataTable var="my" value="#{java.custom_info}" editMode="cell" editable="true" style="font-size: 12px">
<p:ajax event="cellEdit" listener="#{java.custom_detail}"/>
<p:column style="background: white">
<h:outputText value="First Name"/>
</p:column>
<p:column style="background: white">
<p:cellEditor>
<f:facet name="output"> <h:outputText value="#{my.first}"/> </f:facet>
<f:facet name="input"><p:inputText value="#{my.first}" placeholder="Enter First Name" style="width:93%"/></f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
Java Class
public void custom_detail(CellEditEvent event){
String old_str = (String) event.getOldValue();
String first_name = (String) event.getNewValue();
System.out.println("update method reached..."+first_name);
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
Query db = new Query();
Customer bean = new Customer();
if(session!=null){
if(((String)session.getAttribute("login"))!=null){
Calendar currentDate=Calendar.getInstance();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datenow=format.format(currentDate.getTime());
session.setAttribute("firstname", first_name);
session.setAttribute("modified_date",datenow);
bean.setFirst(first_name);
bean.setUsrid((String)session.getAttribute("user"));
bean.setUsr_modified(datenow);
db.update_personalinfo(bean);
}
}
}

Selected row is null using different forms

I am getting a null selected row when I press the showDialog command button
I can't see what is my problem
This is my first form:
<h:form id="firstForm">
<p:commandButton action="#{testBB.showDialog}" id="showDialog"
update=":secondForm" value="#{msg['show.dialog']}" />
</h:form>
This is my second form:
<h:form id="secondForm">
<p:dataTable id="testDatatable"
rendered="#{not empty testBB.list}"
rowKey="#{order.orderNumber}"
selection="#{testBB.selectedRow}"
selectionMode="single"
sortBy="customerName" value="#{testBB.list}" var="order">
<p:column headerText="#{msg['order.number']}">
<h:outputText value="#{order.orderNumber}" />
</p:column>
<p:column headerText="#{msg['total.value']}">
<h:outputText value="#{order.totalValue}" />
</p:column>
</p:dataTable>
</h:form>
My backing bean:
#ManagedBean
#ViewScoped
public class TestBB implements Serializable {
private List<Order> list;
private Order selectedRow;
public void showOrder() {
try {
System.out.println(selectedRow);
} catch (Exception exception) {
}
}
}
And my DTO:
public class Order implements Serializable {
private int orderNumber;
private double totalValue;
public void showOrder() {
try {
System.out.println(selectedRow);
} catch (Exception exception) {
}
}
/** Getters and setters */
}
What is wrong in my code?
Put
<p:ajax event="rowSelect" />
<p:ajax event="rowUnselect" />
inside your datatable for selection/unselection to happen as soon as click happens.
If you need selection only on button click use 'process' like this
<p:commandButton process=":secondForm:testDatatable" update=":secondForm"/>
You need to update the model on rowSelection. This can be done using a <p:ajax event="rowSelect" />. Here is how I think your Datatable should look like:
<p:dataTable id="testDatatable"
rendered="#{not empty testBB.list}"
rowKey="#{order.orderNumber}"
selection="#{testBB.selectedRow}"
selectionMode="single"
sortBy="customerName" value="#{testBB.list}" var="order">
<p:ajax event="rowSelect" />
<p:column headerText="#{msg['order.number']}">
<h:outputText value="#{order.orderNumber}" />
</p:column>
<p:column headerText="#{msg['total.value']}">
<h:outputText value="#{order.totalValue}" />
</p:column>
</p:dataTable>
A good example can be found here (Primefaces Demo).
As Kerem Baydogan suggested you have to include process attribute with your data table id in your command button.
If you are not including process attribute then no components will be processed in the component tree and no modal values will be updated hence you are getting selection as null.
I feel there is no need of updating data table if you are not changing its state.

how to get the database values by using hibernate and how to display that values in a primefaces dataTable?

Here i have some doubts.If you know the answer please post here.
How to get the list of values into database table by using hibernate?
How to display that values into primefaces dataTable?
Here i am posting what i am trying .see below
<p:dataTable id="users" value="#{user.listUsers}" var="user"
sortBy="user.id" rows="10" style="width: 30%">
<p:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet>
<h:outputText value="#{user.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{user.name}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Surname" />
</f:facet>
<h:outputText value="#{user.surName}" />
</p:column>
</p:dataTable>
Here i am finding the answer how to display a table values into primefaces dataTable.
Here i am placing what i am trying code.
<p:dataTable value="#{actions.messages}" var="act">
<p:column style="width:10%;text-align:left" filterBy="#{act.id}" >
<f:facet name="header">
<h:outputText value="ID" id="id" />
</f:facet>
<h:outputText value="#{act.id}" />
</p:column>
<p:column style="width:20%;text-align:left"
sortBy="#{act.action}">
<f:facet name="header">
<h:outputText value="Customer" id="customer" />
</f:facet>
<h:outputText value="#{act.action}" />
</p:column>
<p:column style="width:40%;text-align:left"
sortBy="#{act.scheduled}">
<f:facet name="header">
<h:outputText value="Date" />
</f:facet>
<h:outputText value="#{act.scheduled}" />
</p:column>
<p:column style="width:30%;text-align:left"
sortBy="#{act.status}">
<f:facet name="header">
<h:outputText value="Action" />
</f:facet>
<h:outputText value="#{act.status}" />
</p:column>
</p:dataTable>
Here is the hibernate dao class name called ActionsDao
public class ActionsDao {
public Session session;
public ArrayList<Actions> getActions() {
session = Util.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try {
#SuppressWarnings("unchecked")
ArrayList<Actions> actionsList = (ArrayList<Actions>) session
.createQuery("from Actions").list();
tx.commit();
boolean found = false;
for (Actions actions : actionsList) {
Actions action = new Actions();
action.setId(actions.getId());
action.setAction(actions.getAction());
action.setActual(actions.getActual());
action.setAssignedBy(actions.getAssignedBy());
action.setAssignedTo(actions.getAssignedTo());
action.setScheduled(actions.getScheduled());
action.setOutcome(actions.getOutcome());
action.setStatus(actions.getStatus());
actionsList.contains(action);
found = true;
}
if (found) {
return actionsList;
} else {
return null;
}
} catch (Exception e) {
System.out.println("Error In getActions() -->" + e.getMessage());
return (null);
} finally {
session.close();
}
}
}
Here is the bean class i am developing name called Actions.
#ManagedBean(name = "actions", eager = true)
#SessionScoped
public class Actions implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String action;
private String assignedTo;
private String assignedBy;
private Date scheduled;
private Date actual;
private String outcome;
private String status;
//write here setter & getter methods of the above properties
public ArrayList<Actions> getMessages() {
ActionsDao dao = new ActionsDao();
return dao.getActions();
}
}
you can use the annotations or hbm file that yours choice to map the bean class.
Here i am using only mapping file why because, first i am trying to create annotations but it raised some problems that jar files are some problem.your using the correct jar files that not a problem you can continue the annotations otherwise hbm file is best.

Primefaces Editable Datatable example from official site primefaces.org does not work

<p:dataTable var="car" value="#{tableBean.carsSmall}" id="carList" editable="true">
<f:facet name="header">
In-Cell Editing
</f:facet>
<p:ajax event="rowEdit" listener="#{tableBean.onEdit}" update=":form:messages" />
<p:ajax event="rowEditCancel" listener="#{tableBean.onCancel}" update=":form:messages" />
<p:column headerText="Model" style="width:125px">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.model}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{car.model}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
Simple example from official site of primefaces does not work.
the error is
javax.faces.view.facelets.TagException: /faces/default.xhtml #36,106
Event:rowEditCancel is not supported.
I also faced the same problem. But I found the solution that I was using version 3.1 of primefaces. And this version not supports rowEditCancel event.
In-Cell Editing data table is supported in primefaces 3.5 and primefaces 4.0 upgrade needed
Make sure in your bean, you have a method defined like this:
public void onCancel(RowEditEvent event) {
//...put your logic here
}