PrimeFaces <p:cellEditor> not changing value of variable - primefaces

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);
}
}
}

Related

In Cell editing does not update value on focus change

Cell value is not captured when mouse focus changes
I have a simple datatable that shows the name and income. The cell values are editable. When I update the cell value with a different value and hit "Update" (basically change mouse focus) the new value is not captured. The update method
prints the old value. However, when I press the "tab" key I can see the updated values printed. I am not sure what I am missing
Here is my backing bean:
#PostConstruct
public void init() {
NVPair nvp = new NVPair("xx", 10000);
dataList.add(nvp);
nvp = new NVPair("yy", 20000);
dataList.add(nvp);
}
public void update() {
for (NVPair item : dataList) {
System.out.println("Name: " + item.getName() + " Value: " +
item.getValue());
}
}
Here is the view:
<h:form>
<p:dataTable id="testTable" value="#{testBean.dataList}" var="item"
editable="true" editMode="cell">
<p:ajax event="cellEdit" listener="#{testBean.onCellEdit}"
immediate="true" update="testTable" />
<p:column headerText="Name">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{item.name}" />
</f:facet>
<f:facet name="input">
<h:outputText value="#{item.name}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Income">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{item.value}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{item.value}" />
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
<p:commandButton action="#{testBean.update}" value="Update" />
</h:form>
When the focus changes, I am expecting that the edited value is captured just like when the tab key is pressed.

Primefaces DataTable Selection's Attribute is Null if not rendered in Dialog

I'm not sure if this is a limitation or a coding error on my part. I have a Primefaces DataTable that allows the user to select it (single select radio) and then click on a CommandButton to edit the data in a Dialog.
In the HTML code where I display the selected object, I omitted the ID, Lat, and Long attributes because a human user shouldn't be touching these attributes. The Dialog is showing the selected attributes correctly, however, when I tried to find the existing record using the selected object's ID for update, it thrown a no result error. When I checked the selected object's ID attribute, it has a value of NULL.
I tinker around with the HTML code and found that in order to have the any of the attributes copied to the selected object, the attributes must be shown in the XHTML. When I include the attribute as with Render = False, the attribute will have a value of NULL. Any suggestions on hiding attributes that human users shouldn't modify beside using Disable tag attribute?
Class Property{
Int ID;
String Address;
Long Lat;
Long Long;
String Desc;
}
public void modifyCurrentProject() {
HashMap<String, Object> params = new HashMap();
params.put("id", currentProject.getId());
//fails to find Project by ID as currentProject.getID == null
Project temp = (Project) dbu.findByNamedQuery("project.findID", params);
//Omit statements to overwrite attached object's values before writing back to database
dbu.modifyEntity(); //dbu is another object to control database access
currentProject = null;
}
<h:body>
<p:growl id="projectMsg" showDetail="true"/>
<h:form id="form">
<p:dataTable id="projectTable" var="project" value="#{projectBacking.builder.projects}" selection="#{projectBacking.currentProject}" rowKey="#{project.id}">
<f:facet name="header">My Project List
<p:commandButton value="Add" type="button" onclick="PF('projectAdd').show()"/>
<p:commandButton value="Edit" update=":editForm" oncomplete="PF('projectEdit').show()"/>
</f:facet>
<p:column selectionMode="single"/>
<p:column headerText="ID">
<h:outputText value="#{project.id}"/>
<p:column headerText="Description">
<h:outputText value="#{project.property.description}"/>
</p:column>
<p:column headerText="Street">
<h:outputText value="#{project.street}"/>
</p:column>
<p:column headerText="City">
<h:outputText value="#{project.city}"/>
</p:column>
<p:column headerText="Province">
<h:outputText value="#{project.state}"/>
</p:column>
<p:column headerText="Latitude">
<h:outputText value="#{project.latitude}"/>
</p:column>
<p:column headerText="Longitude">
<h:outputText value="#{project.longitude}"/>
</p:column>
<p:column headerText="Status">
<h:outputText value="#{project.status}"/>
</p:column>
</p:dataTable>
</h:form>
<!-- Omitted the Add Project Dialog box -->
<p:dialog header="Edit Project" modal="true" height="400" width="80%" widgetVar="projectEdit">
<h:form id="editForm">
<p:panel id="editProject" header="Edit Project" toggleable="true" closable="false">
<!--<h:inputText id="id" value="#{projectBacking.currentProject.id}"/>-->
<h:inputText id="country" value="#{projectBacking.currentProject.country}"/>
<h:inputText id="lat" rendered="true" value="#{projectBacking.currentProject.latitude}"/>
<h:inputText id="long" rendered="true" value="#{projectBacking.currentProject.longitude}"/>
<h:outputLabel for="desc" value="Description" />
<h:inputText id="desc" value="#{projectBacking.currentProject.description}"/>
<h:outputLabel for="street" value="Street" />
<h:inputText id="street" value="#{projectBacking.currentProject.street}"/>
<h:message for="street"/>
<h:outputLabel for="city" value="City:" />
<h:inputText id="city" required="false" value="#{projectBacking.currentProject.city}"/>
<h:message for="city" />
<h:outputLabel for="state" value="Province" />
<h:inputText id="state" required="true" value="#{projectBacking.currentProject.state}"/>
<h:commandButton value="Save Project" type="submit" action="#{projectBacking.modifyCurrentProject()}">
</h:commandButton>
</p:panel>
</h:form>
</p:dialog>
</h:body>
After placing some more breakpoints in the code, I discovered that upon saving the changes in the Dialog, it's creating a new backing bean. If the is set to Render=false, Disabled=true, or ReadOnly=true then new backing bean's object's value will not be set and retain the initial value as specified in the object's constructor.
Basically, the cause is incorrect scope. I was using Request scope when the problem occurred. However, changing to Session scope resolved it.

Save checkbox state After ReLoadThe Page in JSF Primefaces

Im using a "DataTable - Selection",(http://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml) in primefaces 5 to check the selected rows, but when I reload the page, the checkboxes are unchecked, How can i keep the state of the checkboxes in session?
**
*********DataTable****************
**
<p:dataTable var="var" value="#{vistaBean.listaFichero}" rowKey="#{var.nombre}" paginator="true" rows="10"
selection="#{vistaBean.selectFichero}">
<f:facet name="header">
Votar ProductBox
</f:facet>
<p:column headerText="Votar" selectionMode="multiple"/>
<p:column headerText="Nombre del Fichero">
<h:outputText value="#{var.nombre}" />
</p:column>
<p:column headerText="Ver/Descargar">
<h:commandLink id="pdf" action="#{vistaBean.downLoad}">
<f:setPropertyActionListener target="#{vistaBean.ruta}" value="#{var.ruta}" />
<h:graphicImage library="images" name="pdf.png" />
</h:commandLink>
</p:column>
</p:dataTable>
<p:commandButton value="Guardar Votos" action="#{vistaBean.addVoto()}" update="msgs"/>
<p:commandButton value="Ver Votos" update="display" oncomplete="PF('dlg').show()" icon="ui-icon-check" inmediate="true"/>
<p:dialog header="Product Box Seleccionados" modal="true" showEffect="clip" widgetVar="dlg" resizable="false">
<p:outputPanel id="display">
<p:dataList value="#{vistaBean.selectFichero}" var="v">
#{v.nombre}
</p:dataList>
</p:outputPanel>
</p:dialog>
I solve the issue by installing a newer version of Primefaces, 5.2, i had the 5.1 and it had that bug.
This is how you set a session variable:
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
session.setAttribute("foo", "bar");
This is how you can get session variables:
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements())
{
String attr = (String)e.nextElement();
System.err.println(" attr = "+ attr);
Object value = session.getValue(attr);
System.err.println(" value = "+ value);
}
Your only task is to save the state of your checkboxes into the session and then load it.

Primefaces treetable implementation

I am trying to implement primefaces treetable with checkbox and multiselection, like the one in primefaces showcase, everything works as expected but when I try to get the list of selected nodes, I only get the top nodes. So for example, if the user selects a child node of "Movies", it is not present in the TreeNode[] selectedNodes. Why is it behaving like this? What am I missing?
My codes are
In .xthml file
<p:treeTable value="#{backingBean.theTree}" var="tree" id="tree"
selection="#{backingBean.selectedNodes}" selectionMode="checkbox">
<p:column style="width:150px">
<f:facet name="header">
Name
</f:facet>
<span title="#{tree.objectCode}"><h:outputText value="#{tree.objectName}" /></span>
</p:column>
<p:column style="width:150px">
<f:facet name="header">
Object Description
</f:facet>
<h:outputText value="#{tree.objectDescription}" />
</p:column>
</p:treeTable>
Backingbean:
this.theTree = new DefaultTreeNode("root", null);
TreeNode eachParent = new DefaultTreeNode(new Document(...some properties...), root);
And I have a recursive loop which basically creates more nodes in the same way.

Can't leave editing mode on prime faces roweditor

I have this code that shows a table with a prime faces datatable and a roweditor column. When I click on pencil icon, editable mode is enabled but after that, when I click on check or cancel icon nothing happens, editable mode remains active. I've search a lot about it but I couldn't find response. Listener on backing bean is not called.
This is my code of the view:
<p:dataTable var="vac" value="#{vacDocBean.obl}" id="documentacion" editable="true">
<f:facet name="header">
Table
</f:facet>
<p:ajax event="rowEdit" update="#this" listener="#{vacDocBean.onEditRow(_record)}" />
<p:column headerText="Edad">#{vacuna.inm.e}</p:column>
<p:column headerText="Inm">#{vac.inm.n}</p:column>
<p:column headerText="Fecha aplicacion">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{vac.fechaAplicacion}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
</h:outputText>
</f:facet>
<f:facet name="input">
<p:calendar value="#{vac.fechaAplicacion}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Marca">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{vac.marca}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{vac.marca}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Lote">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{vac.lote}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{vac.lote}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Editar" styleClass="ui-editable-column-options">
<!-- <p:commandLink ajax="true">-->
<p:rowEditor />
<!-- </p:commandLink > -->
</p:column>
<f:facet name="footer">
VacunaciĆ³n documentada
</f:facet>
</p:dataTable>
And the backing bean:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.calendario;
#Named(value="vacDocBean")
#SessionScoped
public class VacDocBean implements Serializable {
private List<Vac> obl;
public VacDocBean() {
obl = new ArrayList<Vac>();
setVac();
Collections.sort(obl);
}
private void setVac()
{
this.obl = new ArrayList(Helper.getObl());
}
public List<Vac> getObl() {
return obl;
}
public void setObligatorias(List<Vac> obl) {
this.obl = obl;
}
public void onEditRow(RowEditEvent e){
System.out.println("Hello");
}
public void onCancel(RowEditEvent e){
System.out.println("Hello 2");
}
}
Thanks!!
Just surround your h:datatable by a h:form tag, since the editable datatable needs it.
To make your listener method work, try that, whithout changing method's signature:
<p:ajax event="rowEdit" listener="#{vacDocBean.onEditRow}" />