Using subTable for grouping

A helper component, subTable can be used to group row data inside a table.

How to do it…

A basic definition of a table that contains subTable is given here:

<p:dataTable value="#{dataTableBean.boxers}" var="boxer">
  <f:facet name="header">
    Boxers
  </f:facet>

  <p:columnGroup type="header">
    <p:row>
      <p:column rowspan="2" headerText="Boxer" />
      <p:column colspan="2" headerText="Stats" />
    </p:row>
    <p:row>
      <p:column headerText="Wins" />
      <p:column headerText="Losses" />
    </p:row>
  </p:columnGroup>

  <p:subTable var="stats" value="#{boxer.stats}">
    <f:facet name="header">
      <h:outputText value="#{boxer.name}" />
    </f:facet>
    <p:column>
      <h:outputText value="#{stats.match}" />
    </p:column>
    <p:column>
      <h:outputText value="#{stats.win}" />
    </p:column>
    <p:column>
      <h:outputText value="#{stats.loss}" />
    </p:column>
    <p:columnGroup type="footer">
      <p:row>
        <p:column footerText="Totals: " 
          style="text-align:right"/>
        <p:column footerText="#{boxer.allWins}" />
        <p:column footerText="#{boxer.allLosses}" />
      </p:row>
    </p:columnGroup>
  </p:subTable>
</p:dataTable>

The columnGroup component is used to combine rows and columns in headers and footers of the table. The model used within the table is briefly described here:

public class Boxer {
  private String name;
  private List<Stat> stats = new ArrayList<Stat>();
}

public class Stat {
  private String match;
  private int win;
  private int loss;
}

A visual output of the table is given here:

How to do it…

PrimeFaces Cookbook Showcase application

This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.

The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter5/subTable.jsf.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.138.195