3

I have a JSF datatable which has 8 columns. Last 4 columns are numerical valued columns. Lets say my datatable brings 20 rows result. I want to add last row which only contains last 4 column's fields and contain sum of 20 rows' corresponding values. I want to add last row with Facelets code. How can I do that ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Carlos
  • 167
  • 3
  • 10

1 Answers1

1

If you have one table as this one, you can add this footer facet:

<h:dataTable id="table1" value="#{shoppingCartBean.items}" var="item"
             border="1">
    <f:facet name="header">
        <h:outputText value="Your Shopping Cart" />
    </f:facet>
    <h:column>
        <f:facet name="header">
            <h:outputText value="Item Description" />
        </f:facet>
        <h:outputText value="#{item.description}" />
    </h:column>
    <h:column>
        <f:facet name="header">
            <h:outputText value="Price" />
        </f:facet>
        <h:outputText value="#{item.price}" />
    </h:column>
    <f:facet name="footer">
        <h:panelGroup style="display:block; text-align:right">
            <h:outputText value="Total 1: #{shoppingCartBean.total1}" />
            <h:outputText value="Total 2: #{shoppingCartBean.total2}" />
            <h:outputText value="Total 3: #{shoppingCartBean.total3}" />
            <h:outputText value="Total 4: #{shoppingCartBean.total4}" />
        </h:panelGroup>
    </f:facet>
</h:dataTable>

Then you should code the total functions in your backing bean:

@ManagedBean
public class ShoppingCartBean {
    ...
    public int total1() {
        // Do the sum of all elements from first column of table as you wish....

        return result;
    }

    public int total2() {
        // Do the sum of all elements from second column of table as you wish....

        return result;
    }
}

If you prefer a more elaborated and reusable solution, you can create your own EL function as this:

<f:facet name="footer">
    <h:panelGroup style="display:block; text-align:right">
        <h:outputText value="Total 1: #{func:calculateTotal(shoppingCartBean.items, 4}" />
        <h:outputText value="Total 2: #{func:calculateTotal(shoppingCartBean.items, 5}" />
        <h:outputText value="Total 3: #{func:calculateTotal(shoppingCartBean.items, 6}" />
        <h:outputText value="Total 4: #{func:calculateTotal(shoppingCartBean.items, 7}" />
    </h:panelGroup>
</f:facet>

For this solution you can check BalusC description on how to create a custom el function

Regards,

Community
  • 1
  • 1
Rodmar Conde
  • 956
  • 1
  • 12
  • 24