在Facelets中向DataTable行添加最后一行

3

我有一个包含8列的JSF数据表,其中最后4列是数字值列。假设我的数据表带来了20行结果。我想添加最后一行,它只包含最后4列的字段,并包含20行对应值的总和。我想使用Facelets代码添加最后一行。我该怎么做?


<f:facelet name="footer"> 对我很有帮助。谢谢。 - Carlos
1个回答

1
如果您有一张像这样的表格,您可以添加这个“页脚”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>

那么您应该在后端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;
    }
}

如果您更喜欢一种更详细且可重用的解决方案,你可以创建您自己的EL函数,如下所示:
<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>

对于这个解决方案,您可以查看BalusC如何创建自定义el函数的描述

敬礼


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接