Get to specific book using hyperlink in JSF [duplicate] - jsf

This question already has answers here:
Creating master-detail pages for entities, how to link them and which bean scope to choose
(2 answers)
Closed 7 years ago.
I have a very strange situation here. I have made my application in JSF and it looks just great, however I am not quite sure about following: I want to have a hyperlink so that once I click on my book title I get to a page with all the details regarding that book. My code looks so far:
My class looks like this:
package com.century.rental;
import javax.ejb.Stateless;
import javax.persistence.*;
import java.util.List;
#Stateless
public class GameEJB {
#PersistenceContext(unitName = "PerUni")
private EntityManager em;
public List<Game> findGames() {
TypedQuery<Game> query = em.createNamedQuery("Game.findAll", Game.class);
return query.getResultList();
}
public List<Game> findGamesByTitle(String title) {
TypedQuery<Game> query = em.createNamedQuery("Game.findByTitle", Game.class);
query.setParameter("title", title);
return query.getResultList();
}
public Game find(Long id) {
return em.find(Game.class, id);
}
public Game createGame(Game game) {
em.persist(game);
System.out.print("game stored");
return game;
}
}
My controller class looks like this:
package com.century.rental;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class GameController {
#EJB
private GameEJB gameEJB;
private Game game = new Game();
private String title = new String();
private List<Game> gameList = new ArrayList<Game>();
private List<Game> sgameList = new ArrayList<Game>();
public GameController() {
}
public String doCreateGame() {
gameEJB.createGame(game);
gameList = gameEJB.findGames();
game = new Game();
return "listGames.xhtml";
}
public Game getGame() {
return this.game;
}
public void setGame(Game game) {
this.game = game;
}
public List<Game> getGameList() {
gameList = gameEJB.findGames();
return gameList;
}
public void setGameList(List<Game> gameList) {
this.gameList = gameList;
}
public String searchGames() {
sgameList = gameEJB.findGamesByTitle(title);
return "resultsGames.xhtml";
}
public List<Game> getSgameList() {
return sgameList;
}
public void setSbookList(List<Game> sgameList) {
this.sgameList = sgameList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Entity:
package com.century.rental;
import java.util.Date;
import javax.persistence.*;
#Entity
#NamedQueries({
#NamedQuery(name = "Game.findAll", query = "SELECT g FROM Game g"),
#NamedQuery(name = "Game.findByTitle", query = "SELECT g FROM Game g WHERE g.title = :title")
})
public class Game extends Product {
#Basic(optional = false)
#Column(name = "DEVELOPER_STUDIO", nullable = false, length = 100)
private String developerStudio;
#Basic(optional = false)
#Column(name = "PLATFORM", nullable = false, length = 100)
private String platform;
public Game() {
}
public Game(String title, String description, String rating, Date releaseDate, String developerStudio, String platform) {
super(title, description, rating, releaseDate);
this.developerStudio = developerStudio;
this.platform = platform;
}
public String getDeveloperStudio() {
return developerStudio;
}
public void setDeveloperStudio(String developerStudio) {
this.developerStudio = developerStudio;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
}
My HTML code looks like this:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>List of All Available Games in Database</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="List of All Available Games in Database"/></h1>
<h:dataTable value="#{gameController.gameList}" var="item" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Id"/>
</f:facet>
<h:outputText value="#{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:outputText value="#{item.title}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Description"/>
</f:facet>
<h:outputText value="#{item.description}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Rating"/>
</f:facet>
<h:outputText value="#{item.rating}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Release Date"/>
</f:facet>
<h:outputText value="#{item.releaseDate}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Developer Studio"/>
</f:facet>
<h:outputText value="#{item.developerStudio}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Platform"/>
</f:facet>
<h:outputText value="#{item.platform}"/>
</h:column>
</h:dataTable>
</h:form>
</f:view>
<br /><br />
Add New -OR- Go to Main Page
</h:body>
</html>
Please, help me I am not quite sure how can I make a hyperlink to a specific book from a list of books( as shown in the code )
Thanks.
P.S.
My specific page specificGame, which is supposed to get values from a game that was clicked in listGames page.
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>List specific Game in Database</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="List a specific Game in Database"/></h1>
<h:dataTable value="#{GameController.sGameList}" var="item" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:outputText value="#{item.title}"/>
</h:column
</h:dataTable>
</h:form>
</f:view>
<br /><br />
Add New -OR- Go to Main Page
</h:body>
</html>
Now when I click on a title from the list of games on page list Games I would be redirected to specific Game page which is supposed to look like this: http://i.stack.imgur.com/OHmdp.png
populated by all the values from the particular game.

Try to change the column title in listGames.xhtml to :
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:commandLink value="#{item.title}" action="#{gameController.searchGames(item.title)}" />
</h:column>
Then, adapt the action method in the backing bean to :
public String searchGames(String tit) {
sgameList = gameEJB.findGamesByTitle(tit);
return "resultsGames.xhtml";
}

Related

JSF datatable values are not displayed

Unfortunately, all the values I entered are not displayed on the website, does anyone have an idea why?
On picture 1 I have screenshotted the output.
Programming with JSF for the first time and with Java was a long time ago...
Here is my products.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>JSF Datatable h:dataTable tag demo</title>
</h:head>
<h:body>
<ui:composition template="template/master.xhtml">
<ui:define name="content">
<h:form>
<h:dataTable value = "#{productBean.findAll}"
var = "item"
style="border: 3px solid #FF0000; width:100%"
rows="10"
border="1"
cellpadding="20"
cellspacing="20">
<h:column>
<f:facet name="header"> Produkt ID </f:facet>
<h:outputText value="#{item.id}" />
</h:column>
<h:column>
<f:facet name="header"> Produktname </f:facet>
<h:outputText value="#{item.name}" />
</h:column>
<h:column>
<f:facet name="header" > Preis </f:facet>
<h:outputText value="#{item.preis}" />
</h:column>
<h:column>
<f:facet name="header"> Beschreibung </f:facet>
<h:outputText value="#{item.beschreibung}" />
</h:column>
<h:column>
<f:facet name="header"> Foto</f:facet>
<h:graphicImage library="images" name="item.bildpfad"></h:graphicImage>
</h:column>
<h:column>
<f:facet name="header"> Stückzahl </f:facet>
<h:outputText value="#{item.stückzahl}" />
</h:column>
</h:dataTable>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
My ProductDemo.java
package com.mycompany.kuechenstudio.model;
import java.util.ArrayList;
import java.util.List;
public class ProductDemo {
public List<Product> findAll(){
List<Product> productsList = new ArrayList<>();
productsList.add(new Product(1,"Kühlschrank",400f, "weiß",
"/images/Kühlschrank.jpg",1));
productsList.add(new Product(2,"Tisch ",300f, "braun",
"/images/Küchentisch.jpg",5));
productsList.add(new Product(3,"Schrank1",150f, "klein",
"/images/Küchenschrank_klein.jpg",3));
productsList.add(new Product(4,"Schrank2",200f, "groß",
"/images/Küchenschrank_groß.jpg",2));
productsList.add(new Product(5,"Spülmaschine",350f,"schnell",
"/images/Spülmaschine.jpg",6));
return productsList;
}
}
My Product.java
package com.mycompany.kuechenstudio.model;
public class Product {
private int id;
private String name;
private float preis;
private String beschreibung;
private String bildpfad;
private int stückzahl;
public int getStückzahl(){
return stückzahl;
}
public void setStückzahl(int stückzahl) {
this.stückzahl = stückzahl;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public float getpreis() {
return preis;
}
public String getBeschreibung() {
return beschreibung;
}
public void setBeschreibung(String beschreibung) {
this.beschreibung = beschreibung;
}
public String getBildpfad() {
return bildpfad;
}
public void setBildpfad(String bildpfad) {
this.bildpfad = bildpfad;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPreis(float preis) {
this.preis = preis;
}
public Product(int id, String name, float preis, String beschreibung,
String bildpfad, int stückzahl) {
super();
this.id = id;
this.name = name;
this.preis = preis;
this.beschreibung = beschreibung;
this.bildpfad = bildpfad;
this.stückzahl = stückzahl;
}
}
My beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
bean-discovery-mode="all">
<managed-bean>
<managed-bean-name>productBean</managed-bean-name>
<managed-bean-class>com.jsf.datatable.ProductDemo</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
Die Ausgabe
I got it. The ProductDemo.java must be in a managed bean and the value in the xhtml didnt match with the java class

Why Managed Bean List becomes including null elements in sessionscoped?

I want to get some information about contacts and display them in a table. I used two managed beans named by PersonalContact and BusinessContact and one abstract class named by Contact including firstName, lastName, ID, email and mphone attributes which derives from.Also, one generic class to add beans to an arraylist. I put here just BusinessContact as a bean, business as XHTML and tableBusiness as a table and generic class AddressBook, files of personal is parallel of them.
Inputs getting from screen are assigned to attributes of beans but when the beans go to Arraylist to be added, its content becomes null. Why is that? Is that about scopes?
Here is my bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class BusinessContact extends Contact{
private String companyName,workPhone;
public BusinessContact() {}
public BusinessContact(String companyName, String workPhone, String ID, String firstName, String lastName, String email, String mphone) {
super(ID, firstName, lastName, email, mphone);
this.companyName = companyName;
this.workPhone = workPhone;
}
/**
* Creates a new instance of BusinessContact
*/
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getWorkPhone() {
return workPhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
}
Here is my business.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Contact</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
First Name:
<h:inputText id="firstName" required="true"
requiredMessage="Can not be blank" value="#{businessContact.firstName}">
</h:inputText>
ID:
<h:inputText id="id" required="true"
requiredMessage="Can not be blank" value="#{businessContact.ID}">
</h:inputText>
Last Name:
<h:inputText id="lastName" required="true"
requiredMessage="Can not be blank" value="#{businessContact.lastName}">
</h:inputText>
E-mail:
<h:inputText id="email" required="true"
requiredMessage="Can not be blank" value="#{businessContact.email}">
</h:inputText>
Mobile Phone:
<h:inputText id="mphone" required="true"
requiredMessage="Can not be blank" value="#{businessContact.mphone}">
</h:inputText>
Company Name:
<h:inputText id="companyName" required="true"
requiredMessage="Can not be blank" value="#{businessContact.companyName}">
</h:inputText>
Work Phone:
<h:inputText id="workPhone" required="true"
requiredMessage="Can not be blank" value="#{businessContact.workPhone}">
</h:inputText>
</h:panelGrid>
<h:commandButton value="Add" id="add" action="#{addressBook.add(BusinessContact)}"></h:commandButton>
<h:commandButton value="Delete" id="delete" action="#{addressBook.remove(BusinessContact)}"></h:commandButton>
<h:outputLink id="t" value="tableBusiness.xhtml"> Click see the table</h:outputLink>
<ui:debug hotkey="k" rendered="true"/>
</h:form>
</h:body>
This is my generic class for putting beans to an arraylist:
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class AddressBook <T extends Contact> {
private ArrayList<T> list = new ArrayList<T>();
T clas;
public ArrayList<T> getList() {
return list;
}
public void setList(ArrayList<T> list) {
this.list = list;
}
public void add(T obj) {
this.clas=obj;
//System.out.println(obj.getFirstName()+" ");
list.add(obj);
}
public void remove(T obj)
{
list.remove(obj);
}
/**
* Creates a new instance of AddressBook
*/
public AddressBook() {}
}
And lastly, this xhmtl for displaying arraylist in table:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form>
"#{businessContact.firstName}"
<h:dataTable value="#{addressBook.list}" var="BusinessContact">
<h:column>
<f:facet name="header">
First Name
</f:facet>
<h:outputText value="#{businessContact.firstName}">
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
Last Name
</f:facet>
#{businessContact.lastName}
</h:column>
<h:column>
<f:facet name="header">
Mobile Phone
</f:facet>
#{businessContact.mphone}
</h:column>
<h:column>
<f:facet name="header">
E-mail
</f:facet>
#{businessContact.email}
</h:column>
<h:column>
<f:facet name="header">
Company Name
</f:facet>
#{businessContact.companyName}
</h:column>
<h:column>
<f:facet name="header">
Work Phone
</f:facet>
#{businessContact.workPhone}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
You are naming the var="BusinessContact" but trying to use it as "businessContact"
Change this line
<h:dataTable value="#{addressBook.list}" var="BusinessContact">
to this
<h:dataTable value="#{addressBook.list}" var="businessContact">

How can I get my ArrayList in my JSF to dynamically print?

When I run the JSF with the items listed, I get the first part to come up where I can enter in details successfully, but they won't show up on the screen like I want them to! Can you guys please help:
Here's my index.xhtml code:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>JSF Tester</title>
</h:head>
<h:body>
<h:form>
First name: <h:inputText value="#{PersonController.current.firstName}"/>
Last name: <h:inputText value="#{PersonController.current.lastName}"/>
Birth date: (mm-dd-yyyy) <h:inputText value="#{PersonController.current.date}">
<f:convertDateTime dateStyle="full" type="date" timeZone="CST" pattern="MM-dd-yyyy"/>
</h:inputText>
<h:commandButton value="Submit Person" action="#{PersonController.savePerson()}" />
<h:dataTable var="persons" value="#{PersonController.current}">
<h:column>
<f:facet name="header">First Name</f:facet>
#{PersonController.current.firstName}
</h:column>
<h:column>
<f:facet name="header">Last Name</f:facet>
#{PersonController.current.lastName}
</h:column>
<h:column>
<f:facet name="header">Date</f:facet>
#{PersonController.current.date}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
And here is my class containing the ArrayList:
/**
$ Id: PersonController.java v1.0, 7/16/2014
$ Name: $
*/
package org.usd.csci.person.jsf;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
*
* #author
*/
#ManagedBean(name="PersonController")
#SessionScoped
public class PersonController implements Serializable {
Person current = new Person();
List<Person> persons = new ArrayList<Person>();
/**
* Creates a new instance of PersonController
*/
public PersonController() {
}
public Person getCurrent() {
return current;
}
public List<Person> getPersons() {
return persons;
}
public void savePerson() {
persons.add(current);
current = new Person();
}
}
You are setting the datatable in a wrong way. It should be like the below.
value parameter should address the ArrayList. In your case PersonController.persons.
var parameter is used to address the Person objects in persons list. You can name it whatever you want.
To access a property of Person object You will use this var parameter. For example if you set var as
var = "variableName", You can access the properties of a Person object using variableName.firstName variableName.lastName etc.
<h:dataTable var="person" value="#{PersonController.persons}">
<h:column>
<f:facet name="header">First Name</f:facet>
#{person.firstName}
</h:column>
<h:column>
<f:facet name="header">Last Name</f:facet>
#{person.lastName}
</h:column>
<h:column>
<f:facet name="header">Date</f:facet>
#{person.date}
</h:column>
</h:dataTable>

<p:dialog> Primefaces Probleme?

I have created a complexe dataTable for my DiplomeBean and it shows Correctly
The deal is whene i select a row from the list ,nothing happend
I want whene i select a row it shows me the elements of the diplome.
My Bean
package com.beans;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.bo.DiplomeBo;
import com.converter.DiplomeDataModel;
import com.model.Collaborateur;
import com.model.Diplome;
public class DiplomeBean {
public Integer idDiplome;
public String ecole;
public String typeEcole;
public String typeDiplome;
public Integer promotion;
private Set<Collaborateur> collaborateurs = new HashSet<Collaborateur>(0);
public Diplome selectedDiplome;
public Diplome getSelectedDiplome() {
return selectedDiplome;
}
public void setSelectedDiplome(Diplome selectedDiplome) {
this.selectedDiplome = selectedDiplome;
}
public Integer getIdDiplome() {
return idDiplome;
}
public void setIdDiplome(Integer idDiplome) {
this.idDiplome = idDiplome;
}
private DiplomeBo diplomeBo;
public String getEcole() {
return ecole;
}
public void setEcole(String ecole) {
this.ecole = ecole;
}
public String getTypeEcole() {
return typeEcole;
}
public void setTypeEcole(String typeEcole) {
this.typeEcole = typeEcole;
}
public Integer getPromotion() {
return promotion;
}
public void setPromotion(Integer promotion) {
this.promotion = promotion;
}
public Set<Collaborateur> getCollaborateurs() {
return collaborateurs;
}
public void setCollaborateurs(Set<Collaborateur> collaborateurs) {
this.collaborateurs = collaborateurs;
}
public void setDiplomeBo(DiplomeBo diplomeBo) {
this.diplomeBo = diplomeBo;
}
public String getTypeDiplome() {
return typeDiplome;
}
public void setTypeDiplome(String typeDiplome) {
this.typeDiplome = typeDiplome;
}
public String AddDiplome(){
Diplome diplome =new Diplome();
diplome.setEcole(getEcole());
diplome.setPromotion(getPromotion());
diplome.setTypeDiplome(getTypeDiplome());
diplome.setTypeEcole(getTypeEcole());
diplomeBo.addDiplome(diplome);
clearForm();
return "Ajout Bien Fait !!";
}
public List<Diplome> getAllDiplome(){
return diplomeBo.findAllDiplome();
}
private void clearForm(){
this.setEcole("");
this.setPromotion(0);
this.setTypeEcole("Choisir type..");
this.setTypeEcole("Choisir type..");
}
}
My 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: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>
<body>
<f:view>
<h:outputLink value="Admin/default.xhtml">Go to your app</h:outputLink>
<h:form>
<p:dataTable value="#{diplome.getAllDiplome()}" var="d" paginator="true" rows="10" rowKey="diplome.idDiplome"
selection="#{diplome.selectedDiplome}" selectionMode="single"
onRowSelectUpdate="display" onRowSelectComplete="diplomeDialog.show()">
<f:facet name="header">
Diplome Liste
</f:facet>
<p:column sortBy="#{d.idDiplome}" filterBy="#{d.idDiplome}">
<f:facet name="header">
<h:outputText value="Model" />
</f:facet>
<h:outputText value="#{d.idDiplome}" />
</p:column>
<p:column sortBy="#{d.ecole}" filterBy="#{d.ecole}">
<f:facet name="header">
<h:outputText value="Ecole" />
</f:facet>
<h:outputText value="#{d.ecole}" />
</p:column>
</p:dataTable>
<p:dialog header="Diplome Detail" widgetVar="diplomeDialog" resizable="false"
width="200" showEffect="explode" hideEffect="explode">
<h:outputText value="id:" />
<h:outputText value="#{diplome.selectedDiplome.idDiplome}" />
<h:outputText value="Ecole:" />
<h:outputText value="#{diplome.selectedDiplome.ecole}" />
</p:dialog>
</h:form>
</f:view>
</body>
</html>
Thank's :)
Perhaps try modifying it slightly to be more like the Primefaces showcase example. For example, try something like this:
...
<h:form id="form">
...
<p:dataTable value="#{diplome.getAllDiplome()}" var="d" paginator="true" rows="10"
rowKey="diplome.idDiplome" selection="#{diplome.selectedDiplome}" selectionMode="single">
<p:ajax event="rowSelect" update=":form:display" oncomplete="PF('diplomeDialog').show()" />
...
</p:dataTable>
...
<p:dialog header="Diplome Detail" widgetVar="diplomeDialog" resizable="false"
width="200" showEffect="explode" hideEffect="explode">
<h:panelGrid id="display">
...
</h:panelGrid>
</p:dialog>
</h:form>
...
I can't test this right now, but it seems like it should work since it is essentially the same as the showcase's example.

Dependent columns in Primefaces datatable

I'm using an editable Primefaces p:datatable to show the data to the user. In this datatable, I have a p:column with a h:selectOneMenu, and another one with a p:selectBooleanCheckbox.
I want to check or uncheck and disable or enable the checkbox depending on the value selected in the h:selectOneMenu.
If I only had one h:selectOneMenu and one p:selectBooleanCheckbox, I'd use a p:ajax to attach a listener to the change event, and I'd manipulate the p:selectBooleanCheckbox in this method. But I have a pair of h:selectOneMenu and p:selectBooleanCheckbox per row and I don't know how to do this.
This is what I tried:
<h:form>
<p:dataTable var="appointment" value="#{prescController.appointmentsToday}" editable="true" id="tblAppointments">
<p:ajax event="rowEdit"
listener="#{prescController.onEdit}" update=":messages" />
<p:column sortBy="presc.drug" headerText="Drug">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{appointment.presc.drug.name}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{appointment.presc.drug}"
converter="#{drugConverter}" required="true">
<f:selectItem itemLabel="" noSelectionOption="true" />
<f:selectItems value="#{prescController.drugs}"
var="drug" itemLabel="#{drug.name}" />
<p:ajax update="autoAmount" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column sortBy="presc.autoAmount" headerText="Auto amount">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="Y"
rendered="#{not empty appointment.presc.drug.rules and appointment.presc.autoAmount}" />
<h:outputText value="N"
rendered="#{empty appointment.presc.drug.rules or not appointment.presc.autoAmount}" />
</f:facet>
<f:facet name="input">
<p:selectBooleanCheckbox id="autoAmount"
value="#{not empty appointment.presc.drug.rules and appointment.presc.autoAmount}"
disabled="#{appointment.presc.drug.name eq 'somethingsomething'}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
The post Retrieving other component's client ID in JSF 2.0 describes how to retrieve ids of other components in a page. In my opinion, the #{p:component('sampleButton')} should find the next component having this ID in the component tree - this should be the same row.
Alternatively, you should be able to rerender the whole row via JSF 2 #{component.parent.clientId} functionality (measure out, how many "parent" steps you need, e.g. #{component.parent.parent.clientId}).
Hope it helps, else just add comments... :-)
I can't imagine why you are unsatisfied with simple update="checkboxId"but what you can try is updating component through widgetVar which you can generate during page render.
Tiny example:
<?xml version="1.0" encoding="UTF-8" ?>
<!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:h="http://xmlns.jcp.org/jsf/html" xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Table Example</title>
</h:head>
<h:body>
<h:form prependId="false">
<p:dataTable var="data" value="#{tableBean.data}">
<p:column headerText="Command">
<p:commandButton value="Toggle" actionListener="#{tableBean.toggleSelection(data.id)}"
update="#widgetVar(tableCheckboxComponent_#{data.id})" />
</p:column>
<p:column headerText="Value">
<h:outputText value="#{data.value}" />
</p:column>
<p:column headerText="Selected">
<p:selectBooleanCheckbox widgetVar="tableCheckboxComponent_#{data.id}" value="#{data.selected}" />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
Backing bean:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class TableBean {
private Map<String, MyData> data;
public List<MyData> getData(){
return new ArrayList<MyData>(data.values());
}
public TableBean() {
data = new HashMap<String, MyData>();
for (int i = 0; i<22; i++) {
String id = "id" + Integer.toString(i);
data.put(id, new MyData( id , i));
}
}
public void toggleSelection(String id) {
MyData myData = data.get(id);
myData.setSelected(!myData.isSelected());
}
}
And Data object:
public class MyData {
private String id;
private boolean selected;
private int value;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public MyData(String id, int value) {
this.id = id;
this.value = value;
this.selected = false;
}
}
I still don't know why my approach didn't work.
In the end, I added a listener to the p:ajax component to manipulate the SelectBooleanCheckbox in the managed bean
<p:ajax listener="#{prescBean.onDrugSelected}" update="autoAmount" />
public void onDrugSelected(AjaxBehaviorEvent event) {
Drug drug = (Drug) ((UIOutput) event
.getSource()).getValue();
boolean hasRules = drug.getRules().size() > 0;
SelectBooleanCheckbox cbAutoAmount = (SelectBooleanCheckbox) ComponentUtils
.findComponent(FacesContext.getCurrentInstance().getViewRoot(),
"autoAmount");
cbAutoAmount.setDisabled(!hasRules);
cbAutoAmount.setValue(hasRules);
}

Resources