The h:dataTable tag iterates over data to create an HTML table. Here's how you use it:
<h:dataTable value='#{items}' var='item'> <h:column> <%-- left column components --%> <h:outputText value='#{item.propertyName}'/> </h:column> <h:column> <%-- next column components --%> <h:outputText 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: 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 over 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's 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. Let's start our h:dataTable exploration with a simple table. |