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

时间:2018-07-11 10:48:16

标签: jsf primefaces

我使用NetBeans 8.2,GlassFish 5.0,PrimeFaces 5.0。 我创建了一个带有单元格可编辑数据表的页面。当我在ajax事件之后对一个单元格进行修改时,整个数据表都被修改后的值所代替。就像ajax response不会发回整个表内容一样。我该如何纠正?

方面:

    <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 );
  }

  ...

}

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

请问有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

创建一个更新表的表,并将范围更改为@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>