I'm having problems with PrimeFaces Dialog. I have a Dialog that will appear when clicking a commandButton, in the oncomplete method, of a dataTable. This Dialog will have another dataTable that will also have another button to open another dialog, this time with another dataTable and nothing else. My problem is, the first Dialog does not pull the objects correctly, as you can see in the code, the button calls the method in the actionListener but the dialog seems to open before executing the actionListener and opens with the objects still empty.
package com.sysnutriweb.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import com.sysnutriweb.dao.AlimentoXDietaDAO;
import com.sysnutriweb.dao.DietaDAO;
import com.sysnutriweb.domain.Alimento;
import com.sysnutriweb.domain.AlimentoXDieta;
import com.sysnutriweb.domain.Dieta;
@Named(value = "MBDiet")
@ManagedBean(name = "MBDiet")
@SessionScoped
public class DietaBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private List listDiets;
private Dieta diet = new Dieta();
private DietaDAO dDao;
private List foods = new ArrayList<>();
private Alimento food;
private String flagS = "V-";
@PostConstruct
public void begin() {
dDao = new DietaDAO();
listDiets = dDao.loadAll();
flagS += "B-";
}
//@PostConstruct
public void getDietSelected(ActionEvent e) {
diet = (Dieta) e.getComponent().getAttributes().get("selectedDiet");
System.out.println("Diet: " + diet.toString());
List<AlimentoXDieta> axd = new AlimentoXDietaDAO().loadAll();
for (int i = 0; i < axd.size(); i++) {
if (axd.get(i).getDietId().getId() == diet.getId())
foods.add(axd.get(i));
}
flagS+="S-";
System.out.println("Called: " + foods.size() + "\nFlagS: " + flagS);
}
public void getFoodSelected(ActionEvent e) {
food = (Alimento) e.getComponent().getAttributes().get("selectedFood");
}
// Getters Setters
}
And here's the problematic part of my XHTML.
<p:column headerText="Descrição da dieta" id="descricao">
<p:outputLabel value="#{diet.descricaoDieta}" />
</p:column>
<p:column headerText="Ações" id="acoes" width="10%">
<center>
<p:commandButton icon="ui-icon-info" id="btnInfo" update="@form"
actionListener="#{MBDiet.getDietSelected}"
oncomplete="PF('dlgFoods').show()" ajax="true">
<f:attribute name="selectedDiet" value="#{diet}" />
</p:commandButton>
<p:tooltip id="toolTipBtnInfo" for="btnInfo"
value="Clique para ver os alimentos presentes nesta dieta"
position="top" />
</center>
</p:column>
</p:dataTable>
</h:form>
</p:layoutUnit>
This part of the code calls the dialog described here
<!-- Diet info -->
<p:dialog
header="Alimentos na dieta #{MBDiet.flagS} #{MBDiet.diet.nomeDieta}"
widgetVar="dlgFoods" modal="true" showEffect="explode"
resizable="false" draggable="false" appendTo="@(body)">
<h:form id="formInfoTable">
<p:dataTable emptyMessage="Nenhum dado cadastrado!" paginator="true"
id="tbFoodsDiet" value="#{MBDiet.foods}" var="food">
<p:column headerText="Nome" id="nome" width="10px">
<p:outputLabel value="#{food.foodId.nome}" />
</p:column>
<p:column headerText="Hora para consumir" id="descricao" width="40%">
<p:outputLabel value="#{food.hora}" />
</p:column>
<p:column headerText="Ações" id="acoes" width="10%">
<center>
<p:commandButton icon="ui-icon-info" id="btnInfo"
update=":formInfo" actionListener="#{MBDiet.getFoodSelected}"
oncomplete="PF('dlgFoodInfo').show()">
<f:attribute name="selectedFood" value="#{food}" />
</p:commandButton>
<p:tooltip id="toolTipBtnInfo" for="btnInfo"
value="Clique para ver os detalhes deste alimento" position="top" />
</center>
</p:column>
</p:dataTable>
</h:form>
</p:dialog>
That will soon call this dialog
<!-- Food info -->
<p:dialog header="Alimento #{MBDiet.food.nome}" widgetVar="dlgFoodInfo"
modal="true" showEffect="fade" height="100" resizable="false" id="dlgFoodInfo"
draggable="true" appendTo="@(body)">
<h:form id="formInfo">
<h:panelGrid columns="2">
<p:outputLabel value="Nome:" />
<p:outputLabel value="#{MBDiet.food.nome}" />
<p:outputLabel value="Açucares:" />
<p:outputLabel value="#{MBDiet.food.acucares}" />
<p:outputLabel value="Cálcio:" />
<p:outputLabel value="#{MBDiet.food.calcio}" />
<p:outputLabel value="Calorias:" />
<p:outputLabel value="#{MBDiet.food.calorias}" />
<p:outputLabel value="Carboidratos:" />
<p:outputLabel value="#{MBDiet.food.carboidratos}" />
<p:outputLabel value="Colesterol:" />
<p:outputLabel value="#{MBDiet.food.colesterol}" />
<p:outputLabel value="Ferro:" />
<p:outputLabel value="#{MBDiet.food.ferro}" />
<p:outputLabel value="Fibras:" />
<p:outputLabel value="#{MBDiet.food.fibras}" />
<p:outputLabel value="Gorduras:" />
<p:outputLabel value="#{MBDiet.food.gorduras}" />
<p:outputLabel value="Insaturadas:" />
<p:outputLabel value="#{MBDiet.food.insaturadas}" />
<p:outputLabel value="pootássio:" />
<p:outputLabel value="#{MBDiet.food.potassio}" />
<p:outputLabel value="Proteínas:" />
<p:outputLabel value="#{MBDiet.food.proteinas}" />
<p:outputLabel value="Saturadas:" />
<p:outputLabel value="#{MBDiet.food.saturadas}" />
<p:outputLabel value="Trans:" />
<p:outputLabel value="#{MBDiet.food.trans}" />
<p:outputLabel value="Vitamina A:" />
<p:outputLabel value="#{MBDiet.food.vitaminaA}" />
<p:outputLabel value="Vitamina C:" />
<p:outputLabel value="#{MBDiet.food.vitaminaC}" />
<p:outputLabel value="Vitamina D:" />
<p:outputLabel value="#{MBDiet.food.vitaminaD}" />
</h:panelGrid>
<p:separator />
<p:outputLabel value="Informações especiais" />
<p:separator />
<h:panelGrid columns="2">
<p:outputLabel value="Data de cadastro:" />
<p:outputLabel value="#{MBDiet.food.dtCad}" />
<p:outputLabel value="Identificação:" />
<p:outputLabel value="#{MBDiet.food.id}" />
</h:panelGrid>
</h:form>
</p:dialog>
If you prefer, I'll leave the github link and apologize, this is my first question!
Right now, thank you all!
Link: link