在 cellEdit p:ajax 事件中消失 p:DataTable

Disappearing p:DataTable on cellEdit p:ajax event

我使用 NetBeans 8.2、GlassFish 5.0、PrimeFaces 5.0。 我创建了一个包含单元格 editable 数据 table 的页面。当我在 ajax 事件后对一个单元格进行修改时,整个数据 table 被修改后的值替换。就像 ajax 响应不会发回整个 table 内容一样。我该如何更正它?

小面孔:

    <h:form id="frmDemands">
        <p:growl id="msgs" showDetail="true"/>
        <div>
        <p:dataTable 
            id="dtblDemands" 
            value="#{demandUtility.demands}" 
            var="demand" 
            editable="true" 
            editMode="cell" 
            paginator="true" 
            paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
            rowsPerPageTemplate="5,10,15">
            <p:ajax event="cellEdit" listener="#{applicantBean.onCellEdit}" update=":frmDemands :frmDemands:msgs"/>
            <f:facet name="header">Demands</f:facet>
            <p:column headerText="ID">
                <h:outputText value="#{demand.id}"/>
            </p:column>
            <p:column headerText="Amount">
                <p:cellEditor>
                    <f:facet name="output"><h:outputText value="#{demand.amount}"/></f:facet>
                    <f:facet name="input"><p:inputText value="#{demand.amount}"/></f:facet>
                </p:cellEditor>
            </p:column>
            <p:column headerText="State">
                <h:outputText value="#{demand.state}"/>
            </p:column>
            <p:column>
                <p:commandButton value="Delete" rendered="#{applicantBean.isDemandDeleteable( demand )}" actionListener="#{applicantBean.onDeleteDemand( demand.id )}" update="@form"/>
            </p:column>
        </p:dataTable>
        </div>
    </h:form>

支持 bean 处理 cellEdit 事件:

@Named
@RequestScoped
@Data
public class ApplicantBean
{

  @Inject
  private DemandUtility demandUtility;

  ...

  public void onCellEdit( CellEditEvent event_ )
  {
    int rowIndex = event_.getRowIndex();
    double newValue = (Double) event_.getNewValue();
    Demand demand = demandUtility.getDemands().get( rowIndex );
    demand.setAmount( newValue );

    Event event = new Event( demandUtility.getNextEventId(), new Date(), "Demand (" + demand.getId() + ") modified! New amount: " + newValue );
    demandUtility.getEvents().add( event );
  }

  ...

}

当我尝试将 @parent 值设置为 ajax 事件的 update 属性时,dataTable 也会消失。

有人可以帮帮我吗?

创建更新您的 table 并将您的范围更改为 @ViewScoped 它对我有用。

<h:form id="frmDemands">
    <p:growl id="msgs" showDetail="true"/>
    <p:remoteCommand name="rcCellEditUpdate" 
                     update=":frmDemands:dtblDemands :frmDemands:msgs" />
    <div>
        <p:dataTable 
            id="dtblDemands" 
            value="#{demandUtility.demands}" 
            var="demand" 
            editable="true" 
            editMode="cell" 
            paginator="true" 
            paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
            rowsPerPageTemplate="5,10,15">
            <p:ajax event="cellEdit" listener="#{applicantBean.onCellEdit}" oncomplete="rcCellEditUpdate();"/>
            <f:facet name="header">Demands</f:facet>
            <p:column headerText="ID">
                <h:outputText value="#{demand.id}"/>
            </p:column>
            <p:column headerText="Amount">
                <p:cellEditor>
                    <f:facet name="output"><h:outputText value="#{demand.amount}"/></f:facet>
                    <f:facet name="input"><p:inputText value="#{demand.amount}"/></f:facet>
                </p:cellEditor>
            </p:column>
            <p:column headerText="State">
                <h:outputText value="#{demand.state}"/>
            </p:column>
            <p:column>
                <p:commandButton value="Delete" rendered="#{applicantBean.isDemandDeleteable( demand )}" actionListener="#{applicantBean.onDeleteDemand( demand.id )}" update="@form"/>
            </p:column>
        </p:dataTable>
    </div>
</h:form>