primefaces datable dont refresh new data - jsf

Hi I have a datatable which publishes the information very well, however, for this particular case something happens, in one column have a Boolean value that can only be true once the entire table of the database, so if I detect someone puts another record as true then update the above false and so the cycle ends. The update is done right in the database but when I want to refresh the datatable to also show the modification of another record, and does not work ...
this is my xhtml:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
template="template.xhtml">
<ui:define name="content">
<h:form id="someForm">
<p:growl id="msg" showDetail="true" life="3000" autoUpdate="true"/>
<p:panelGrid style="100%">
<p:row>
<p:column style="350px">title 1</p:column>
<p:column>
<p:inputText id="cveSituacion" value="#{someBean.cve}" maxlength="20" />
</p:column>
</p:row>
<p:row>
<p:column style="350px">title 2</p:column>
<p:column>
<p:inputText id="name" value="#{someBean.name}" />
</p:column>
</p:row>
<p:row>
<p:column colspan="2">
<p:spacer></p:spacer>
</p:column>
</p:row>
<p:row>
<p:column>
<p:commandButton action="#{someBean.searchAll}" value="Search All" ajax="true" update="someForm" immediate="true"/>
</p:column>
</p:row>
<p:row>
<p:column colspan="4">
<p:spacer></p:spacer>
</p:column>
</p:row>
<p:row>
<p:column colspan="4">
<p:dataTable id="resultSearch" var="someVariable" value="#{someBean.listOfData}" editable="true">
<p:ajax event="rowEdit" listener="#{someBean.onEdit}" update=":someForm:msg :someForm:resultSearch" />
<p:ajax event="rowEditCancel" listener="#{someBean.onCancel}" update=":someForm:msg" />
<p:column headerText="Clave Sit Ticket" sortBy="#{someVariable.cve}">
<h:outputText value="#{someVariable.cve}" />
</p:column>
<p:column headerText="Name" sortBy="#{someVariable.name}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{someVariable.name}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{someVariable.name}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Description" sortBy="#{someVariable.desc}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{someVariable.desc}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{someVariable.desc}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Sit" sortBy="#{someVariable.situation}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{someVariable.situation}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu id="cboBox" value="#{someVariable.situation}" style="width=200px">
<f:selectItems value="#{commonCboBean.listOfData}" var="cboBox" itemValue="#{cboBox.cve}" itemLabel="#{cboBox.desc}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Default" sortBy="#{someVariable.BSitDefault}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{someVariable.BSitDefault}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu id="cboVoF" value="#{someVariable.BSitDefault}" style="width=200px">
<f:selectItems value="#{fovBean.fov}" var="fov" itemValue="#{fov.bandera}" itemLabel="#{fov.descripcion}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Edit" style="width:50px;" >
<p:rowEditor />
</p:column>
</p:dataTable>
</p:column>
</p:row>
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>
this is my backing bean:
#SuppressWarnings("serial")
#ManagedBean
#ViewScoped
public class SomeBean implements Serializable{
private String cve;
private String name;
private List<SomeObject> listOfData;
private SomeObject newDefault;
private SomeObject sitDefault;
private RequestContext context = null;
... some methods ....
public void onEdit(RowEditEvent event) {
/*
IF I JUST UPDATE DATA LIKE THIS: updateData(SomeObject obj) THE DATATABLE
IS UPDATED CORRECTLY, BUT IF A LEAVE THE NEXT CODE, IT DOESNT
*/
newDefault = (SomeObject)event.getObject();
if( newDefault.getFlag.equals(Constants.V) ){
List<SomeObject> list = ejb.find(newDefault.getCve);
if( list == null || list.size() <= 0 ){
updateData( newDefault );
}else{
if( list.get(0).getCve().equals( newDefault.getCve() ) ){
updateData( newDefault );
}else{
SomeObject sitDefault = list.get(0);
if( ejb.update(sitDefault) > 0 ){
updateData(newDefault);
searchAll();
}else
FacesContext.getCurrentInstance().addMessage( null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"sometitle",
"some messsage")
);
}
}
}else{
updateData( newDefault );
}
}
public void updateNewDefault(){
sitDefault.setBSitDefault("F");
if( ejb.update(sitDefault) > 0 ){
updateData(newDefault);
searchAll();
}else
FacesContext.getCurrentInstance().addMessage( null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"error",
"some error has ocurred")
);
}
private void updateData(SomeObject obj){
int result = ejb.update( obj );
if( result > 0 ){
FacesContext.getCurrentInstance().addMessage( null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"OK",
"OK"
)
);
searchAll();
}else{
FacesContext.getCurrentInstance().addMessage( null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"ERROR",
"SOME ERROR")
);
}
}
public void searchAll(){
setListOfData( ejb.findAll() );
}
/******** getters & setters *************/
}
Please help!!! took a while stuck with it :(

Related

using <p:dataTable> cellEdit event is never fired in Primefaces

I am having issue in firing an ajax call on the cellEdit event of a Data Table. The table shows up just fine on the UI but nothing happens when I click any of the cell.
xhtml
<h:form>
<p:dataTable id="decisionTree" var="tree"
value="#{treeBean.content}" editable="true" editMode="cell"
styleClass="smallGrid">
<f:facet name="header">
Notes Decision Tree
</f:facet>
<p:ajax event="cellEdit" listener="#{treeBean.onCellEdit}"
immediate="true" update=":#{p:component('notesTextArea')}" />
<p:column headerText="Comment Type">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.commentType}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.commentType}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="MTCNs">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.mtcns}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.mtcns}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Call Type">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.callType}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.callType}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Phone">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.phone}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.phone}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Dispute Reason">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.disputeReason}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.disputeReason}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Placement Decision">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.placementDescision}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.placementDescision}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<!--
<p:column>
<p:rowEditor />
</p:column>
-->
</p:dataTable>
</h:form>
Here is the Bean.
#Component("treeBean")
#Scope(value = "view", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TreeBean {
private List<TreeDto> content;
private String result="";
public List<TreeDto> getContent() {
return content;
}
public void setContent(List<TreeDto> content) {
this.content = content;
}
#PostConstruct
public void init() {
content=new ArrayList<TreeDto>();
TreeDto dto1=new TreeDto();
dto1.setCommentType("First Attempt");
dto1.setMtcns("mtcn1");
dto1.setCallType("OBC");
dto1.setPhone("8975730838");
dto1.setDisputeReason("Fraud");
dto1.setPlacementDescision("Write Off");
content.add(dto1);
}
public void onCellEdit(CellEditEvent event) {
RequestContext.getCurrentInstance().showMessageInDialog(new FacesMessage(FacesMessage.SEVERITY_INFO, "Status","something clicked"));
}
}
My intention to capture the value of the cells clicked. But I am not able to get an event fired in the first place on cell edit. Please give me some suggestions on how to resolve this.
Try to add a widgetVar to your dataTable and then edit your cell with a contextMenu for your data table, which call onclick="PF('yourWidgetVar').showCellEditor();return false;"
Somenthing like this
<p:contextMenu for="decisionTree" widgetVar="cMenu">
<p:menuitem value="Edit Cell" icon="ui-icon-search" onclick="PF('yourWidgetVar').showCellEditor();return false;"/>
<p:menuitem value="Hide Menu" icon="ui-icon-close" onclick="PF('cMenu').hide()"/>
</p:contextMenu>
You can take a look to Primefaces documentation too:
https://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml

Filtered jsf outputtext

I would like to know how to filter so that only the patents of vehicles that are available appear.
<p:column headerText="Patent car">
<h:outputText value="#{v.fkcar.patent}"/>
</p:column>
In the database there is a table called State with 2 income
id_state 1 ---- name_state Available and id_state 2 ---- name_state Not available
What I need is that only the patents of the vehicles that have the State in Available are shown
The State table has its respective Bean
I hope you can help me: D
Entry code Rent
<h:head>
<title>Realizar Alquiler</title>
<h:outputStylesheet library="css" name="style.css" />
</h:head>
<h:body>
<h2>Realizar Alquiler</h2>
<h:form id="fv">
<h:panelGrid columns="1" styleClass="panelGridCenter">
<h:outputText value="Precio"/>
<p:inputText value="#{alquilerBean.precio}" required="true"/>
<p:outputLabel for="datetime" value="Fecha" />
<p:calendar id="datetime" value="#{alquilerBean.fecha3}" pattern="MM/dd/yyyy HH:mm:ss" required="true"/>
Cliente
<p:selectOneMenu value="#{alquilerBean.cliente.idCliente}">
<f:selectItems value="#{clienteBean.cliente}" var="e" itemLabel="#{e.rut}" itemValue="#{e.idCliente}"/>
</p:selectOneMenu>
Patente
<p:selectOneMenu value="#{alquilerBean.vehiculo.idVehiculo}">
<f:selectItems value="#{vehiculoBean.vehiculos}" var="o" itemLabel="#{o.patente}" itemValue="#{o.idVehiculo}"/>
</p:selectOneMenu>
¿Entregado?
<p:selectOneMenu value="#{alquilerBean.entregado.idEntregado}">
<f:selectItems value="#{entregadoBean.entregado}" var="o" itemLabel="#{o.nombreEntregado}" itemValue="#{o.idEntregado}"/>
</p:selectOneMenu>
<br/>
<h:outputText value=""/>
<p:commandButton actionListener="#{alquilerBean.crear()}" value="Agregar Alquiler" update="fv"/>
</h:panelGrid>
<br/>
<p:dataTable value="#{alquilerBean.alquiler}" var="v" emptyMessage="No hay registros" >
<p:column headerText="Fecha">
<h:outputText value="#{v.fecha}"/>
</p:column>
<p:column headerText="Precio">
<h:outputText value="#{v.precio}"/>
</p:column>
<p:column headerText="Entregado">
<h:outputText value="#{v.fkEntregado.nombreEntregado}"/>
</p:column>
<p:column headerText="Rut Cliente">
<h:outputText value="#{v.fkCliente.rut}"/>
</p:column>
<p:column headerText="Patente Auto"
<h:outputText value="#{v.fkVehiculo.patente}"/>
</p:column>
</p:column>
<f:facet name="footer">
Se han alquilado #{alquilerBean.alquiler.size()} Vehiculos.
</f:facet>
</p:dataTable>
</h:form>
<br/>
<h:form>
<h:commandLink action="index">
<h:graphicImage value="resources/images/atras2.png" />
</h:commandLink>
</h:form>
</h:body> </html>
Here is the state bean code.
private int id_entregado;
private String nombre_entregado;
public EntregadoBean() {
}
public String crear(){
Entregado e = new Entregado();
e.setNombreEntregado(nombre_entregado);
entregadoFacade.create(e);
return "#";
}
public List<Entregado> getEntregado(){
return entregadoFacade.findAll();
}
public EntregadoFacadeLocal getEntregadoFacade() {
return entregadoFacade;
}
public void setEntregadoFacade(EntregadoFacadeLocal entregadoFacade) {
this.entregadoFacade = entregadoFacade;
}
public int getId_entregado() {
return id_entregado;
}
public void setId_entregado(int id_entregado) {
this.id_entregado = id_entregado;
}
public String getNombre_entregado() {
return nombre_entregado;
}
public void setNombre_entregado(String nombre_entregado) {
this.nombre_entregado = nombre_entregado;
}
}

primefaces rowEdit Event to update table only if the event is a success

I am calling a rowEdit event and I want to refresh the table only when the event performs the db update successfully. Below are my code.
XHTML
<p:dataTable id="xobjTable" var="xobj" value="#{xListView.xListResponseObject}"
scrollRows="20" scrollable="true" liveScroll="true" scrollHeight="600"
rowKey="#{xobj.X}"
selection="#{xListView.selectedObject}" selectionMode="single"
style="margin-top: 20px; margin-bottom:20px" editable="true">
<f:facet name="header">
Search Results
<p:spacer width="20"/>
<h:commandLink id="csv">
<p:graphicImage value="csv.png" width="24"/>
<p:dataExporter type="csv" target="xobj" fileName="xListSearch" />
<p:tooltip id="toolTipFade" for="csv" value="Click on CSV to download entire table as a csv file. " />
</h:commandLink>
</f:facet>
<p:ajax event="rowEdit" listener="#{xListView.onRowEdit}" update=":xListform:xobjTable" />
<p:column headerText="X" style="width:60px;text-align: center">
<h:outputText value="#{xobj.X}" />
</p:column>
<p:column headerText="Y" style="width:60px;text-align: center">
<h:outputText value="#{xobj.Y}" />
</p:column>
<p:column headerText="Modified Date" style="width:160px;text-align: center">
<h:outputText value="#{xobj.modifiedDate}" />
</p:column>
<p:column headerText="Active" style="width:160px;text-align: center">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{xobj.active}"/>
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{xobj.active}">
<f:selectItems value="#{xListView.activeList}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Location" style="width:160px;text-align: center">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{xobj.location}"/>
</f:facet>
<f:facet name="input">
<p:inputText id="location" value="#{xobj.location}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="State" style="width:160px;text-align: center">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{xobj.state}"/>
</f:facet>
<f:facet name="input">
<p:inputText id="state" value="#{xobj.state}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="x Country Code" style="width:160px;text-align: center">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{xobj.xCountryCode}"/>
</f:facet>
<f:facet name="input">
<p:inputText id="xCountryCode" value="#{xobj.xCountryCode}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="xrarier" style="width:160px;text-align: center">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{xobj.carrier}"/>
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{xobj.carrier}">
<f:selectItems value="#{xListView.xarrierList}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:32px" headerText="Edit" rendered="#{allTabs.isShow(xListView.title)}">
<p:rowEditor />
</p:column>
<p:column style="width:160px;text-align: center" rendered="#{allTabs.isShow(xListView.title)}" headerText="Delete">
<p:commandButton id="deleteDid" actionListener="#{xListView.deletexy(xobj)}"
icon="ui-icon ui-icon-trash red" title="Delete" update="#form">
<p:confirm header="Confirmation" message="#{xListView.finalDeleteMessage}" icon="ui-icon-alert" />
</p:commandButton>
</p:column>
</p:dataTable>
Here is the event method
public void onRowEdit(RowEditEvent event) {
xListResponseObject editObject = (xListResponseObject) event.getObject();
logger.debug("Coming to edit the row.");
String editXYStatus = xListDAO.editxy(editObject);
if (editxyStatus.equals(SUCCESS_RESPONSE)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "INFO", "X/Y edit successful!");
FacesContext.getCurrentInstance().addMessage(null, msg);
} else {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "INFO", "X/Y edit failed :" + editxyStatus);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
I would like the update to happen only when the rowEdit event updates the db.
Can I have a boolean set in my method and access data in the update in the p:ajax ?
Add to end of your SUCCESS_RESPONSE
RequestContext.getCurrentInstance().update("form:npaobjTable");
Or whatever your table real id is.
I was having the same problem.
Below URL will solve your problem.
http://forum.primefaces.org/viewtopic.php?f=3&t=38412&p=153402#p153402
actual response :
public void onRowEdit(RowEditEvent event) {
NpaListResponseObject editObject = (NpaListResponseObject) event.getObject();
logger.debug("Coming to edit the row.");
String editNpaNxxStatus = npaListDAO.editNpaNxx(editObject);
if (editNpaNxxStatus.equals(SUCCESS_RESPONSE)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "INFO", "NPA/NXX edit successful!");
FacesContext.getCurrentInstance().addMessage(null, msg);
} else {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "INFO", "NPA/NXX edit failed :" + editNpaNxxStatus);
FacesContext.getCurrentInstance().addMessage(null, msg);
FacesContext.getCurrentInstance().validationFailed();
}
}
added FacesContext.getCurrentInstance().validationFailed(); at last in failure scenario.

JSF Primefaces datatable in cell editMode not sending the edited value to the managed bean

I have two primefaces dataTables. The first show the instances of a MainEntity.
The second one show a list of Words (just strings) associated with the selected MainEntity on the first datatable.
The printscreen illustrates what I mean.
My problem is, when I edit a string on the Word List, my listener method won't receive the new value. In fact, when I call the event.getNewValue() method, I get old value.
What am I missing?
I'm using JavaServer Faces 2.2, Primefaces 5.0 and Spring Framework 4.0.3.
Thanks in advance for any help.
The code for the xhtml, the managed bean and the MainEntity are as follows:
mainEntity.xhtml:
<h:body>
<h:form id="mainEntityForm">
<p:panelGrid columns="1" fullPage="true" id="dashboard">
<f:facet name="header">
<h2>MainEntity Table</h2>
</f:facet>
<p:panel>
<f:facet name="header">
MainEntity List
<p:commandButton value="New MainEntity"
actionListener="#{mainEntityController.createMainEntityDialog}"
styleClass="header-button">
<p:ajax event="dialogReturn" update=":mainEntityForm:mainEntityTable" />
</p:commandButton>
</f:facet>
<p:dataTable id="mainEntityTable" var="mainEntity" value="#{mainEntityController.mainEntities}"
editable="true" editMode="cell" widgetVar="cellMainEntity"
selectionMode="single" selection="#{mainEntityController.selectedMainEntity}"
rowKey="#{mainEntity.id}" tableStyle="width:auto">
<p:ajax event="rowSelect" update=":mainEntityForm:stringGrid :mainEntityForm:entityAGrid :mainEntityForm:entityBGrid" />
<p:ajax event="cellEdit" listener="#{mainEntityController.onEditMainEntity}" update=":mainEntityForm:mainEntityTable" />
<p:column headerText="ID">
<h:outputText value="#{mainEntity.id}" />
</p:column>
<p:column headerText="Name">
<p:cellEditor>
<f:facet name="output">
<h:outputText id="nameOutput" value="#{mainEntity.name}" />
</f:facet>
<f:facet name="input">
<h:inputText id="atyInput" value="#{mainEntity.qty}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Commands">
<p:commandButton title="Remove MainEntity" icon="ui-icon-trash"
actionListener="#{mainEntityController.deleteMainEntity(mainEntity)}"
update=":mainEntityForm:mainEntityTable" />
</p:column>
</p:dataTable>
</p:panel>
<p:panelGrid fullPage="true" id="mainEntityDetail">
<f:facet name="header">
<p:row>
<p:column colspan="4">
<h2>MainEntity Detail</h2>
</p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<p:panel>
<f:facet name="header">
Word List
<p:commandButton value="New Word"
actionListener="#{mainEntityController.addNewWord()}"
styleClass="header-button"
update=":mainEntityForm:wordGrid">
</p:commandButton>
</f:facet>
<p:dataTable id="wordGrid" var="word"
value="#{mainEntityController.selectedMainEntity.wordList}"
tableStyle="width:auto" editable="true" editMode="cell" widgetVar="cellWord">
<p:ajax event="cellEdit" listener="#{mainEntityController.onEditWord}" />
<p:column headerText="Word">
<p:cellEditor>
<f:facet name="output">
<h:outputText id="wordOutput" value="#{word}" />
</f:facet>
<f:facet name="input">
<h:inputText id="wordInput" value="#{word}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Commands">
<p:commandButton title="Remove Word"
icon="ui-icon-trash"
actionListener="#{mainEntityController.removeWord(word)}"
update=":mainEntityForm:wordGrid" />
</p:column>
</p:dataTable>
</p:panel>
</p:column>
</p:row>
</p:panelGrid>
</p:panelGrid>
</h:form>
</h:body>
MainEntityController.java (managed bean):
#ManagedBean
#SessionScoped
public class MainEntityController {
//...
private MainEntity selectedMainEntity;
public List<MainEntity> mainEntities;
//...
public MainEntity getSelectedMainEntity(){
return selectedMainEntity;
}
public void setSelectedMainEntity(MainEntity mainEntity){
this.selectedMainEntity = mainEntity;
}
//...
public void onEditWord(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
if(newValue != null && !newValue.equals(oldValue)) {
// THE PROBLEM IS HERE
// I NEVER ACTUALLY REACH THIS CODE
// newValue is always equal to oldValue!
FacesContext context = FacesContext.getCurrentInstance();
String word = context.getApplication().evaluateExpressionGet(context, "#{word}", String.class);
System.out.println(newValue);
System.out.println(word);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Word Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
MainEntity.java:
#Entity
public class MainEntity {
#Id
private String id;
#ElementCollection(fetch=FetchType.EAGER)
#CollectionTable(
name="MainEntity_Word",
joinColumns = #JoinColumn(name = "id", referencedColumnName="id")
)
#Column(name = "word")
private Set<String> wordList;
//...
public void addWord(String word) {
this.wordList.add(word);
}
public void removeWord(String word) {
this.wordList.remove(word);
}
public Set<String> getWordList() {
return wordList;
}
public void setWordList(Set<String> wordList) {
this.wordList = wordList;
}
}
Change
<h:inputText id="wordInput" value="#{word}" />
to
<p:inputText id="wordInput" value="#{word}" />
or
<h:inputText id="wordInput" value="#{word}" >
<p:ajax event="change" process="#this"/>
</h:inputText>
You need to ajaxify the input component, and at last update the outputText component.
Edited:
change this column :
<p:column headerText="Word" >
<p:cellEditor>
<f:facet name="output">
<h:outputText id="wordOutput" value="#{word}" />
</f:facet>
<f:facet name="input">
<h:inputText id="wordInput" value="#{word}" />
</f:facet>
</p:cellEditor>
</p:column>
to
<p:column headerText="Word" >
<p:cellEditor>
<f:facet name="output">
<h:outputText id="wordOutput" value="#{word}" />
</f:facet>
<f:facet name="input">
<p:inputText id="wordInput" value="#{word}" >
<p:ajax event="change" update="wordOutput" />
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
Edit 2 :
From Chapter 24 Introduction to the Java Persistence API
If an entity instance be passed by value as a detached object, such as
through a session bean’s remote business interface, the class must
implement the Serializable interface.
I finally got it working.
I suspected of the String immutable nature and what I did was to encapsulate the 'word' in a class like the following:
public class Word {
private String word;
Word (String word) {
this.setWord(word);
}
public String getWord() {
return this.word;
}
public void setWord(String word) {
this.word = word;
}
}
After that, I also had to create a set inside the managed bean (controller) to hold the set of words, like 'private Set managedWordList;'.
I also changed the getter for the list, inside the managed bean, to convert from the String set to a Word set like:
public Set<Word> getWordList() {
if (this.selectedMainEntity != null){
this.wordList = new HashSet<Word>();
for (String word : this.selectedMainEntity.getWordList()) {
this.wordList.add(new Word(word));
}
}
return this.wordList;
}
Finally, I changed the dataTable to reference the new set. From:
<p:dataTable ... value="#{mainEntityController.selectedMainEntity.wordList}"... />
To:
<p:dataTable ... value="#{mainEntityController.wordList}"... />
And only then I got it to work as I expected.

primefaces datatable :rowedit not working

I'm trying to edit row data in primfaces dataTable but not working
I try the same code of primefaces showcases in below link
http://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml
my jsf page
<h:form id="form">
<p:dataTable id="depTable" value="#{departmentBean.departments}"
var="dep" editable="true">
<f:facet name="header">
All departments
</f:facet>
<p:ajax event="rowEdit" listener="#{departmentBean.onRowEdit}"
update=":form:depTable" />
<p:ajax event="rowEditCancel"
listener="#{departmentBean.onRowCancel}" update=":form:depTable" />
<p:column headerText="Id">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{dep.id}" />
</f:facet>
<f:facet name="input">
<p:inputText id="modelInput" value="#{dep.id}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Name">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{dep.name}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{dep.name}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
and method in bean
public void onRowEdit(RowEditEvent event) {
System.out.println(((Department) event.getObject()).getName());
FacesMessage msg = new FacesMessage("Car Edited",
((Department) event.getObject()).getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Edit Cancelled",
((Department) event.getObject()).getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
I'm using primfaces 4.0
In my case it was the problem that my backing bean was #SessionScoped (in PF example it is #ViewScoped) so I need to modify dataTabe value:
<p:dataTable id="settingsTbl" value="#{settingsBean.settingsList}" var="item" editable="true">
.....
public List<Settings> getSettingsList() {
if(settingsList==null){
settingsList = settingsFacade.findAll();
}
return settingsList;
}
I had encountered this issue and my workaround was to put the dataTable within a Form.
https://plnkr.co/edit/nYxHfY8PsGH586Om?open=lib%2Fscript.js
<div ng-controller="MainCtrl">
<div ui-grid="gridOptions" ui-grid-edit ui-grid-row-edit ui-grid-cellNav class="grid">
</div>
<button id='flushDirty' ng-click='flushDirty()'>Flush Dirty</button>
<button id='addRow' ng-click='addRow()'>Add Row</button>
<br/>
<p>Dirty rows: {{dirtyRows.length}}</p>
<div ng-repeat="row in dirtyRows">
{{row.entity.id}}, {{row.entity.gender}}
</div>
</div>

Resources