A Simple Table

Chapter 5. Data Tables

Topics in This Chapter

  • "The Data Table Tag h:dataTable" on page 171

  • "A Simple Table" on page 173

  • "Headers, Footers, and Captions" on page 178

  • "JSF Components" on page 182

  • "Editing Table Cells" on page 186

  • "Styles" on page 189

  • "Database Tables" on page 191

  • "Table Models" on page 197

  • "Scrolling Techniques" on page 214

Classic web applications deal extensively in tabular data. In the days of old, HTML tables were preferred for that task, in addition to acting as page layout managers. That latter task has, for the most part, been subsequently rendered to CSS, but displaying tabular data is still big business.

This chapter discusses the h:dataTable tag, a capable but limited component that lets you manipulate tabular data.

Note

The h:dataTable tag represents a capable component/renderer pair. For example, you can easily display JSF components in table cells, add headers and footers to tables, and manipulate the look and feel of your tables with CSS classes. However, h:dataTable is missing some high-end features that you might expect out of the box. For example, if you want to sort table columns, you will have to write some code to do that. See "Sorting and Filtering" on page 203 for more details on how to do that.


The Data Table Tag h:dataTable

The h:dataTable tag iterates over data to create an HTML table. Here is how you use it:

  <h:dataTable value='#{items}' var='item'>      <h:column>         <%-- left column components --%>         <h:output_text value='#{item.propertyName}'/>      </h:column>      <h:column>         <%-- next column components --%>         <h:output_text value='#{item.anotherPropertyName}'/>      </h:column>      <%-- add more columns, as desired --%>   </h:dataTable>

The value attribute represents the data over which h:dataTable iterates; that data must be one of the following:

  • A Java object

  • An array

  • An instance of java.util.List

  • An instance of java.sql.ResultSet

  • An instance of javax.servlet.jsp.jstl.sql.Result

  • An instance of javax.faces.model.DataModel

As h:dataTable iterates, it makes each item in the array, list, result set, etc., available within the body of the tag. The name of the item is specified with h:dataTable's var attribute. In the preceding code fragment, each item (item) of a collection (items) is made available, in turn, as h:dataTable iterates through the collection. You use properties from the current item to populate columns for the current row.

You can also specify any Java object for h:dataTable's value attribute, although the usefulness of doing so is questionable. If that object is a scalar (meaning it is not a collection of some sort), h:dataTable iterates once, making the object available in the body of the tag.

The body of h:dataTable tags can contain only h:column tags; h:dataTable ignores all other component tags. Each column can contain an unlimited number of components in addition to optional header and footer components.

h:dataTable pairs a UIData component with a Table renderer. That combination provides robust table generation that includes support for CSS styles, database access, custom table models, and more. We start our h:dataTable exploration with a simple table.



Core JavaServerT Faces
Core JavaServer(TM) Faces (2nd Edition)
ISBN: 0131738860
EAN: 2147483647
Year: 2004
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net