Custom scope of a ManagedBean in JSF2.0 with PrimeFaces 3.5 - jsf-2

Following is my requirement-
Here I have a p:panelGrid which can add & delete the row of table. The grid contains some p:inputText and various other PrimeFaces components along with a p:fileUpload component in each row. The component p:fileUpload is set with mode="advanced" auto="true" attributes, which automatically uploads the file and hide itself after completing the successful upload.
The whole p:panelGrid is in #ViewScoped, hence working fine. I kept p:fileUpload component in #RequestScoped since for each upload request it has to upload the file but after adding new row, the previous state is not persisted anymore. so the p:fileUpload is starting visible in previous rows also. That's what I don't want. Do I need to write any custom scope for it?
Below is the view-|
<h:form>
<p:panel id="agentForm" header="#{msg.AGENTS_INFORMATION}"
style="overflow:auto; margin-bottom: 2px">
<div align="center" style="margin-top: 20px; margin-bottom: 2px">
<ui:repeat value="#{agent.scenarioList}" var="c">
<p:panelGrid>
<p:row>
<p:column>
<p:inputText id="ipaddress" value="#{c.machineIpAddress}"
style="width:90%">
<p:watermark for="ipaddress" value="#{msg.MACHINE_IP_ADDRESS}" />
</p:inputText>
</p:column>
<p:column>
<p:inputText id="username" value="#{c.machineUsername}"
style="width:90%">
<p:watermark for="username" value="#{msg.MACHINE_USERNAME}" />
</p:inputText>
</p:column>
<p:column>
<p:password id="passwd" value="#{c.machinePassword}">
<p:watermark for="passwd" value="#{msg.MACHINE_PASSWORD}" />
</p:password>
</p:column>
<p:column id="fileUpload">
<p:fileUpload rendered="#{!fileUploadController.hidden}"
label="Upload Script" style="font-size: 100% !important;"
showButtons="false"
fileUploadListener="#{fileUploadController.upload}"
mode="advanced" auto="true" sizeLimit="100000"
allowTypes="/(\.|\/)(py|txt)$/"
update="fileUpload, outPanel, :message" />
<p:outputPanel id="outPanel">
<!-- Below outputLabel will be linked to uploaded file, so that User can see the file -->
<p:outputLabel style="cursor: pointer" value="View uploded Script"
label="View Script" rendered="#{fileUploadController.hidden}" />
</p:outputPanel>
</p:column>
<p:column>
<p:inputText id="testname" value="#{c.testName}"
style="width:90%">
<p:watermark for="testname" value="#{msg.TEST_NAME}" />
</p:inputText>
</p:column>
<p:column>
<p:spinner id="threads" value="#{c.threads}" min="1" max="500"
size="8">
<p:tooltip for="threads" value="#{msg.TEST_NAME}"
showEffect="slide" hideEffect="slide" />
</p:spinner>
</p:column>
<p:column>
<p:selectBooleanCheckbox id="chkSelected" value="#{c.selected}">
<p:tooltip for="chkSelected" value="#{msg.CHECKBOX}"
showEffect="slide" hideEffect="slide" />
</p:selectBooleanCheckbox>
</p:column>
</p:row>
</p:panelGrid>
</ui:repeat>
<p:toolbar style="margin-top: 10px;">
<p:toolbarGroup align="right">
<p:commandButton value="#{msg.ADD_IT}"
update=":message, agentForm"
actionListener="#{agent.addComponent()}" />
<p:commandButton value="#{msg.DELETE_IT}"
update=":message, agentForm"
actionListener="#{agent.deleteComponent()}" />
</p:toolbarGroup>
</p:toolbar>
</div>
</p:panel>
</h:form>
My managed bean which is in #ViewScoped look like this-
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.ravij.performance.model.Scenario;
#ManagedBean(name = "agent")
#ViewScoped
public class AgentInfo implements Serializable {
private static final long serialVersionUID = 1L;
List<Scenario> scenarioList;
#PostConstruct
public void initBean() {
this.scenarioList = new ArrayList<Scenario>();
this.scenarioList.add(new Scenario());
}
public void addComponent() {
if (this.scenarioList != null) {
this.scenarioList.add(new Scenario());
} else {
this.initBean();
}
}
public void deleteComponent() {
List<Scenario> itemsToDelete = new ArrayList<Scenario>();
if (this.scenarioList != null) {
for (Scenario b : this.scenarioList) {
if (b.isSelected()) {
itemsToDelete.add(b);
}
}
this.scenarioList.removeAll(itemsToDelete);
}
}
public List<Scenario> getScenarioList() {
return scenarioList;
}
public void setScenarioList(List<Scenario> scenarioList) {
this.scenarioList = scenarioList;
}
}
The Scenario object contains all the information of a row. Below is the code-
package org.ravij.performance.model;
import java.io.Serializable;
public class Scenario implements Serializable {
private String machineIpAddress;
private String machineUsername;
private String machinePassword;
private String uploadedFilePath;
private String testName;
private int threads = 1;
private boolean selected = false;
//Below are the getters and setter w.r.t all the above variables
//I am not putting it, to make the code short
}
The managed bean FileUploadController is in #RequestScoped

You should simply keep your hidden attribute with the other values in your #ViewScoped bean. Your current code has a single hidden attribute shared with all your <p:fileUpload components which is probably not what you want.
The behavior looks good because you are only updating the current fileUpload but according to your code all the others <p:fileUpload component are supposed to be hidden.
You should also put your <h:form into your <ui:repeat so that you can know the current line which is concerned by the file being uploaded by putting something like an index (which you can get from the <ui:repeat using varStatus attribute) or any other identifier to match the current line in an hidden input.
From #{fileUploadController.upload} the easiest manner to get the hidden parameter is to get the response from FacesContext as explained here : How to get parametrs to BackingBean from jsf page in <ui:repeat>
UPDATE
It was a bit harder than expected, the problem is that <p:fileUpload will send everything in the enclosing form (didn't try to play with process attribute) and thus it will be hard to know what row is concerned by the file upload.
Also I didn't knew that you couldn't put <h:form in your <ui:repeat but the behavior of your delete button is blocking as it expects to get everything in one form.
I made a working POC using dialog to put the fileupload outside, here is how :
The trivial Scenario.java :
public class Scenario implements Serializable {
private String machineIpAddress;
private String machineUsername;
private String machinePassword;
private String uploadedFilePath;
private String testName;
private int threads = 1;
private boolean selected = false;
private boolean hidden = false; // This is new
// + Getters/Setters
}
A few changes in the AgentInfo.java :
#ManagedBean(name = "agent")
#ViewScoped
public class AgentInfo implements Serializable {
private List<Scenario> scenarioList;
private Scenario currentScenario; // This is new
// I removed the #PostConstruct which I rarely use
public void addComponent() {
if (this.scenarioList != null) {
this.scenarioList.add(new Scenario());
}
}
public void deleteComponent() {
if (this.scenarioList == null) {
return;
}
List<Scenario> itemsToDelete = new ArrayList<Scenario>();
for (Scenario scenario : this.scenarioList) {
if (scenario.isSelected()) {
itemsToDelete.add(scenario);
}
}
this.scenarioList.removeAll(itemsToDelete);
}
// This is new, it must be called before opening the upload dialog
// in order to keep a pointer on the current scenario you are working on
public void prepareUpload(Scenario scenario) {
this.currentScenario = scenario;
}
// I put the upload method here
public void upload(FileUploadEvent event) {
// Do what you need to do here
this.currentScenario.setHidden(true);
RequestContext.getCurrentInstance().execute("uploadDialogWidget.hide()");
}
public List<Scenario> getScenarioList() {
if (this.scenarioList == null) {
this.scenarioList = new ArrayList<Scenario>();
this.scenarioList.add(new Scenario());
}
return scenarioList;
}
public void setScenarioList(List<Scenario> scenarioList) {
this.scenarioList = scenarioList;
}
public Scenario getCurrentScenario() {
return currentScenario;
}
public void setCurrentScenario(Scenario currentScenario) {
this.currentScenario = currentScenario;
}
}
The most changes are in the view, I put a <h:commandButton to open the dialog in the form. I also added the dialog, and added the redisplay attribute for your password fields (which is necessary to have if you want to keep the value after form submission).
Note that I removed references to a component with message id which was not gave, don't forget to reintroduce it.
the .xhtml :
<h:form id="agentForm">
<p:panel header="#{msg.AGENTS_INFORMATION}"
style="overflow:auto; margin-bottom: 2px">
<div align="center" style="margin-top: 20px; margin-bottom: 2px">
<ui:repeat value="#{agent.scenarioList}" var="c">
<p:panelGrid>
<p:row>
<p:column>
<p:inputText id="ipaddress" value="#{c.machineIpAddress}"
style="width:90%">
<p:watermark for="ipaddress" value="#{msg.MACHINE_IP_ADDRESS}" />
</p:inputText>
</p:column>
<p:column>
<p:inputText id="username" value="#{c.machineUsername}"
style="width:90%">
<p:watermark for="username" value="#{msg.MACHINE_USERNAME}" />
</p:inputText>
</p:column>
<p:column>
<p:password id="passwd" value="#{c.machinePassword}" redisplay="true">
<p:watermark for="passwd" value="#{msg.MACHINE_PASSWORD}" />
</p:password>
</p:column>
<p:column id="fileUpload">
<p:commandButton icon="ui-icon-arrowthick-1-n" value="Upload"
actionListener="#{agent.prepareUpload(c)}"
update=":uploadDialog"
oncomplete="uploadDialogWidget.show()"
rendered="#{!c.hidden}" />
<p:outputPanel id="outPanel">
<!-- Below outputLabel will be linked to uploaded file, so that User can see the file -->
<p:outputLabel style="cursor: pointer" value="View uploded Script"
rendered="#{c.hidden}" />
</p:outputPanel>
</p:column>
<p:column>
<p:inputText id="testname" value="#{c.testName}"
style="width:90%">
<p:watermark for="testname" value="#{msg.TEST_NAME}" />
</p:inputText>
</p:column>
<p:column>
<p:spinner id="threads" value="#{c.threads}" min="1" max="500"
size="8">
<p:tooltip for="threads" value="#{msg.TEST_NAME}"
showEffect="slide" hideEffect="slide" />
</p:spinner>
</p:column>
<p:column>
<p:selectBooleanCheckbox id="chkSelected" value="#{c.selected}">
<p:tooltip for="chkSelected" value="#{msg.CHECKBOX}"
showEffect="slide" hideEffect="slide" />
</p:selectBooleanCheckbox>
</p:column>
</p:row>
</p:panelGrid>
</ui:repeat>
<p:toolbar style="margin-top: 10px;">
<p:toolbarGroup align="right">
<p:commandButton value="#{msg.ADD_IT}" update="agentForm"
actionListener="#{agent.addComponent()}" />
<p:commandButton value="#{msg.DELETE_IT}" update="agentForm"
actionListener="#{agent.deleteComponent()}" />
</p:toolbarGroup>
</p:toolbar>
</div>
</p:panel>
</h:form>
<p:dialog id="uploadDialog" widgetVar="uploadDialogWidget" header="File upload">
<h:form rendered="#{!empty agent.currentScenario}">
<p:fileUpload
label="Upload Script" style="font-size: 100% !important;"
showButtons="false"
fileUploadListener="#{agent.upload}"
mode="advanced" auto="true" sizeLimit="100000"
allowTypes="/(\.|\/)(py|txt)$/"
update=":agentForm">
</p:fileUpload>
<p:commandButton value="Cancel" onclick="uploadDialogWidget.hide();" onstart="return false;" />
</h:form>
</p:dialog>
You should consider to move from <p:panelGrid to a <p:dataTable which has a built in mechanism to work with row selection.

Related

page can't get value from Bean

I have a table p:dataTable. When I select an item, it's refresh another p:dataTable with associed values.
It's works fine. But when I select an item in the second table, the page (xhtml) don't get the value from the bean.
In debug mode I see that the value is updated on bean, but acessing #{bean.value} is always null.
Where is the codes:
<h:form id="form">
<h:panelGrid columns="2">
<!-- FRIST TABLE -->
<p:dataTable id="alunos" var="aluno" emptyMessage="Nenhum aluno cadastrado" value="#{alunos.usuarios}" selectionMode="single" selection="#{alunos.usuarioSelecionado}" rowKey="#{aluno.id}">
<f:facet name="header">
Alunos
</f:facet>
<p:ajax event="rowSelect" listener="#{alunos.onRowSelect}" update=":form:disci" />
<p:column headerText="Nome" width="60%">
<h:outputText value="#{aluno.nome}" />
</p:column>
<p:column headerText="Curso">
<h:outputText value="Default" />
</p:column>
</p:dataTable>
<!-- FUNCTIONS FOR FRIST TABLE THAT ARE WORKING -->
<p:column rowspan="4">
<p:commandButton value="Adicionar" update="alunos" icon="ui-icon-plus" actionListener="#{alunos.addUser()}"/>
<p:commandButton value="Editar" icon="ui-icon-pencil" actionListener="#{alunos.editarUser()}"/>
<p:commandButton value="Remover" icon="ui-icon-close" actionListener="#{alunos.apagaUser()}"/>
</p:column>
</h:panelGrid>
<p:outputLabel for="disci" value="Disciplinas que o aluno cursa" />
<!-- SECOND TABLE - Updated when an iten is selected in frist table (working)-->
<p:dataTable id="disci" var="disciplina" emptyMessage="Selecione um aluno" value="#{alunos.disciplinas}" selectionMode="single" selection="#{alunos.disciplinaSelecionada}" rowKey="#{disciplina.id}">
<p:ajax event="rowSelect" listener="#{alunos.onRowSelectDisciplina}" oncomplete="PF('dDialog').show()"/>
<p:column headerText="Nome" width="80%">
<h:outputText value="#{disciplina.nome}" />
</p:column>
</p:dataTable>
<!-- Dialog to show informations - ALWAYS NULL -->
<p:dialog header="Info" widgetVar="dDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
<p:outputPanel id="detail" style="text-align:center;">
<p:panelGrid columns="2" columnClasses="label,value">
<h:outputText value="Id:" />
<h:outputText value="#{alunos.disciplinaSelecionada.id}" />
<h:outputText value="Nome" />
<h:outputText value="#{alunos.disciplinaSelecionada.nome}" />
</p:panelGrid>
</p:outputPanel>
</p:dialog>
</h:form>
And the Bean:
#ManagedBean(name = "alunos")
#ViewScoped
public class Alunos extends NovoUsuario {
private Disciplina disciplinaSelecionada;
public Alunos() {
super(TiposUsuario.ALUNO);
}
//Method called when an iten is selected in SECOND TABLE
//In debug, the Object Disciplina is correct, as selected in table.
public void onRowSelectDisciplina(SelectEvent event) {
this.disciplinaSelecionada = (Disciplina) event.getObject();
}
//getters and setters
}
extended class:
public class NovoUsuario implements Serializable{
private List<Usuario> usuarios;
protected Usuario usuarioSelecionado;
TiposUsuario tipo;
private String nome;
private String senha;
protected List<Disciplina> disciplinas;
//Constructor
public NovoUsuario(TiposUsuario tipo) {
this.tipo = tipo;
}
//Initialize FRIST TABLE
#PostConstruct
public void init() {
UsuarioCRUD usuarioCRUD = new UsuarioCRUD();
this.usuarios = usuarioCRUD.listarTipoUsuario(tipo);
}
//Method used in FRIST TABLE
public void onRowSelect(SelectEvent event) {
disciplinas = getDisciplinaUsuario();
}
//Method that get data to SECOND table when a row is selectd in frist
public List<Disciplina> getDisciplinaUsuario() {
DisciplinaCRUD dcrud = new DisciplinaCRUD();
if (usuarioSelecionado != null) {
return dcrud.listaDisciplinasUsuario(usuarioSelecionado);
}
return null;
}
public void salvaUsuario() {
/* code to create*/
}
public void editarUser(){
/* code to update*/
}
public void apagaUser(){
/* code to delete*/
}
//GETTERS AND SETTERS
}
image: http://i.stack.imgur.com/Qbs0T.png
repository: https://bitbucket.org/VagnerGon/novo-academico
I'd say you just need to update the dialogs content from the p:ajax in the second table - just as you update the second datatable, when you select in the first.
So add id="dialog" to the dialog, and make the p:ajax in the second table
<p:ajax event="rowSelect" listener="#{alunos.onRowSelectDisciplina}" update=":form:dialog" oncomplete="PF('dDialog').show()" />
Or omit the new id and just do update=":form:detail".

Primefaces datatable hasn't been refreshed after applying the filter

I have some code is similar to the following showcase:
http://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml
The filter is applied correctly in the backing bean, although the datatable hasn't been refreshed with the new filtered rows. It remains as it is before applying the filter. Is there something missing or wrong in this showcase?
<h:form>
<p:dataTable var="serviceEntity" value="#{serviceSearchMB.allServices}" widgetVar="serviceSearchTable"
emptyMessage="No services found with given criteria" filteredValue="#{serviceSearchMB.filteredServices}">
<f:facet name="header">
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter" onkeyup="PF('serviceSearchTable').filter();"
style="width:150px" placeholder="Enter keyword" />
</f:facet>
<p:column filterBy="#{serviceEntity.serviceName}" headerText="Service Name" filterMatchMode="contains" >
<h:outputText value="#{serviceEntity.serviceName}" />
</p:column>
...
Backing Bean:
#ManagedBean(name="serviceSearchMB")
#RequestScoped
public class ServiceSearchManagedBean implements Serializable {
private List<ServiceSearchEntity> filteredServices;
public List<ServiceSearchEntity> getFilteredServices() {
return filteredServices;
}
public void setFilteredServices(List<ServiceSearchEntity> filteredServices) {
this.filteredServices = filteredServices; // Already set the filtered list correctly.
}
public List<ServiceSearchEntity> getAllServices() {
//already returns all services.
}
...

JSF 2.0 outputText rendered selectonemenu

I m using JSF-2 and I want to show an Outputtext when changes h:selectOneMenu value to 'A' , but it doesnt work :
Here is the view:
<p:column>
<p:selectOneMenu id="type"
value="#{Controller.typeR}" style="width:100px;">
<f:selectItem itemLabel="--Selectionner--" itemValue="-1" />
<f:selectItem itemLabel="A" itemValue="1" />
<f:selectItem itemLabel="B" itemValue="2" />
<f:selectItem itemLabel="C" itemValue="3" />
<p:ajax update="test"
listener="#{Controller.handleTypeChange}" />
</p:selectOneMenu>
</p:column>
<p:column>
<h:outputText id ="test" value="A OK :" rendered="#{Controller.typeAOk}" />
</p:column>
the managed bean
#SuppressWarnings("serial")
#ManagedBean(name = "Controller")
#ViewScoped
public class NoIe{
public void handleTypeChange(){
if (typeR.equals("1")) {
setTypeAOk(true);
System.out.print(typeAOk);
}}
//Getter and Setter
Any help will be greatly appreciated!
As #{Controller.typeAOk} seems to be false your outputText won't be part of the resulting html page and thus, it won't be available for updating.
In cases like this, you will need to wrap your outputText inside another component and then update that component which is always rendered. Here is an example:
<p:column>
<p:outputPanel id="test">
<h:outputText value="A OK :" rendered="#{Controller.typeAOk}" />
</p:outputPanel>
</p:column>
Once again: Only rendered components can be updated.
Here is the full code I used to test the solution (note that you can use a panelGroup too):
<h:form>
<p:dataTable value="#{viewMBean.list}" var="l">
<p:column>
<p:selectOneMenu id="type" value="#{viewMBean.id}" style="width:100px;">
<f:selectItem itemLabel="--Selectionner--" itemValue="-1" />
<f:selectItem itemLabel="A" itemValue="1" />
<f:selectItem itemLabel="B" itemValue="2" />
<f:selectItem itemLabel="C" itemValue="3" />
<p:ajax update="test" />
</p:selectOneMenu>
</p:column>
<p:column>
<h:panelGroup id ="test">
<h:outputText value="A OK :" rendered="#{viewMBean.id eq 1}" />
</h:panelGroup>
</p:column>
</p:dataTable>
</h:form>
The ManagedBean
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class ViewMBean implements Serializable {
private Integer id;
private List<SimpleBean> list;
#PostConstruct
public void setup() {
list = new ArrayList<SimpleBean>();
list.add(new SimpleBean(11, "A"));
list.add(new SimpleBean(22, "B"));
list.add(new SimpleBean(33, "C"));
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<SimpleBean> getList() {
return list;
}
}

PrimeFaces datatable not refreshing correctly after an add operation

I have a datatable which has add edit delete and filter operations.
All the operations are working fine on the database but the datatable is not updating correctly after an add operation.
It is updated correctly after the 1st add operation.
when I add 2nd row the datatable is updated but the row which was added first is replaced with the row which was added 2nd but in the db they are updated correctly.
similarly when I add a 3rd row the 1st row is replaced with the 3rd row.
Software stack used in the CRUD dataTable application are :- PrimeFace 3.4.2,JSF 2.1,NetBeans 7.2.1,Java 1.6.0_37 GlassFish 3.1.x,MySql 5.x
This is my code.
**xhtml page**
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Prime Faces Data table</title>
</h:head>
<h:body>
<h:form id="form1">
<h:messages id="messages1"/>
<br/>
<p:commandButton id="Addbtn" value="Add" oncomplete="AddDialog.show()"
update=":addform"/>
<br/>
<p:dataTable id="dtusers" value="#{userdataController.retrieveData()}"
var="item"
rows="8" paginator="true" emptyMessage="No Records Found"
paginatorAlwaysVisible="false">
<p:column style="width:150px" filterMatchMode="contains"
filterBy ="#{item.id}">
<f:facet name="header"> Id </f:facet>
<h:outputText value="#{item.id}"/>
</p:column>
<p:column style="width:150px" filterMatchMode="contains"
filterBy="#{item.name}">
<f:facet name="header"> Name </f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column style="width:150px">
<p:commandButton id="editButton" update=":editform"
oncomplete="EditDialog.show()" value="Edit">
<f:setPropertyActionListener value="#{item}"
target="#{userdataController.selectedUser}"/>
</p:commandButton>
</p:column>
<p:column style="width:150px">
<p:commandButton id="deleteButton" update=":deleteform"
oncomplete="UserDialog.show()" value="Delete">
<f:setPropertyActionListener value="#{item}"
target="#{userdataController.selectedUser}"/>
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<p:dialog header="Add User Data" resizable="false" id="AddDialog"
widgetVar="AddDialog" modal="true" showEffect="fade" hideEffect="fade"
position="center" >
<h:form id="addform">
<p:outputPanel id="AddDisplay">
<p:panelGrid columns="2" >
<f:facet name="header">
Add Userdata
</f:facet>
<h:outputLabel for="Id" value="Enter Id:"/>
<p:inputText id="Id" value="#{userdataController.selected.id}" />
<h:outputLabel for="Name" value="Enter Name:"/>
<p:inputText id="Name"
value="#{userdataController.selected.name}"/>
<p:commandButton actionListener="#{userdataController.createdata}"
update=":form1:dtusers :form1:messages1" value="Save"
oncomplete="AddDialog.hide()" style="margin:0"/>
</p:panelGrid>
</p:outputPanel>
</h:form>
</p:dialog>
//similarly dialogs for delete and edit
</h:body>
</html>
**ManagedBean**
#ManagedBean(name = "userdataController")
#SessionScoped
public class UserdataController implements Serializable {
private Userdata current;
private DataModel items = null;
#EJB
private PrimeFacesDb.session.UserdataFacade ejbFacade;
private int id;
private String name;
private Userdata SelectedUser;
public UserdataController() {
}
//getters setters for id,name and SelectedUser
private UserdataFacade getFacade() {
return ejbFacade;
}
public List retrieveData() {
List result = getFacade().findAll();
return result;
}
public void create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage("Userdata created successfully");
} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Persistence Error occured");
}
}
}

How to clear all input fields in p:dataTable?

I am using JSF 2.0 with PrimeFaces. I have a <p:dataTable>. I have a <p:inputText> in a column. I can edit and save it. I have also a reset button, but it is not working.
<h:form id="f">
<f:facet name="head">Enteri Karbon Hesaplaması</f:facet>
<p:dataTable value="#{orderBean.orderList}" var="o" id="bir">
<p:column>
<f:facet name="header">Hayvan Adi</f:facet>
<h:outputText value="#{o.hayvanadi}"/>
</p:column>
<p:column>
<f:facet name="header">Karbon Salinimi Değeri</f:facet>
<h:outputText value="#{o.karbonsalinimi}"/>
</p:column>
<p:column>
<f:facet name="header">Adet</f:facet>
<p:inputText id="spinner" maxlength="12" value="#{o.adet}"/>
</p:column>
</p:dataTable>
<p:commandButton value="Kaydet" action="#{orderBean.saveAction()}" update="bir"/>
<p:commandButton value="Temizle" update="bir" process="#this" actionListener="#{orderBean.reset}"/>
</h:form>
Here is the relevant part of my backing bean:
#ManagedBean
#SessionScoped
public class OrderBean {
private static final ArrayList<Order> orderList =
new ArrayList<Order>(Arrays.asList(
new Order("Süt İneği", 99 , 0),
new Order("Diğer İnekler", 58, 0),
new Order("Koyun",5, 0),
new Order("Keçi",5, 0),
new Order("At ",18, 0),
new Order("Eşek ",10, 0)
)
);
public String saveAction() {
for (Order order : orderList){
order.setEditable(false);
}
return null;
}
public String editAction(Order order) {
order.setEditable(true);
return null;
}
public void reset() {
RequestContext.getCurrentInstance().reset("form:f");
}
// ...
}
Change the type of your command button to reset ie:
<p:commandButton type="reset" value="Temizle" update="bir" process="#this" actionListener="#{orderBean.reset}"/>
Also, since you are basically trying to make the datatable empty again you can just set orderList to an empty list in the reset function.
You can reset a form using <p:commandButton type="reset" or <p:commandButton update="#form". The latter is also used in p:inplace.
Edit: The reason your button isn't working is because the id referred in its update attribute isn't correct. It should be :formId:dataTableId. The #form is easier though.

Resources