PrimeFaces commandButton ajax does not work - ajax

I have simple JSF page and simple backing bean. I have p:commandButton on the page and p:datatable. With commandButton I need to update dataTable with ajax, but it does not happens. What could be the reason?
Here is the page:
<ui:define name="content">
<h2>#{menu['management.roles.perms']}</h2>
<h:form id="form">
<p:fieldset style="border: 0px" toggleable="false" collapsed="false">
<p:tabView>
<p:tab title="#{messages['roles']}">
<p:dataTable id="dataTable" value="#{rolesBean.roles}" var="role"
editable="true" rows="10" rowKey="#{role.id}">
<p:ajax event="rowEdit" listener="#{rolesBean.onEdit}"/>
<f:facet name="footer">
<p:commandButton value="#{misc['add.role']}"
actionListener="#{rolesBean.addRole}" update="dataTable"/>
</f:facet>
<p:column headerText="#{misc['name.database']}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{role.name}"/>
</f:facet>
<f:facet name="input">
<h:inputText value="#{role.name}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="#{misc['date']}">
<f:facet name="output">
<h:outputText value="#{role.creationDate}">
<f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
</f:facet>
</p:column>
<p:column headerText="#{misc['description']}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{role.description}"/>
</f:facet>
<f:facet name="input">
<h:inputText value="#{role.description}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="#{messages['edit']}">
<p:rowEditor />
</p:column>
</p:dataTable>
</p:tab>
<p:tab title="#{messages['perms']}">
</p:tab>
<p:tab title="#{clients['history']}">
</p:tab>
</p:tabView>
</p:fieldset>
</h:form>
</ui:define>
And here is the code of backingBean:
#ViewScoped
#ManagedBean(name = "rolesBean")
public class RolesViewBean extends BasicViewBean<Role> {
private static final long serialVersionUID = 27377603187254821L;
#Inject
RoleDao dao;
private List<Role> roles;
#Override
protected BasicDao<Role> getDAO() {
return dao;
}
public void onEdit(RowEditEvent event){
this.entity = (Role) event.getObject();
dao.update(this.entity);
}
public void addRole(){
roles.add(new Role());
}
public List<Role> getRoles() {
roles = dao.getRoleList();
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
}

At first glance, the problem is that your addRole method modifes the "roles" list, while the getRoles (which gets called by the dataTable) resets it to whatever is saved in the database.
So your datatabe gets updated, it' just that it gets updated with the same values.
Think about that ;)

Related

setPropertyActionListener is not setting the target value in the bean

I am trying to set some information in dialogue box when the search button is clicked but the problem I am facing is that f:setPropertyActionListener is not setting the target value in the backing bean so when the popup comes its empty.
Dialogue box looks like this :
<h:form id="form">
<p:fieldset legend="Saved Branches" style="margin-bottom: 30px;">
<p:dataTable id="branches" var="branch" value="#{branchOnBoarding.savedBranches}">
<f:facet name="header">
Branch Information
</f:facet>
<p:column headerText="Branch Name">
<h:outputText value="#{branch.branchName}" />
</p:column>
<p:column headerText="Branch Code">
<h:outputText value="#{branch.branchCode}" />
</p:column>
<p:column headerText="Building">
<h:outputText value="#{branch.building}" />
</p:column>
<p:column headerText="Street">
<h:outputText value="#{branch.street}" />
</p:column>
<p:column headerText="City">
<h:outputText value="#{branch.city}" />
</p:column>
<p:column headerText="State">
<h:outputText value="#{branch.state}" />
</p:column>
<p:column headerText="Country">
<h:outputText value="#{branch.country}" />
</p:column>
<p:column style="width:32px;text-align: center">
<p:commandButton update=":form:branchDetail" oncomplete="PF('branchDialog').show()" icon="ui-icon-search" title="View">
<f:setPropertyActionListener value="#{branch}" target="#{branchOnBoarding.selectedBranch}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:dialog header="Branch Info" widgetVar="branchDialog">
<p:outputPanel id="branchDetail" style="text-align:center;">
<p:panelGrid columns="2" rendered="#{not empty branchOnBoarding.selectedBranch.branchName}" columnClasses="label,value">
<h:outputText value="Branch Name:" />
<h:outputText value="#{branchOnBoarding.selectedBranch.branchName}" />
<h:outputText value="Branch Code:" />
<h:outputText value="#{branchOnBoarding.selectedBranch.branchCode}" />
<h:outputText value="Building:" />
<h:outputText value="#{branchOnBoarding.selectedBranch.building}" />
<h:outputText value="Street:" />
<h:outputText value="#{branchOnBoarding.selectedBranch.street}" />
<h:outputText value="City:" />
<h:outputText value="#{branchOnBoarding.selectedBranch.city}" />
<h:outputText value="State:" />
<h:outputText value="#{branchOnBoarding.selectedBranch.state}" />
<h:outputText value="Country:" />
<h:outputText value="#{branchOnBoarding.selectedBranch.country}" />
</p:panelGrid>
</p:outputPanel>
</p:dialog>
</p:fieldset>
Code for backing bean looks like this :
#Component(value="branchOnBoarding")
#ManagedBean
#SessionScoped
#SpringScopeView
public class BranchOnBoarding implements Serializable{
private BranchUI branchUI = new BranchUI();
private List<Branch> savedBranches = new ArrayList<>();
private Branch selectedBranch;
public Branch getSelectedBranch() {
return selectedBranch;
}
public void setSelectedBranch(Branch selectedBranch) {
this.selectedBranch = selectedBranch;
}
public List<Branch> getSavedBranches() {
savedBranches = branchDao.findAll();
//for(Branch b:savedBranches)
//System.err.println(b.getBranchName());
return savedBranches;
}
public void setSavedBranches(List<Branch> savedBranches) {
this.savedBranches = savedBranches;
}
}
Dialogue box looks like this :

OneMenu & 2 ajax inside won't work

I'm facing a huge problem since 2 weeks.
I tested 2 ajax method one by one and they work. But when I want to have both in a oneMenu, the dropdown supposed to update the third oneMenu of the page does'nt work.
<h:body>
<h:form>
<p:growl id="growl" showDetail="true" />
<p:outputLabel value="Sélectionner une section " for="sectionBox"/>
<p:selectOneMenu id="sectionBox"
value="#{evaluationViewController.currentSection}"
converter="sectionConverter"
var="c"
filter="true"
filterMatchMode="startsWith"
effect="fade"
>
<p:ajax listener="#{evaluationViewController.onSectionSelected}" update="ue,ueO,growl"/>
<f:selectItem itemLabel="Sélectionnez uen section" itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{evaluationViewController.currentSections}"
var="currentSection"
itemLabel="#{currentSection.name}"
itemValue="#{currentSection}"
/>
<p:column>
<h:outputText value="#{c.name}"/>
</p:column>
</p:selectOneMenu>
<p:outputLabel value="Sélectionner une UE " for="ue"/>
<p:selectOneMenu id="ue"
value="#{evaluationViewController.ue}"
converter="ueConverter"
var="u"
filter="true"
filterMatchMode="startsWith"
>
<p:ajax listener="#{evaluationViewController.onUeSelected}" update="ueO,growl"/>
<p:ajax event="change" update="capacities" listener="#{evaluationViewController.listenerCapacitiesUpdate}"/>
<f:selectItem itemLabel="Sélectionnez une UE" itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{evaluationViewController.ues}"
var="ues"
itemLabel="#{ues.name}"
itemValue="#{ues}"
/>
<p:column>
<h:outputText value="#{u.name}"/>
</p:column>
</p:selectOneMenu>
<p:outputLabel value="Sélectionner l'UE enseignée" for="ueO"/>
<p:selectOneMenu id="ueO"
value="#{evaluationViewController.organizedUe}"
converter="organizedUeConverter"
var="o"
filter="true"
filterMatchMode="startsWith"
>
<f:selectItem itemLabel="Sélectionnez une année" itemValue="" noSelectionOption="true"/>
<f:selectItems
value="#{evaluationViewController.organizedUes}"
var="organizedUes"
itemLabel="#{organizedUes.name}"
itemValue="#{organizedUes}"
/>
<p:column>
<h:outputText value="#{o.name}"/>
</p:column>
<p:column>
<h:outputText value="#{o.level.name}"/>
</p:column>
</p:selectOneMenu>
<p:separator/>
<p:separator/>
<p:dataTable id="capacities"
var="cap"
value="#{evaluationViewController.capacities}"
editable="true"
editMode="cell"
widgetVar="cellCap"
>
<f:facet name ="header">
Capacitées
</f:facet>
<p:ajax event="cellEdit"
listener="#{evaluationViewController.onCellEdit}"
update="#this"/>
<p:column headerText="Dénomination">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{cap.name}"/></f:facet>
<f:facet name="input"><p:inputText value="#{cap.name}" style="width: 30%" label="Dénomination"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Description">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{cap.description}"/></f:facet>
<f:facet name="input"><h:inputText value="#{cap.description}" style="width: 90%" label="Description"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Réussite obligatoire">
<p:selectBooleanCheckbox value="#{cap.isThresholdOfSuccess}" style="alignment-adjust: central"/>
</p:column>
</p:dataTable>
<p:separator/>
</h:form>
</h:body>
I followed the primefaces demo as well for the bean :
public class EvaluationViewController implements Serializable {
#EJB
private SectionFacade ejbSectionFacade;
private List<Section> currentSections;
private Section currentSection;
#EJB
private UeFacade ejbUeFacade;
private List<Ue> ues;
private Ue ue;
#EJB
private OrganizedUeFacade ejbOrganizedUeFacade;
private List<OrganizedUe> organizedUes;
private OrganizedUe organizedUe;
#EJB
private CapacityFacade ejbCapacity;
private List<Capacity> capacities;
private Capacity capacity;
#PostConstruct
public void init() {
currentSections = ejbSectionFacade.findAll();
ues = new ArrayList<>();
organizedUes = new ArrayList<>();
capacities = new ArrayList<>();
}
public void onSectionSelected() {
if (currentSection != null && !currentSection.getName().equals("")) {
ues = ejbUeFacade.findBySection(currentSection);
ue = null;
capacity = null;
FacesMessage facesMessage = new FacesMessage("Sélection", currentSection.getName());
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
} else {
ues = null;
}
}
public void onUeSelected() {
if ((currentSection != null && !currentSection.getName().equals(""))
&& (ue != null && !ue.getName().equals(""))) {
organizedUes = ejbOrganizedUeFacade.findByUe(ue);
capacities = ejbCapacity.findByUe(ue);
capacity = null;
FacesMessage facesMessage = new FacesMessage("Sélection de l'UE : ", ue.getName());
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
} else {
organizedUes = null;
}
}
public void listenerCapacitiesUpdate() {
capacities = ejbCapacity.findByUe(ue);
}
The DataTable is updated and rendered but not the last oneMenu. I really don't get it. I would be very gratefull for the answer.
There are two things at hand here:
If you do not provide an explict event, then a default one is taken
You cannot add two ajax tags to the same component listening to the same event
So if in this case the default ajax event of the p:selectOneMenu is change, only the second one will be processed. Adding an explicit event other than change to the first ajax handler will make it work
Off-topic but important things you should normally do debugging issues like this:
Find the differences between the two and see and try to make them as identical as possible
See if the order plays a role by switching them
See if using a different component makes it work

Unable to update p:datatable cell after ajax call on another cell's cellEdit event

In my cell edit event in my backing bean, if the cell value is changed I want to update the value of another column as well of the datatable. The value is getting correctly set in my list object but is not getting reflected in my datatable. I tried updating using the datatable using RequestContext as well, but still the list is not getting refreshed. What am I doing wrong? Should I call any row event for this change?
Code snippet :
Bean
#Named("editView")
#ViewScoped
public class EditTableView implements Serializable {
private List<Cars> carList;
private List<String> brands;
#PostConstruct
public void init() {
brands = new ArrayList<String>();
brands.add("BMW");
brands.add( "Mercedes");
brands.add("Volvo");
brands.add("Audi");
brands.add("Renault");
brands.add("Fiat");
brands.add("Volkswagen");
brands.add("Honda");
brands.add("Jaguar");
brands.add( "Ford");
brands.add("Maruti Suzuki");
carList = new ArrayList<Cars>();
modifiedList = new ArrayList<Cars>();
Cars car =new Cars();
car.setCarId(1);
car.setBrand("BMW");
car.setColor("Grey");
car.setModelNumber("2323");
car.setSelfDriven("Y");
car.setYear("1980");
car.setStatus("");
carList.add(car);
}
public void onCellEdit(CellEditEvent event) {
int rowIndex = event.getRowIndex();
Cars car = null;
LOGGER.info("Car Edited"+rowIndex);
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
DataTable carTable = (DataTable) event.getSource();
String index = carTable.getClientId(FacesContext.getCurrentInstance());
LOGGER.info(index);
if(newValue != null && !newValue.equals(oldValue)) {
LOGGER.info("Cell Changed Old: " + oldValue + ", New:" + newValue);
car=carList.get(rowIndex);
carList.get(event.getRowIndex()).setStatus("Changed");
modifiedList.add(car);
}
}
}
xhtml
<h:form id="cellEditForm">
<p:dataTable id="cars" var="car" value="#{editView.carList}"
editable="true" editMode="cell" widgetVar="cellCars">
<f:facet name="header">Cell Editing</f:facet>
<p:ajax event="cellEdit" listener="#{editView.onCellEdit}"
update=":cellEditForm:cars" />
<p:column headerText="Id">
#{car.carId}
</p:column>
<p:column headerText="Model Name" style="background-color:yellow">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.modelNumber}" />
</f:facet>
<f:facet name="input">
<p:inputText id="modelInput" value="#{car.modelNumber}"
style="width:96%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Year" style="background-color:yellow">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.year}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{car.year}" style="width:96%" label="Year" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Brand" style="background-color:yellow">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.brand}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{car.brand}" style="width:100%">
<f:selectItems value="#{editView.brands}" var="man"
itemLabel="#{man}" itemValue="#{man}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Self Driven" style="background-color:yellow">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.selfDriven}" />
</f:facet>
<f:facet name="input">
<p:inplace>
<p:inputText value="#{car.selfDriven}" />
</p:inplace>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Color" style="background-color:yellow">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.color}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{car.color}" style="width:96%" label="Color" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<h:outputText value="#{car.status}" id="statusLabel"></h:outputText>
</p:column>
</p:dataTable>
</h:form>
I used jquery eventually. Got the index of the updated row and called jquery function from my bean.
RequestContext requestContext = RequestContext.getCurrentInstance();
String call = "updateCell(" + event.getRowIndex() + ")";
requestContext.execute(call);
Jquery code
function updateCell(i) {
var index = "cellEditForm:cars:"+i+":statusLabel";
document.getElementById(index).innerHTML="Changed";
}
OmniFaces has a great util for this, called 'Ajax'
A StackOverflow post about this shows how to use it: How to use OmniFaces Ajax.updateColumn() or Ajax.updateRow() via p:ajax
Update form not table or put table in panel and update panel.
to be safe
<h:form id="cellEditForm">
<p:panel id="something" styleclass="pero">
<p:dataTable id="cars
update="#(.pero)"
be sure to put id on panel because class update will search for that id and use it. This solution is because i don't know rest of the code and it is one that will work on any code.

primefaces p:ajax event="cellEdit" not firing

I have datatable with celledit, the ajax event does not fire on cell edit. The event listener does not get called.Thanks.
<ui:define name="content">
<h:form prependId="false" id="tForm">
<p:datatable id="dTable" value="#{bean.tData}" var="data"
editable="true" editMode="cell">
<p:ajax event="cellEdit" listener="#{bean.updateData(data)}" />
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{data.name}"></h:outputText>
</f:facet>
<f:facet name="input">
<p:inputText value="#{data.name}"></p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
</p:datatable>
</h:form>
</ui:define>
bean listener method
public void updateData(Data d){
logger.debug(d.getName());
}
I think you should use this instead:
<p:ajax event="cellEdit" listener="#{bean.updateData}" />
public void updateData(CellEditEvent event) {
Object newValue = event.getNewValue();
logger.debug(newValue.toString());
}

DataTable Order by column erase values

I have PrimeFaces dataTable that is been filled by a Ajax call.
When I click on a column title, to order its values, the values disappear.
<p:commandButton value="Pesquisar" actionListener="#{requestController.listRequests}" update="dataTable" />
Here is my view:
<p:dataTable id="dataTable" var="order" value="#{requestController.backing.requestsList}"
paginator="true" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">
<p:column sortBy="#{order.companyRequest}">
<f:facet name="header">
<h:outputText value="Pedido" />
</f:facet>
<h:outputText value="#{order.companyRequest}" />
</p:column>
<p:column sortBy="#{order.company.companyName}">
<f:facet name="header">
<h:outputText value="Cliente" />
</f:facet>
<h:outputText value="#{order.company.companyName}" />
</p:column>
<p:column sortBy="#{order.emissionDate}">
<f:facet name="header">
<h:outputText value="Data" />
</f:facet>
<h:outputText value="#{order.emissionDate}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
</h:outputText>
</p:column>
<p:column sortBy="#{order.requestSituation.description}">
<f:facet name="header">
<h:outputText value="Status" />
</f:facet>
<h:outputText value="#{order.requestSituation.description}" />
</p:column>
<p:column>
<f:facet name="header">
</f:facet>
<h:form>
<h:commandLink value="Detalhes"/>
</h:form>
</p:column>
</p:dataTable>
EDIT
RequestController
#ManagedBean
#RequestScoped
public class RequestController implements Serializable
{
private RequestBacking backing;
public RequestController()
{
backing = new RequestBacking();
}
public void changeEventListener(ValueChangeEvent e)
{
backing.requestSearchType = e.getNewValue().toString();
}
public void change()
{
switch (backing.requestSearchType)
{
case "data":
backing.mask = "99/99/9999";
backing.maskSize = "10";
break;
case "cnpj":
backing.mask = " 99.999.999/9999-99";
backing.maskSize = "20";
break;
default:
backing.mask = "";
backing.maskSize = "50";
}
}
public void listRequests() throws ParseException
{
CompanyVO companyVO = new CompanyVO();
switch (backing.requestSearchType)
{
case "cnpj":
companyVO.setCnpj(backing.requestSearchValue);
break;
case "cliente":
companyVO.setCompanyName(backing.requestSearchValue);
break;
case "pedido":
backing.requestVO.setCompanyRequest(Integer.parseInt(backing.requestSearchType));
break;
}
SupplierVO supplierVO = new Support().getUserSession().getSupplier();
backing.requestVO.setEmissionDate(new Support().convertDate(backing.requestSearchValue));
backing.requestVO.setSupplier(supplierVO);
backing.requestVO.setCompany(companyVO);
backing.requestsList = new ArrayList<>(backing.getBo().getRequest(backing.requestVO));
if (backing.requestsList.isEmpty())
{
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN, "Nenhum registro encontrado!", null);
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
backing.requestsList = null;
}
}
..backing getter and setter
}
My requestsList is on my RequestBacking where I have all my getters and setters, please correct me if there is a better way of doing this, I'm using it because it leaves my controller cleaner.
public List<RequestVO> requestsList;
Apparently the value="#{requestController.backing.requestsList}" didn't return the same value as it did on the initial request. That can happen if it's a request scoped bean and/or if the requestsList is populated on every request based on a request based variable.
That's just a design mistake. Put the managed bean in the view scope and make sure that you aren't doing any business logic in a getter method. The nested class backing is also suspicious or it must be a poor naming.
See also:
Why JSF calls getters multiple times
How to choose the right bean scope?
Update in a nutshell, your bean should look something like this:
#ManagedBean
#ViewScoped
public class Orders {
private String query; // +getter +setter
private List<Order> results; // +getter (no setter required)
#EJB
private OrderService service;
public void search() {
results = service.search(query);
}
// Add/generate normal getters/setters (don't change them!)
}
and your view should look like this:
<h:form>
<p:inputText value="#{orders.query}" />
<p:commandButton value="Search" action="#{orders.search}" update=":tableForm" />
</h:form>
<h:form id="tableForm">
<p:dataTable value="#{orders.results}" var="order" ... rendered="#{not empty orders.results}">
...
</p:dataTable>
<h:outputText value="No results found" rendered="#{facesContext.postback and empty orders.results}" />
</h:form>

Resources