Buttons on footer of dynamic dialog

UI Components for JSF
Post Reply
olafvdmeer
Posts: 13
Joined: 30 Apr 2019, 08:36

21 May 2019, 11:16

I try to add some buttons on the footer of a dynamic created dialog, but I don't get it working. Any suggestions how to do this?
I am using primefaces 7.0.

I create the dialog like this:

Code: Select all

  
  public void viewCar()
  {
    Map<String, Object> options = new HashMap<String, Object>();
    options.put( "resizable", true );
    options.put( "modal", true );
    options.put( "draggable", true );
    options.put( "headerElement", "customheader" );

    PrimeFaces.current().dialog().openDynamic( "car-dialog", options, null );
  }
  
car-dialog.xhtml

Code: Select all

<!DOCTYPE html>
<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>
</h:head>
<h:body>
	<h:form id="carform">
        <p:outputPanel id="carDetail" style="text-align:center;">
            <p:panelGrid  columns="2" columnClasses="label,value">
                 
                <h:outputText value="Id:" />
                <h:outputText value="#{dtSelectionView.selectedCar.id}" />
 
                <h:outputText value="Year" />
                <h:outputText value="#{dtSelectionView.selectedCar.year}" />
 
                <h:outputText value="Color:" />
                <h:outputText value="#{dtSelectionView.selectedCar.color}" style="color:#{dtSelectionView.selectedCar.color}"/>
             
                <h:outputText value="Price" />
                <h:outputText value="$ #{dtSelectionView.selectedCar.price}" />
            </p:panelGrid>
        </p:outputPanel>
	</h:form>
	
	<!-- Try to add a button on the footer, but it is never show -->
	<f:facet name="footer">
		<p:commandButton value="Close" id="close" onclick="PF('carDialog').hide()" icon="pi pi-times" />
	</f:facet>	        
	
</h:body>
</html>
for a complete test case use this as index.xhtml

Code: Select all

<!DOCTYPE html>
<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:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui">
	
<h:head>
</h:head>
	
<h:body>

	<h:form id="form">

		<p:dataTable id="basicDT" var="car" value="#{dtSelectionView.cars}"	>
			<f:facet name="header">
            	Dynamic dialog
        	</f:facet>
			<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 style="width:32px;text-align: center">
				<p:commandButton action="#{dtSelectionView.viewCar}" icon="pi pi-search" title="View">
					<f:setPropertyActionListener value="#{car}" target="#{dtSelectionView.selectedCar}" />
				</p:commandButton>
			</p:column>
		</p:dataTable>
    		
	</h:form>
</h:body>
</html>
SelectionView.java

Code: Select all


import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.PrimeFaces;

@ManagedBean( name = "dtSelectionView" )
@SessionScoped
public class SelectionView implements Serializable
{
  private static final long serialVersionUID = -8897214602403269855L;

  private List<Car> cars;
  private Car selectedCar;

  @ManagedProperty( "#{carService}" )
  private CarService service;

  @PostConstruct
  public void init()
  {
    service = new CarService();
    cars = service.createCars( 20 );
  }

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

  public void viewCar()
  {
    Map<String, Object> options = new HashMap<String, Object>();
    options.put( "resizable", true );
    options.put( "modal", true );
    options.put( "draggable", true );
    options.put( "headerElement", "customheader" );

    PrimeFaces.current().dialog().openDynamic( "car-dialog", options, null );
  }
}
CarService.java

Code: Select all


import java.io.Serializable;
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 implements Serializable
{
  private static final long serialVersionUID = 5638609355408123343L;

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

  private int getRandomPrice()
  {
    return (int)( Math.random() * 100000 );
  }

  private 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


import java.io.Serializable;

public class Car implements Serializable
{
  private static final long serialVersionUID = 4686946389675400929L;

  public String id;
  public String brand;
  public int year;
  public String color;
  public int price;
  public boolean sold;

  public Car()
  {
  }

  public Car( String id, String brand, int year, String color )
  {
    this.id = id;
    this.brand = brand;
    this.year = year;
    this.color = color;
  }

  public Car( String id, String brand, int year, String color, int price, boolean sold )
  {
    this.id = id;
    this.brand = brand;
    this.year = year;
    this.color = color;
    this.price = price;
    this.sold = sold;
  }

  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 isSold()
  {
    return sold;
  }

  public void setSold( boolean sold )
  {
    this.sold = sold;
  }

  @Override
  public int hashCode()
  {
    int hash = 7;
    hash = 59 * hash + ( this.id != null ? this.id.hashCode() : 0 );
    return hash;
  }

  @Override
  public boolean equals( Object obj )
  {
    if ( obj == null )
    {
      return false;
    }
    if ( getClass() != obj.getClass() )
    {
      return false;
    }
    final Car other = (Car)obj;
    if ( ( this.id == null ) ? ( other.id != null ) : !this.id.equals( other.id ) )
    {
      return false;
    }
    return true;
  }
}

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 38 guests