Can't edit value in datatable using OnRowEdit in PrimeFaces - jsf

I've followed PrimeFaces showcases to use row editing datatable but i can't see why it's not updating data.
here's my datatable:
<p:dataTable id="tab" var="art" value="#{myMB.allArticles}" editable="true" style="margin-bottom:20px">
<f:facet name="header">
</f:facet>
<p:ajax event="rowEdit" listener="#{myMB.onRowEdit}" update=":form:msgs" />
<p:ajax event="rowEditCancel" listener="#{myMB.onRowCancel}" update=":form:msgs" />
<p:column headerText="Name ">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{art.name}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput" value="#{art.name}" style="width:100%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Budget">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{art.budget}" /></f:facet>
<f:facet name="input"><p:inputText value="#{art.budget}" style="width:100%" label="Budget"/></f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>
and in the bean:
public void onRowEdit(RowEditEvent event ) {
Article f = (Article) event.getObject();
formationFacade.update(f);
FacesMessage msg = new FacesMessage("Article Edited", ((Article) event.getObject()).getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
I ve seen this : link but it doesn't work for me I get the old value after editing and I have no change in may database .. can someone help me please?

There's one thing that is strange/wrong, that may or may not be the cause of your issues, but I would recommend fixing. From this update=":form:msgs" I assume that you have an enclosing tags like <h:form id="form"><p:growl id="msgs". On the other hand you repeat the id="form" inside <p:dataTable id="form". Repeating the id is surely wrong, but I'm not sure if its the cause of your issue

first, you have to provide the EditMode parameter with the value of
CellEdit
or RowEdit as your case.
second, you can do edit like this without event if you want a simple way to edit data table is
modifyYourModel(Model x){
// call your function to modify
}
and in the data table, you can do that
<p:dataTable id="tab" var="art" value="#{myMB.allArticles}" editable="true" EditMode="rowEdit" style="margin-bottom:20px">
<f:facet name="header">
</f:facet>
<p:ajax event="rowEdit" listener="#{myMB.modifyYourModel(art)}" update=":form:msgs" />
<p:ajax event="rowEditCancel" listener="#{myMB.onRowCancel}" update=":form:msgs" />

Use your IDs instead of :form, :msgs.
<p:ajax event="rowEdit" listener="#{myMB.onRowEdit}" update="messages tab" />
<p:ajax event="rowEditCancel" listener="#{myMB.onRowCancel}" update="messages tab" />
Also you have to update list "allArticles" by edited object.

Related

How to update Cells dynamically in Primefaces Data Table

I've a scenario Like i'll use cell Edit in Data Table.
I've two cells
I've 1st Cell is Editable
2nd Cell Value needs to be Displayed by doing some arithmetic operations on 1st Cell input (Eg: 2nd cell value =(1st cell value)*.14
My Code is Like
<p:dataTable id="cars2" var="car" value="#{dtEditView.cars2}" editable="true" editMode="cell" widgetVar="cellCars">.
<p:ajax event="cellEdit" listener="#{dtEditView.onCellEdit}" update=":form:msgs" />
<p:column headerText="Cell 1">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.id}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput" value="#{car.id}" style="width:96%"/>
<p:ajax event="keyup" listener="#{car.yearupdate}" update="2:3"></p:ajax></f:facet>
</p:cellEditor>
</p:column>
<p:column id="2" headerText="Cell 2">
<f:facet name="output"><h:outputText id="3" value="#{car.year}" /></f:facet>
</p:column>
</p:dataTable>
I want to Know the Java Code for Listener method code to update the 2nd cell value
In xhtml we need to call the listener (p:ajax tag).
in bean update the other column data in bulk ( iterate over all search records and update corressponding valuefor all recs ) ..
xhtml code like
<p:dataTable id="listDetails" value="${managedBean.searchList}" var="row">
<p:ajax event="rowEdit" listener="#{managedBean.onRowEdit}"
update=":managedBean:messages" />
<p:ajax event="rowEditCancel" listener="#{managedBean.onRowCancel}" update=":managedBean:messages" />
<p:column priority="17"
headerText="col 1"
styleClass="wrap text-right" style="width: 140px;">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.val1}" />
</f:facet>
<f:facet name="input">
<p:inputText id="modelInput" value="#{row.val1}"
<p:ajax event="blur" listener="#{managed.valueUpdate}"
update="tempValue></p:ajax>
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<p:column priority="2" headerText="Col 2" exportable="true">
<p:cellEditor>
<f:facet name="output">
<h:outputText id="value" value="#{row.value}" />
</f:facet>
<f:facet name="input">
<h:outputText id="tempValue" value="#{row.tempValue}" />
</f:facet>
</p:cellEditor>
</p:column>
in Bean
public void valueUpdate(){
for (BO bo : searchList) {
if(bo.val1!=null)
{
bo.value = bo.val1+50; // here u can do calc and assihn to the
bo.tempValue=bo.value;
}
}
}

Primefaces editable datatable inserting itself on row edit?

I am trying to put together a datatable to display some company information using Primefaces and java hosted with Tomcat7. Unfortunately due to sensitive information, I cannot give specifics about the data I'm working with. My code has been modified only to change variable names to more generic ones, and to remove business logic that doesn't touch the page.
I've got just about everything working, except when I try to add a new row to my datatable, it inserts the entire table into itself at the row that I was editing. This also happens when just cancelling the edit.
This is very confusing, considering I'm only inserting an item into a List object, which is in turn being fed into the table by Primefaces.
Here is the contents of my xhtml:
<h:form id="form">
<p:growl id="msgs" showDetail="true"/>
<p:dataTable var="object" value="#{page.objectList}" id="tableName"
scrollable="false"
liveResize="true"
resizableColumns="true"
editable="true"
sortMode="multiple"
reflow="true"
paginator="true"
rows="20"
paginatorTemplate="{CurrentPageReport} {PreviousPageLink} {PageLinks} {NextPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,20,50,100">
<f:facet name="header">
Table Title
</f:facet>
<p:ajax event="rowEdit" listener="#{page.onRowEdit}" update=":form:msgs" />
<p:ajax event="rowEditCancel" listener="#{page.onRowCancel}" update=":form:msgs" />
<p:column headerText="col1">
<p:cellEditor>
<f:facet name="output"><h:outputText id="col1Output" value="#{object.col1item}" /></f:facet>
<f:facet name="input">
<p:inputText id="col1Input" value="#{object.col1item}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="col2">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{object.col2item}" /></f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{object.col2item}" style="width:100%">
<f:selectItems value="#{object.categories}" var="col2item" itemLabel="#{col2item}" itemValue="#{col2item}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="col3">
<p:cellEditor>
<f:facet name="output"><h:outputText id="col3Output" value="#{object.col3item}" /></f:facet>
<f:facet name="input">
<p:inputText id="col3Input" value="#{object.col3item}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="col4">
<p:cellEditor>
<f:facet name="output"><h:outputText id="col4Output" value="$#{object.col4item}" /></f:facet>
<f:facet name="input">
<p:inputText id="col4Input" converterId="com.gvea.utilities.business.Money" value="#{object.col4item}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="col5">
<p:cellEditor>
<f:facet name="output"><h:outputText id="col5Output" value="#{object.col5item}" /></f:facet>
<f:facet name="input">
<p:inputText id="col5Input" value="#{object.col5item}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:32px">
<p:rowEditor/>
</p:column>
</p:dataTable>
</h:form>
and the onRowEdit and onRowCancel:
public void onRowEdit(RowEditEvent event) {
/*-- modify object --*/
...
objectList.add(0, newObject);
/*-- Give success or failure message --*/
}
public void onRowCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Edit Cancelled", "");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
The objectList.add(...) is the only place where I do anything that could potentially chance the page... and I've verified that "newObject" is in fact an instance of the right class. Yet still I end up getting this:
Thanks for any help!
As it turns out, I wasn't updating the datatable on the lines:
<p:ajax event="rowEdit" listener="#{page.onRowEdit}" update=":form:msgs" />
<p:ajax event="rowEditCancel" listener="#{page.onRowCancel}" update=":form:msgs" />
simply adding form to the update line fixed my issue. Like so:
<p:ajax event="rowEdit" listener="#{page.onRowEdit}" update=":form:msgs form" />
<p:ajax event="rowEditCancel" listener="#{page.onRowCancel}" update=":form:msgs form" />

Primefaces DataTable cellEdit required inputText not evaluated

can someone explain why following minimal example won't evaluate the required element? Or is it the "wrong" way and there is a better one I don't know.
I'm aware that my question is similiar to this question: Primefaces cellEditor required inputText. For me it seems that the solution is more of a workaround than a valid way to cope with this problem. So yeah any ideas how to deal with this?
Code:
<p:dataTable id="ruleTableID" var="rule" value="#{rC.rules}" widgetVar="rowsTable"
rows="20" editable="#{rC.editable}"
editMode="cell" paginator="true"
paginatorPosition="bottom" paginatorTemplate="{Save}"
emptyMessage="#{t['rule.empty']}">
<p:ajax event="cellEdit" update=":newRules:messages, :newRules:" />
<p:column headerText="#{t['policy.registerNumber']}">
<p:cellEditor>
<f:facet name="input">
<p:inputText styleClass="ruleInputText" value="#{rule.registerNr}" required="true" maxlength="4">
<f:validateLength minimum="4" />
<f:validateRegex pattern="([A-Z\d]{4})" />
<p:clientValidator/>
</p:inputText>
</f:facet>
<f:facet name="output">
<h:outputText value="#{rule.registerNr}" />
</f:facet>
</p:cellEditor>
</p:column>
<f:facet name="{Save}">
<p:commandButton id="saveButton" value="#{t['button.save']}" action="#{rC.saveRules}" update=":newRules" rendered="#{rC.isAllValueSet}" />
</f:facet>
</p:dataTable>
If you are using client side validation you have to add the attributes ajax="false" and validateClient="true" to your commandButton
You can see a example here:
http://www.primefaces.org/showcase/ui/csv/event.xhtml

Editing rows in p:dataTable

I try to update a datatable row with PrimeFaces 3.1.1 but it makes all values null.
public void onRowEdit(RowEditEvent event) {
Client us= (Client) event.getObject();
System.out.println("event edit"+us);
clientService.editClient( us );
FacesMessage msg = new FacesMessage("Client modifié", ((Client) event.getObject()).getName_customer());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
This is my page xhtml:
<h:form>
<p:dataTable var="client" value="#{clientBean.clients}" widgetVar="clientTable" emptyMessage="No customers found with given criteria" filteredValue="#{clientBean.filteredClients}" paginator="true" rows="10" rowsPerPageTemplate="5,10,15" editable="true" id="testContainer">
<p:ajax event="rowEdit" update="#this" listener="#{clientBean.onRowEdit(event)}" />
<f:facet name="header"><p:outputPanel><h:outputText value="Recherche d'un client:" /> </p:outputPanel>
</f:facet>
<p:column filterBy="#{client.user.username}" headerText="Collaborateur" filterMatchMode="contains">
<h:outputText value="#{client.user.username}" />
</p:column>
<p:column filterBy="#{client.name_customer}" headerText="Nom" filterMatchMode="contains">
<f:facet name="output">
<h:link outcome="CustomerDetails?faces-redirect=true&includeViewParams=true" value="#{client.name_customer}" >
<f:param name="idCustomer" value="#{client.costumer_id}"></f:param>
<f:param name="nameCustomer" value="#{client.name_customer}"></f:param>
</h:link> </f:facet>
<f:facet name="input"><p:inputText value="#{client.name_customer}"/></f:facet>
</p:cellEditor>
</p:column>
<p:column filterBy="#{client.statut}" headerText="Statut" filterMatchMode="contains">
the problem is that your object Client us is null , try to display a property from your object us if is null so that will confirm that you don't get your object from your page correctly . So i suggest you to replace that :
<p:ajax event="rowEdit" update="#this" listener="#{clientBean.onRowEdit}" />
with that
<p:ajax event="rowEdit" update="#this" listener="#{clientBean.onRowEdit(event)}" />
Note that i just added the event property to onRowEdit.

Cell edit in primefaces is not updating the value

I have a datatable in my primefaces application . The code for the frontend has
<!-- Start of customer datatable -->
<p:dataTable var="customer" value="#{customerBean.customers}" paginator="true" selection="#{customerBean.selectedCustomer}"
selectionMode="single" onRowSelectUpdate=":custList" onRowSelectComplete="custTab.show()" id="custList" widgetVar="custList" update=":custList">
<f:facet name="header">
List of Customers
<p:outputPanel>
<p:commandButton value="+" type="button" onclick="addCustDlg.show()"/>
</p:outputPanel>
</f:facet>
<p:column sortBy="#{customer.id}" filterBy="#{customer.id}" update=":custList">
<f:facet name="header">
<h:outputText value="ID"/>
</f:facet>
<h:outputText value="#{customer.id}"/>
</p:column>
<p:column sortBy="#{customer.name}" filterBy="#{customer.name}" headerText="NAME" filterMatchMode="contains" update=":custList">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{customer.name}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{customer.name}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column sortBy="#{customer.description}" filterBy="#{customer.description}" headerText="DESCRIPTION">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{customer.description}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{customer.description}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column sortBy="#{customer.signupDate}" filterBy="#{customer.signupDate}" headerText="SIGN UP DATE">
<f:facet name="output">
<h:outputText value="#{customer.signupDate}"/>
</f:facet>
</p:column>
<p:column sortBy="#{customer.validUntil}" filterBy="#{customer.validUntil}" headerText="EXPIRY DATE">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{customer.validUntil}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{customer.validUntil}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column sortBy="#{customer.status}" filterBy="#{customer.status}" headerText="STATUS">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{customer.status}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{customer.status}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="CREATION DATE" sortBy="#{customer.creationDate}" filterBy="#{customer.creationDate}">
<f:facet name="output">
<h:outputText value="#{customer.creationDate}"/>
</f:facet>
</p:column>
<p:column headerText="LAST UPDATE DATE" sortBy="#{customer.lastUpdateDate}" filterBy="#{customer.lastUpdateDate}">
<f:facet name="output">
<h:outputText value="#{customer.lastUpdateDate}"/>
</f:facet>
</p:column>
<p:column headerText="Options">
<p:rowEditor/>
</p:column>
</p:dataTable>
<!-- End of dataTable (customer datatable) -->
And the function for handling the rowEvent is specified in the bean as
public void custRowEdit(RowEditEvent event){
Customer cust = (Customer) event.getObject();
EntityManagerHelper.beginTransaction();
custDao.update(cust);
EntityManagerHelper.commit();
}
However , on an update event , when I am editing the cell in the table , I do not get the new updated value of the attribute .
Like in the image below , when I edit the status of the entry with ID 1 from 11 to 4 , in the function custRowEdit , when I try to get the customer object , I still get the status of the customer as 11 and not 4 .
Can anyone help me with understanding why the value of the cell is not being set ?
from Where You are invoking custRowEdit(RowEditEvent event) method. I have not any related thing in your code.
In order to make your listener invoke add below attribute in your datatable declaration.
rowEditListener="#{customerBean.listenerInBackingBean}"
<p:dataTable var="customer" value="#{customerBean.customers}" paginator="true" selection="#{customerBean.selectedCustomer}"
selectionMode="single" onRowSelectUpdate=":custList" onRowSelectComplete="custTab.show()" id="custList" widgetVar="custList" update=":custList">
<f:facet name="header"
rowEditListener="#{customerBean.cutRowEvent}"
>
Check the implementation of customerBean.customers. I reloaded the content from the database every time the method got called. Wrong. This should happen in the constructor instead. Now everything works fine. Thought it was a JavaScript error ...
Thanks this helped me.
May I add that instead of loading the list from a query in the constructor, if one has a #SessionScoped managed bean one can instead use a reset() method to reset lists to null and then lazily populate the lists from the query. The reset can then be called on page load using an f:event:
<f:view>
<f:metadata>
<f:event type="preRenderView" listener="#{sessionScopedBean.reset}"/>
</f:metadata>
</f:view>
I encountered a similar situation that was resolved by removing the update="" tag from the dataTable. The table's default behavior is to update the row, which evidently, in my case, was not occurring.
You are missin the p:ajax event to trigger the method, function or whatever you wanna do after the cell editing
it's something like
<p:ajax event="cellEdit" listener="#{mBean.onCellEdit}" update="elementX" />

Resources