p:ajax tag keeps values when i re-open dialog

UI Components for JSF
Post Reply
leonidas79
Posts: 5
Joined: 21 Feb 2018, 17:37

21 Feb 2018, 17:47

i open a dialog to edit data from a dataTable using this code :

Code: Select all

<p:menuButton value="Actions">
<p:menuitem resetValues="true" value="Update"
    icon="fa fa-edit" process="@this"
    actionListener="#{myMB.resetSelectedEntite(a)}"
    update="formUpdateAff:displayEditionAff"
    oncomplete="PF('affUpdateDialogWidget').show()">
    <f:setPropertyActionListener
        target="#{myMB.selectedAffectation}" value="#{a}" />
    <f:actionListener
        type="com.dummy.AffActionListener" />
</p:menuitem>
in my dialog i have some selectOneMenu that use p:ajax tag in order to render or update other components :

Code: Select all

<p:selectOneMenu style="width:100%; font-weight:bold"
					rendered="#{myMB.selectedAffectation.refCompagnie.id == 0}"
					required="true" id="villeId2" appendTo="@this"
					value="#{myMB.selectedAffectation.refGroupement.refVille.id}">
					<f:selectItem itemLabel="----------------" itemValue="" />
					<f:selectItems value="#{lieuUnitMB.selectLieuxByType('VILLE',0)}"
						var="a" itemValue="#{a.id}" itemLabel="#{a.libelle}" />
					<p:ajax
						listener="#{myMB.loadGroupements(myMB.selectedAffectation.refGroupement.refVille.id , myMB.selectedAffectation.refGroupement.refCatGroupement.id)}"
						update="groupIdAff groupIdxxx" />
				</p:selectOneMenu>
if i change this selectOneMenu value (without submitt) -----> close dialog -----> re-open it , i see that the dialog keeps the changed value like if it was submitted , i wonder if there is a way to avoid this behaviour and reset values when a dialog is opened.

Jan Eckert
Posts: 84
Joined: 11 Sep 2014, 10:13
Location: Brussels, Belgium

22 Feb 2018, 10:14

When you close the dialog, AFAIK, it just gets hidden by JavaScript which perfectly explains that it displays the changed, non-submitted values next time you chose to show it again.

To fall back to the last submitted values, You should just have to re-render it onclose or onshow. Like this:

Code: Select all

<p:dialog id="dialog" ... >
	<p:ajax event="close" update="dialog"/>
	...
</p:dialog>
Primefaces 6.1+
Wildfly 11

leonidas79
Posts: 5
Joined: 21 Feb 2018, 17:37

22 Feb 2018, 11:47

i tried your solution but it's not working , i still have the same problem.

maybe i wasn't clear enough , my dialog is filled with data coming from a dataTable using

Code: Select all

<f:setPropertyActionListener
        target="#{myMB.selectedAffectation}" value="#{a}" />
, and i have a selectOneMenu that updates another one is the same dialog , if i change any component that uses

Code: Select all

<p:ajax>
tag and close the dialog (without submutting it ) the next time i re-open the dialog i dont get the data that i have from dataTable but instead i get the values that i changed , by debugging i see that at the end of rendering the dialog the values setters of those components get invoked inside my bean.

Jan Eckert
Posts: 84
Joined: 11 Sep 2014, 10:13
Location: Brussels, Belgium

22 Feb 2018, 12:10

Sorry that it didn't work. Can you provide a minimal example? Because I did exactly that in a similar but easier case. Would love to troubleshoot that!
Primefaces 6.1+
Wildfly 11

leonidas79
Posts: 5
Joined: 21 Feb 2018, 17:37

22 Feb 2018, 17:28

the problem is the

Code: Select all

<p:ajax>
tag , if i change a value in any selectOneMenu and close the dialog (without submitting changes) it set the changed value in the selectedItem , so next time i open the dialog it's loaded xith the last value changed

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

23 Feb 2018, 00:53

Code: Select all

<p:ajax>
is maybe a bit TOO minimal. Jan is looking for an 'mcve' http://stackoverflow.com/help/mcve

leonidas79
Posts: 5
Joined: 21 Feb 2018, 17:37

23 Feb 2018, 12:31

here is the full minimal example to repreduce my problem

index.xhtml :

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
	xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions"
	xmlns:p="http://primefaces.org/ui" lang="en">
<h:head>
	<title>Primefaces</title>
</h:head>
<h:body>
	<p>
		<ui:insert name="body">This is my XHTML.</ui:insert>

		<h:form>
			<p:messages>
			</p:messages>
			<p:dataTable var="car" value="#{dtBasicView.cars}">
				<p:column headerText="Id">
					<h:outputText value="#{car.id}" />
				</p:column>

				<p:column headerText="Year">
					<h:outputText value="#{car.year}" />
				</p:column>

				<p:column headerText="Brand">
					<h:outputText value="#{car.brand}" />
				</p:column>

				<p:column headerText="Color">
					<h:outputText value="#{car.color}" />
				</p:column>
				<p:column headerText="Action">
					<p:menu>
						<p:menuitem value="edit" icon="ui-icon-disk"
							oncomplete="PF('dlg1').show();" update=":myForm:myDialog">
							<f:setPropertyActionListener value="#{car}"
								target="#{dtBasicView.selectedCar}" />
							<p:resetInput target=":myForm:myDialog" />
						</p:menuitem>
					</p:menu>
				</p:column>
			</p:dataTable>
		</h:form>

	</p>
</h:body>
<ui:include src="/dialogEdit.xhtml" />
</html>
BasicView.java

Code: Select all

package org.test.pf;
 
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="dtBasicView")
@SessionScoped
public class BasicView implements Serializable {
     
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<Car> cars;
     
    @ManagedProperty("#{carService}")
    private CarService service;
    
    private Car selectedCar;
 
    @PostConstruct
    public void init() {
        cars = service.createCars(10);
    }
     
    public List<Car> getCars() {
        return cars;
    }
 
    public void setService(CarService service) {
        this.service = service;
    }

	public Car getSelectedCar() {
		return selectedCar;
	}

	public void setSelectedCar(Car selectedCar) {
		this.selectedCar = selectedCar;
	}
}
CarService.java

Code: Select all

package org.test.pf;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
 
@ManagedBean(name = "carService")
@ApplicationScoped
public class CarService {
     
    private final static String[] colors;
     
    private final static String[] brands;
     
    static {
        colors = new String[10];
        colors[0] = "Black";
        colors[1] = "White";
        colors[2] = "Green";
        colors[3] = "Red";
        colors[4] = "Blue";
        colors[5] = "Orange";
        colors[6] = "Silver";
        colors[7] = "Yellow";
        colors[8] = "Brown";
        colors[9] = "Maroon";
         
        brands = new String[10];
        brands[0] = "BMW";
        brands[1] = "Mercedes";
        brands[2] = "Volvo";
        brands[3] = "Audi";
        brands[4] = "Renault";
        brands[5] = "Fiat";
        brands[6] = "Volkswagen";
        brands[7] = "Honda";
        brands[8] = "Jaguar";
        brands[9] = "Ford";
    }
     
    public List<Car> createCars(int size) {
        List<Car> list = new ArrayList<Car>();
        for(int i = 0 ; i < size ; i++) {
            list.add(new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState()));
        }
         
        return list;
    }
     
    private String getRandomId() {
        return UUID.randomUUID().toString().substring(0, 8);
    }
     
    private int getRandomYear() {
        return (int) (Math.random() * 50 + 1960);
    }
     
    private String getRandomColor() {
        return colors[(int) (Math.random() * 10)];
    }
     
    private String getRandomBrand() {
        return brands[(int) (Math.random() * 10)];
    }
     
    public int getRandomPrice() {
        return (int) (Math.random() * 100000);
    }
     
    public boolean getRandomSoldState() {
        return (Math.random() > 0.5) ? true: false;
    }
 
    public List<String> getColors() {
        return Arrays.asList(colors);
    }
     
    public List<String> getBrands() {
        return Arrays.asList(brands);
    }
}
Car.java

Code: Select all

package org.test.pf;

public class Car {

	private String id;
	private String brand;
	private int year;
	private String color;
	private int price;
	private boolean soldState;
	
	
	public Car(String id, String brand, int year, String color, int price, boolean soldState) {
		super();
		this.id = id;
		this.brand = brand;
		this.year = year;
		this.color = color;
		this.price = price;
		this.soldState = soldState;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public boolean getSoldState() {
		return soldState;
	}
	public void setSoldState(boolean soldState) {
		this.soldState = soldState;
	}
	
}
dialogEdit.xhtml

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:p="http://primefaces.org/ui" lang="en">
<h:head>
	<title>Edit</title>
</h:head>
<h:body>
	<h:form id="myForm">
		<p:dialog header="Edit" widgetVar="dlg1" width="200" height="200"
			id="myDialog" closable="true">
			<h:outputText value="#{dtBasicView.selectedCar.brand}" />
			<p:selectOneMenu id="console"
				value="#{dtBasicView.selectedCar.brand}" style="width:125px">
				<f:selectItem itemLabel="Select One" itemValue="" />
				<f:selectItems value="#{carService.brands}" />
				<p:ajax/>
			</p:selectOneMenu>
		</p:dialog>
	</h:form>
</h:body>
</html>
when i open the dialog and change the brand value (without submitting), if i close the dialog and reopen it it keeps the changed value and not the one from the dataTable row , how can i avoid this behavior ? i need to use <p:ajax/> tag to update another component in the dialog.

leonidas79
Posts: 5
Joined: 21 Feb 2018, 17:37

26 Feb 2018, 16:47

i am still facing same issue , please any idea ?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 43 guests