单击Primefaces按钮和Load Filtered Datatable

时间:2018-06-08 16:50:59

标签: java jsf primefaces datatable

我想要的:根据用于计算此按钮中显示的数量的参数,单击一个按钮并加载已过滤的数据表(位于另一个xhtml文件中)。

在index.jsf中有一个带有3个按钮的表单,它显示通过控制器类查询到数据库获得的表中的寄存器数量:

<h:form id="kpiForm">
    <p:panel id="kpiPanel" >
        <p:button id="bt1" value="#{demandasController.amountX}" />
        <p:button id="bt2" value="#{demandasController.amountY}" />
        <p:button id="bt3" value="#{demandasController.amountZ}" outcome="/app/demandas/index" /> <!-- clicking this button loads the page with the datatable, but not filtered.
    </p:panel>
</h:form>

控制器类具有返回金额值的属性(只是为了简短,我只显示与金额X相关的方法。):

public String getAmountX() {
    return LoadAmountX().toString();
}

//setter ommited

private List<Demandas> LoadAmountX() {
        List<Demandas> listAmountX = DemandasFacade.findAmountX();
        return listAmountX;
    }

在我的门面我有:

public List<Demandas> findAmountX() {
        return (List<Demandas>) getEntityManager().createNamedQuery("Demandas.findAmountX", Demandas.class).getResultList();
    }

在我的实体类中,我有命名查询:

@NamedQuery(name = "Demandas.findAmountX", query = "SELECT COUNT(d.id) FROM Demandas d WHERE d.situacao.idSituacao = 3"),

在datatable中,我有一个包含这些参数值的列:

<ui:composition>
<h:form id="DemandasListForm">    
    <p:panel id="PanelListForm" header="#{adeBundle.ListDemandasTitle}">

        <p:dataTable id="datalist"
                     value="#{demandasController.items}"
                     rowKey="#{item.id}"
                     var="item"
                     selection="#{demandasController.selected}"
                     filteredValue="#{demandasController.filteredDemandas}"
                     widgetVar="demandasTable">

            <!-- some columns ommited -->

            <p:column sortBy="#{item.id}" filterBy="#{item.id}" >
                <f:facet name="header">
                    <h:outputText value="id"/>
                </f:facet>
                <h:outputText value="#{item.id}"/>
            </p:column>    

            <p:column sortBy="#{item.status.status}" filterBy="#{item.status.status}" filterMatchMode="in" >
                <f:facet name="filter">
                    <p:selectCheckboxMenu label="Status" onchange="PF('demandasTable').filter()" >
                        <f:selectItems value="#{demandasController.statusListCombo}" />
                    </p:selectCheckboxMenu>
                </f:facet>
                <h:outputText value="#{item.status.status}"/>
            </p:column>    
        </p:dataTable>

    </p:panel>
</h:form>
</ui:composition>

我已经研究过尝试解决它,但我不知道该怎么做:

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

经过长期研究,找到了使用JQuery的简单解决方案:

在某些 index.xhtml 中:

<ui:composition>
  <h:form id="kpiForm">
    <p:panel id="kpiPanel" >
      <p:commandButton value="filter 1" action="/app/index" oncomplete=" $('#MyFormID\\:MyDatatableID\\:BoxID\\:1').click(); PF('wv').filter();" />
      <p:commandButton value="filter 2" action="/app/index" oncomplete=" $('#MyFormID\\:MyDatatableID\\:BoxID\\:2').click(); PF('wv').filter();" />
      <p:commandButton value="filter 3" action="/app/index" oncomplete=" $('#MyFormID\\:MyDatatableID\\:BoxID\\:3').click(); PF('wv').filter();" />
    </p:panel>
  </h:form>
</ui:composition>

another.xhtml 中:

<!-- some code ommited -->
<ui:composition [...]>
  <h:form id="OutsideFormID">
    <p:panel id="MyPanelID" header="Some Title">

      <p:dataTable id="MyDatatableID"
    value="#{datatableController.items}"
    rowKey="#{item.id}"
    var="item"
    selection="#{datatableController.selected}"
    filteredValue="#{datatableController.filteredDemandas}"
    widgetVar="wv">

          <p:column id="MyColumnID" sortBy="#{item.status.statusName}" filterBy="#{item.status.statusName}" filterMatchMode="in" >
            <f:facet name="header">
                <h:outputText value="STATUS"/>
            </f:facet>
            <f:facet name="filter">
            <p:selectCheckboxMenu id="BoxID" widgetVar="BoxWV" label="status" onchange="PF('wv').filter()">
                <f:selectItems id="ComboID" value="#{datatableController.statusListCombo}" />
            </p:selectCheckboxMenu>
            </f:facet>
            <h:outputText value="#{item.status.statusName}"/>
        </p:column>
      </p:dataTable>
    </p:panel>
  </h:form>
</ui:composition>