如何在 primefaces 中进行条件轮询?

How to do conditional polling in primefaces?

我想在 Primefaces(版本:6.0.0)中进行条件轮询。

轮询代码如下:

<p:poll  interval="20" update="liveChart,chrtTC_Cnt_Status,chrtStatus" />

例如,我是现场调用状态。如果状态值为“In-Progress”,则只应开始轮询。否则它不应该进行轮询。

这是我的字段代码,如果此值更改为 'In-Progress' 则轮询应该开始,否则不会

<p:outputLabel value="Status:" />
<p:outputLabel id="lblCurrentStatus" value="#{backingBean.status}" />

这是我的 Xhtml 代码

<h:panelGrid columns="2" width="400">

                    <p:outputLabel for="lblStudentName" value="Select Other StudentName: " />
                    <p:selectOneMenu id="lblStudentName" style="width:250px" 
                        value="#{backingBean.selectedStudent}"
                        panelStyle="width:180px" effect="fade" filter="true"
                        filterMatchMode="startsWith">
                        <f:selectItem itemLabel="Select One" itemValue=""
                            noSelectionOption="true" />
                        <f:selectItems value="#{backingBean.studentItemList}" />
                        <p:ajax
                            listener="#{backingBean.OnChangeOtherStudentDropDown}"
                            update=":idForm:tabStatus:p1,:idForm:tabStatus:p4,growl"
                            process="@form" />
                    </p:selectOneMenu>


                <p:tabView id="tabStatus">
            <p:tab title="Status" id="idStatusTab">
                <h:panelGrid columns="3" cellpadding="10" id="p1">
                    <p:growl id="growl" showDetail="true" />
                    <p:ajaxStatus onstart="PF('statusDialog').show()"
                        onsuccess="PF('statusDialog').hide()" />

                    <p:dialog widgetVar="statusDialog" draggable="false"
                        closable="false" resizable="false" showHeader="false">
                        <h:graphicImage value="/images/ajax-loader.gif" />
                    </p:dialog>
                    <h:panelGrid columns="2" cellpadding="5" id="p2">
                        <p:outputLabel value="Failed:" />
                        <p:outputLabel id="lblCurrentFailed"
                            value="#{backingBean.intCurrentFailedStudent}" />
                        <p:outputLabel value="Processed:" />
                        <p:outputLabel id="lblCurrentProcess"
                            value="#{backingBean.intCurrentPassedStudent}" />
                        <p:outputLabel value="Status:" />
                        <p:outputLabel id="lblCurrentStatus"
                            value="#{backingBean.status}" />

                    </h:panelGrid>
                    <h:panelGrid columns="2" cellpadding="10" id="p3">

                        <p:chart type="pie" model="#{backingBean.pieModel1}"
                            rendered="#{not empty backingBean.pieModel1}"
                            id="chrtStatus" style="width:300px;height:200px">
                        </p:chart>
                        <br />
                    </h:panelGrid>
                </h:panelGrid>


                <h:panelGrid columns="3" cellpadding="10" id="p4">

                <p:poll  interval="20" update="liveChart,chrtTC_Cnt_Status,chrtStatus" />
                    <p:chart type="line"
                        model="#{backingBean.lineCurrentLineChart}" 
                        rendered="#{not empty backingBean.lineCurrentLineChart}"
                        id="liveChart" style="height:500px;width:500px" />


                    <p:chart type="bar"
                        model="#{backingBean.barStatusCountStudent}"
                        rendered="#{not empty backingBean.barStatusCountStudent}"
                        id="chrtTC_Cnt_Status" style="width:500px;height:500px">
                         <p:ajax event="itemSelect" listener="#{backingBean.itemSelect}"  />
                    </p:chart>
                </h:panelGrid>
            </p:tab>

        </p:tabView>
</h:form>

正在运行,

<h:panelGrid columns="1" id="ID_polling" rendered="#{BackingBean.status eq 'In-Progress'}">    
          <p:poll  interval="20" update="liveChart,chrtTC_Cnt_Status,chrtStatus" />    
</h:panelGrid>

我写这个Panel时,只有status = 'In-Progress'时Panel才会显示,否则不显示。

再次感谢,

解决方案一:
将您的 p:poll 包裹在 h:panelGrouph:panelGrid 内,在面板上设置 rendered="#{backingBean.status eq 'In-Progress'}" 并在状态更改时更新面板,这将重置您的轮询。

方案二:
如果您想手动启动和停止轮询,则需要设置 autoStart="false" 并在 p:poll 上添加 widgetVar="statusPoll" 属性,并在更改时调用 PF('statusPoll').start();PF('statusPoll').stop();状态字段的值。