How to ajax update an item in the footer of a PrimeFaces dataTable?

后端 未结 1 1650
一个人的身影
一个人的身影 2020-12-10 00:05

This is a visual of the table I have:

+---------+------------+-------------------+
| Header1 |  Header2   | Header3           |
+---------+------------+----------         


        
相关标签:
1条回答
  • 2020-12-10 00:19

    You can't really update the values of the table from within the table. However, you can use p:remoteCommand to achieve this. Also, I would probably use the cellEdit event on the whole table (instead of a single p:ajax on your pe:inputNumber). The overall result would be:

    <p:remoteCommand name="refreshFooter" update=":formName:outputTotal"/>
    ...
    <p:dataTable id="myTable" value="#{myBean.someList}" var="currentItem">
    
        <p:column headerText="Header1">
            <h:outputText value="#{currentItem.name}" />
        </p:column>
    
        <p:column headerText="Header2">
            <pe:inputNumber value="#{currentItem.inputVal}">
                <p:ajax event="change" listener="#{myBean.changeListener}" process="@this"
                    oncomplete="refreshFooter();" update="outputVal"/>
            </pe:inputNumber>
        </p:column>
    
        <p:column headerText="Header3">
            <h:outputText id="outputVal" value="#{currentItem.outputVal}" />
        </p:column>
    
        <f:facet name="footer">
            Total:
            <h:outputText id="outputTotal" value="#{myBean.total}" />
        </f:facet>
    
    </p:dataTable>
    

    Please note that you might need to add partialSubmit="true" to the p:ajax, this way you only submit the component's data. If you like the cellEdit event idea, you need to make the dataTable editable, and add the following in your p:dataTable, and get rid of the p:ajax inside your pe:inputNumber

    <p:ajax event="cellEdit" partialSubmit="true" listener="#{myBean.onCellEdit}" 
        process="@this" oncomplete="refreshFooter();"/>
    

    Good luck!

    0 讨论(0)
提交回复
热议问题