I'm new in this community, but I really need help! I'm programming a application which gonna run in Desktop, with Java, and online, with Java Web, using Hibernate 4.3, without JPA, and Primefaces 8.2. My issue is, I have a page with a dataTable, this dataTable contains a button on each register to open a dialog with more information of this register. Inside this dialog i have another dataTable with another button inside each register. Here I have two issues.
First, when user clicks on the button in the first dialog, the second dialog appears but, it just ignore the ActionListener.
Second, on the first dialog, when user clicks to go to other page of dataTable, or just increase the number of rows, the data disappear! If you close and reopen the dialog, it open in the page you clicked before and the data will be there.
I'll post the principal code of the problem, the github link and a video to make it easy to figure out the problem.
Bean.java
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 org.primefaces.model.LazyDataModel;
import com.sysnutriweb.dao.AlimentoXDietaDAO;
import com.sysnutriweb.dao.DietaDAO;
import com.sysnutriweb.dataModel.AlimentoDataModel;
import com.sysnutriweb.domain.Alimento;
import com.sysnutriweb.domain.AlimentoXDieta;
import com.sysnutriweb.domain.Dieta;
@ManagedBean(name = "MBDiet")
@SessionScoped
public class DietaBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<Dieta> listDiets;
private Dieta diet = new Dieta();
private DietaDAO dDao;
private LazyDataModel<AlimentoXDieta> foods;
private Alimento food = new Alimento();
@PostConstruct
public void begin() {
dDao = new DietaDAO();
listDiets = dDao.loadAll();
}
public void getDietSelected(ActionEvent e) {
diet = (Dieta) e.getComponent().getAttributes().get("selectedDiet");
List<AlimentoXDieta> foods = new ArrayList<>();
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));
}
this.foods = new AlimentoDataModel(foods);
}
public void getFoodSelected(ActionEvent e) {
food = (Alimento) e.getComponent().getAttributes().get("selectedFood");
}
// Getters and Setters
Index.xhtml
<f:metadata>
<f:param name="selectedDiet" value="#{MBDiet.diet}" />
</f:metadata>
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Menu de dietas SysNutri</title>
</h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit position="north" size="100" resizable="false"
closable="false" collapsible="false">
<center>
<h:outputText value="Sistema de Nutrição SYSNUTRIWEB" />
</center>
</p:layoutUnit>
<p:layoutUnit position="center">
<h:form id="formTable">
<p:dataTable emptyMessage="Nenhum dado cadastrado!"
paginator="false" id="tbAllDiets" value="#{MBDiet.listDiets}"
var="diet">
<p:column headerText="Nome" id="nome">
<p:outputLabel value="#{diet.nomeDieta}" />
</p:column>
<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 immediate="false" icon="ui-icon-info"
id="btnInfo" update=":formInfoTable"
actionListener="#{MBDiet.getDietSelected}"
oncomplete="PF('dlgFoods').show()">
<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>
</p:layout>
<!-- Dialogs -->
<!-- Diet info -->
<p:dialog header="Alimentos da dieta selecionada" widgetVar="dlgFoods"
closable="true" modal="true" showEffect="explode" id="dialogFoods"
resizable="false" draggable="false" appendTo="@(body)">
<h:form id="formInfoTable">
<center>
<p:outputLabel id="diet" value="Dieta #{MBDiet.diet.nomeDieta}"
style="font-weight: bold;" />
</center>
<p:dataTable emptyMessage="Nenhum dado cadastrado!" paginator="true"
id="tbFoodsDiet" value="#{MBDiet.foods}" var="food" rows="4"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="4,5,6,7,8,9,10" lazy="true">
<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 immediate="false" 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 alimentos presentes nesta dieta"
position="top" />
</center>
</p:column>
</p:dataTable>
</h:form>
</p:dialog>
<!-- Second Dialog -->
Github: https://github.com/SakamotoLfe/SysNutriWebV0
Vdieo: Coming soon.