Value not setting in Backing Bean after putting render attribute - jsf-2

In the below code, the value of Price is not setting in backing bean after I set the render attribute. Prior to that It was setting into the backing Bean.
h:dataTable id="Table" value="#{bean.orderList}" var="attr" binding="#{bean.orderTable}">
<h:column>
<f:facet name="header">
<h:outputText value="Item"/>
</f:facet>
<h:outputText value="#{attr.orderItem.itemId}" rendered="#{attr.orderItem.itemId != null}" />
<h:selectOneMenu value="#{attr.orderItem.itemId}" rendered="#{attr.orderItem.itemId == null}">
<f:selectItems value="#{bean.itemList}" var="attrList" itemValue="#{attrList.itemId}" itemLabel="#{attrList.itemName}" />
</h:selectOneMenu>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Price" />
</f:facet>
<h:outputText value="#{attr.price}" rendered="#{attr.orderItem.itemId != null}" />
<h:inputText value="#{attr.price}" rendered="#{attr.orderItem.itemId == null}" />
</h:column>
Please find below the Code for bean
public String addAction() {
Order ord = new Order();
ord.setItems(new Items());
orderList.add(ord);
count=count+1;
return null;
}
#PostConstruct
public void populateForm(){
Order ord = new Order();
ord.setItems(new Items());
orderList= dataDao.getOrders();
ord.add(srcTrustScore);
}

Related

Primefaces command button does not update datatable

I have primefaces command button multiple update problem. Command button is inside a form and datatable is not.
I have tried:
update=":datalist1,form1"
update=":datalist1 form1"
update="datalist1 form1"
update=":#{p:component('datalist1')},form1"
form1 updates every time but still dattable does not update
Version info:
primefaces: 5.3
jsf: org.glassfish 2.3.0-m04
spring: 4.2.3
and I use cdi for controller beans
Page:
<!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://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<body>
<h:form id="form1" >
<h:panelGrid columns="3">
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{personelController.personel.adi}" required="true" requiredMessage="Name can not be null" />
<p:message id="nameMsg" for="name" />
<h:outputLabel value="Surname: " for="surname" />
<p:inputText id="surname" value="#{personelController.personel.soyadi}" required="true" requiredMessage="Surname can not be null" />
<p:message id="surnameMsg" for="surname" />
<h:outputLabel value="Age: " for="age" />
<p:inputText id="age" value="#{personelController.personel.yasi}" required="true" requiredMessage="Age can not be null" />
<p:message id="ageMsg" for="age" />
<h:outputLabel value="Job: " for="job" />
<p:inputText id="job" value="#{personelController.personel.meslek}" required="true" requiredMessage="Job can not be null" />
<p:message id="jobMsg" for="job" />
<h:outputLabel value="City" for="city" />
<p:inputText id="city" value="#{personelController.personel.sehir}" required="true" requiredMessage="City can not be null" />
<p:message id="cityMsg" for="city" />
<p:commandButton id="add" value="Add" actionListener="#{personelController.createPersonel()}" update=":datalist1,form1" />
</h:panelGrid>
</h:form>
<p:dataTable id="datalist1" var="person" value="#{personelController.personels}" resizableColumns="true">
<f:facet name="header">List of Personel</f:facet>
<p:column headerText="ID">
<h:outputText value="#{person.id}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{person.adi}" />
</p:column>
<p:column headerText="Surname">
<h:outputText value="#{person.soyadi}" />
</p:column>
<p:column headerText="Age">
<h:outputText value="#{person.yasi}" />
</p:column>
<p:column headerText="Job">
<h:outputText value="#{person.meslek}" />
</p:column>
<p:column headerText="City">
<h:outputText value="#{person.sehir}" />
</p:column>
</p:dataTable>
</body>
</html>
Class:
#Named
#SpringViewScoped
public class PersonelController implements Serializable{
private static final long serialVersionUID = 1L;
#Inject
private PersonelService personelService;
private List<Personel> personels;
private List<Personel> selectedPersonel;
private Personel personel;
#PostConstruct
public void init(){
personels=personelService.findAll();
personel=new Personel();
}
public PersonelService getPersonelService() {
return personelService;
}
public void setPersonelService(PersonelService personelService) {
this.personelService = personelService;
}
public List<Personel> getPersonels() {
return personels;
}
public void setPersonels(List<Personel> personels) {
this.personels = personels;
}
public List<Personel> getSelectedPersonel() {
return selectedPersonel;
}
public void setSelectedPersonel(List<Personel> selectedPersonel) {
this.selectedPersonel = selectedPersonel;
}
public Personel getPersonel() {
return personel;
}
public void setPersonel(Personel personel) {
this.personel = personel;
}
public void createPersonel(){
System.out.println("Personel adding..");
personelService.create(personel);
personel=new Personel();
System.out.println("Personel added");
}
}
I have researched about primefaces command button update but all advices did not worked for me. Thank you in advance
It seems to me that the newly created personel it never added to the list. So you must for example say
personels.add(personel);
or
personels=personelService.findAll();
in createPersonel(), or set personels to null and do a lazy load in getPersonels().

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.
}
...

primefaces p:rowEdit cascading selectoneMenu

I have an Office datatable that displays department name and role name. department and role are objects. when I select rowedit, when there is a change in the department, the roles should be reloaded. I am not sure how to pass the selected department and rerender the roles.
<p:column headerText="Department">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{office.department.name}" /></f:facet>
<f:facet name="input">
<p:selectOneMenu value="#{office.department}" converter="#{departmentConverter}" style="width:96%" >
<f:selectItems value="#{departments}" var="dept" itemLabel="#{dept.name}" itemValue="#{dept}"/>
<p:ajax event="change" listener="#{officeManagmentController.onDepartmentChanged()}"/>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Role">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{office.role.name}" /></f:facet>
<f:facet name="input">
<p:selectOneMenu value="#{office.role}" converter="#{roleConverter}" style="width:96%" >
<f:selectItems value="#{officeManagementController.assignableRoles}" var="role" itemLabel="#{role.name}" itemValue="#{role}"/>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
the following is my officecontroller managedbean
#ConversationScoped
public class OfficeManagementController implements Serializable {
public void onRowSelect(SelectEvent event) {
FacesMessage msg = new FacesMessage(selectedOffice.getName()+" selected");
selectedDepartment=selectedOffice.getDepartment();
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onDepartmentChanged() {
if(selectedOffice!=null)selectedSystem=selectedOffice.getDepartment();
log.info("Calling onDepartmentChanged " + ((selectedDepartment != null) ? selectedDepartment.getName() : "null"));
if(selectedDepartment != null) {
assignableRoles = roleRepo.findByDepartment(selectedDepartment);
for(int i=0;i<assignableRoles.size();i++){
System.out.println(assignableRoles.get(i));
}
} else {
assignableRoles=null;
}
}
}
What I noticed is the selectedOffice is null in the onDepartmentChanged() method, but in the onrowselect, the value is not null. I haven't progressed more than that. I have to dynamically change the roles when a department changes. Why is the selectedOffice null?

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

<p:commandButton> does not invoke method when <p:column selectionMode="multiple"> is added

I have a <p:dialog> with a <p:dataTable> and a <p:commandButton>.
When I add <p:columm selectionMode="multiple"> to the table, then the button doesn't invoke the action listener method. Without that column, it works fine.
How is this caused and how can I solve it?
Here is my view:
<p:dialog id="CategoriasShowPadre" header="#{msgs['Categorias.BusquedaDeCategorias']}" widgetVar="CategoriasShowPadre" modal="true">
<p:dataTable id="DTBusquedaCategoriasPadre" widgetVar="posiblesTablaP" var="BcatP" value="#{agregarCategorias.categoriasPosibles}"
emptyMessage="#{msgs['Categoria.SinRegistros']}" rowKey="#{BcatP.nombre}" selection="#{agregarCategorias.categoriasPosiblesSelecionadas}">
<p:column selectionMode="multiple" style="width:18px" />
<p:column id="nombreCol" filterBy="#{BcatP.nombre}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="#{msgs['Categoria.ColunmnaNombre']}" />
</f:facet>
<h:outputText value="#{BcatP.nombre}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="#{msgs['Categoria.ColunmnaDescripcion']}" />
</f:facet>
<h:outputText value="#{BcatP.descripcion}" />
</p:column>
</p:dataTable>
<p:commandButton id="AnadiraPadre" value="#{msgs['Categoria.Boton.AgregarCategorias']}"
immediate="true" actionListener="#{agregarCategorias.selecionadosElementosPadres()}"
onclick="CategoriasShowPadre.hide();" />
</p:dialog>
Here is the backing bean:
#ManagedBean
#RequestScoped
public class AgregarCategorias {
private List<Categorias> CategoriasPosibles;
private List<Categorias> CategoriasPosiblesSelecionadas;
#PostConstruct
private void MiPostConstructor() {
this.CategoriasPosibles = // ...
}
public List<Categorias> getCategoriasPosiblesSelecionadas() {
return CategoriasPosiblesSelecionadas;
}
public void setCategoriasPosiblesSelecionadas(List<Categorias> CategoriasPosiblesSelecionadas) {
this.CategoriasPosiblesSelecionadas = CategoriasPosiblesSelecionadas;
}
public List<Categorias> getCategoriasPosibles() {
return CategoriasPosibles;
}
public void setCategoriasPosibles(List<Categorias> CategoriasPosibles) {
this.CategoriasPosibles = CategoriasPosibles;
}
public void selecionadosElementosPadres(ActionEvent evento) {
// my method code
}
}
The 'selection' attribute of the datatable should reference an array of the domain object.
So change private List<Categoria> CategoriasPosiblesSelecionadas for private Categoria[] CategoriasPosiblesSelecionadas

Resources