<p:datatable>的分页器未显示数据

时间:2018-11-26 15:59:30

标签: java hibernate primefaces datatable dialog

我是这个社区的新手,但我确实需要帮助! 我正在编程一个应用程序,该应用程序将使用Hibernate 4.3(不带JPA)和Primefaces 8.2在Java桌面环境中运行,并通过Java Web在线运行。 我的问题是,我有一个带有dataTable的页面,此dataTable在每个寄存器上都包含一个按钮,以打开一个包含该寄存器更多信息的对话框。 在此对话框中,我有另一个dataTable,每个寄存器中都有另一个按钮。 这里有两个问题。

首先,当用户单击第一个对话框中的按钮时,将出现第二个对话框,但是它只是忽略了ActionListener。

第二,在第一个对话框中,当用户单击以转到dataTable的其他页面时,或者只是增加行数,数据就会消失!如果关闭并重新打开对话框,则该对话框将在您之前单击的页面中打开,并且数据将在那里。

我将发布问题的主要代码,github链接和视频,以使查找问题变得容易。

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:很快来。

0 个答案:

没有答案