P: total row count

I have p:dataTable

one similar to showcase . Unfortunately, the storefront uses randomly generated values โ€‹โ€‹for p:summaryRow

.

When referring to a storefront, imagine that the car had a price tag.

How do I summarize the price for the cars grouped by the specified column to display it in the final row?

+3


source to share


3 answers


I have the same problem with you and I found the correct usage on the blog summaryRow websites from the surfaces forum

this is an example showing the correct calculation of cost, counting ...



The Listener gets a group property, which is the sortBy value that you can use to calculate totals or any other operation.

<p:summaryRow listener="#{backingBean.calculateTotal}"> 
                        <p:column colspan="3" style="text-align:right"> 
                            Total: 
                        </p:column> 

                        <p:column> 
                            #{backingBean.total}
                        </p:column> 
                    </p:summaryRow>

/* listener method in your bean */
public void calculateTotal(Object o) {
    this.total = 0.0d;
    String name = "";
    if(o != null) {
        if(o instanceof String) {
            name = (String) o;
            for(MyRowObject p : (List<MyRowObject>) dataTableModel.getWrappedData()) { // The loop should find the sortBy value rows in all dataTable data.
                switch(sortColumnCase) { // sortColumnCase was set in the onSort event
                    case 0:
                        if(p.getcolumn0data().getName().equals(name)) {
                            this.total += p.getcolumn0data().getValue();
                        }
                        break;
                    case 1:
                        if(p.getcolumn1data().getName().equals(name)) {
                            this.total += p.getcolumn1data().getValue();
                        }
                        break;
                }
            }
        }
    }
}

      

+3


source


this is my decision:

<p:dataTable var="elem" value="#{unitSubStatusChart.resultList}" tableStyleClass="app-table-auto">
    <et:defaultPaginator />

    <p:column headerText="#{bundle['class']}" sortBy="#{elem.dtype}">
        <e:outputIconText icon="#{icons[elem.dtype]}" value="#{bundle[elem.dtype]}" />
    </p:column>
    <p:column headerText="#{bundle['status']}" sortBy="#{elem.status}">
        <h:outputText value="#{bundle[elem.status]}" />
    </p:column>
    <p:column headerText="#{bundle['subStatus']}" sortBy="#{elem.subStatus}">
        <h:outputText value="#{bundle[elem.subStatus]}" />
    </p:column>
    <p:column headerText="#{bundle['count']}">
        <h:outputText value="#{elem.count}" />
    </p:column>

    <p:summaryRow listener="#{unitSubStatusChart.onSummaryRow}">
        <p:column colspan="3" style="text-align:right">
            <h:outputText value="#{bundle.total}:" />
        </p:column>
        <p:column>
            <!-- !!! NOTE THIS VALUE EXPRESSION !!! -->
            <h:outputText value="#{component.parent.attributes.total}" />
        </p:column>
    </p:summaryRow>
</p:dataTable>

      

and this is a listener:



public void onSummaryRow(Object filter)
{
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ELContext elContext = facesContext.getELContext();
    ELResolver elResolver = elContext.getELResolver();

    DataTable table = (DataTable) UIComponent.getCurrentComponent(facesContext);

    UIColumn sortColumn = table.getSortColumn();
    ValueExpression expression = sortColumn.getValueExpression("sortBy");
    ValueReference reference = ValueExpressionAnalyzer.getReference(expression);
    String property = (String) reference.getProperty();

    int total = 0;
    List<?> rowList = (List<?>) table.getValue();
    for(Object row : rowList)
    {
        Object value = elResolver.getValue(elContext, row, property);
        if(filter.equals(value))
        {
            // THIS IS THE ONLY POINT TO CUSTOMIZE
            total += ((UnitResult) row).getCount();
        }
    }

    List<UIComponent> children = table.getSummaryRow().getChildren();
    UIComponent column = children.get(children.size() - 1);
    column.getAttributes().put("total", total);
}

      

This listener has no external references : it does not depend on previous onSort events / listeners and does not need any field in the bean.

+2


source


I will suggest a possible way to do this, perhaps not the most perfect one, but I believe it will help.

These steps:

1 - declare a class attribute to summarize the total group value:

...
@Named(value = "myBean")
@YourScope

public class InscricaoBean implements Serializable {
...

//in this example i will use a int type

private int acumulatorAux = 0;
...

      

2 - Create a function to return the total value for each group:

...
public int getAcumulatedValue() {
        int aux = acumulatorAux ;
        acumulatorAux = 0;
        return aux;
    }
...

      

3 - Create a method to calculate values โ€‹โ€‹for each group:

...
public void acumulateValue(int value)
    {
        acumulatorAux += value;
    }
...

      

4 - In the JSF page, you need to call the method acumulateValue

for each line, for example:

...
<p:dataTable value="#{myBean.getList()}" var="testVar">
       <p:column headerText="Header" sortBy="#{testVar.name}">
            #{inscricaoBean.acumulateValue(testVar.value)}
            <h:outputText value="#{testVar.name}" />
       </p:column>
 ...

      

5 - And then you can have summaryRow

as:

    ...
    <p:summaryRow>
        <p:column colspan="1" style="text-align:right">
              <h:outputText value="Total:" />
        </p:column>
        <p:column>
              <h:outputText value="#{inscricaoBean.getAcumulatedValue()}" />
        </p:column>
    </p:summaryRow>
    ...

      

Well, this is a rough way to solve this problem. I think you can improve, but this was the first time I see your question.

Hope it helps.

+1


source







All Articles