|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.sas.Component | +--com.sas.servlet.beans.BaseTransformation | +--com.sas.servlet.beans.BaseTable | +--com.sas.servlet.beans.html.Table
Transformation bean for creating HTML tables. See the BaseTable documentation for additional information.
The following example illustrates the use of the Table Transformation Bean. A new model that implements com.sas.table.StaticTableInterface will be created. In this case a com.sas.models.SimpleTable object will be used, but a com.sas.sasserver.dataset.DataSetInterface model can be used as well. Once the model is created the Table Transformation Bean can be used to represent the table (the HTML-specific Table Transformation Bean will be used).
// Create an instance of the SimpleTable object com.sas.models.SimpleTable weather = new com.sas.models.SimpleTable(); weather.initialize();
// Define the columns for the model
// Add the column for the day weather.addColumn(1, java.lang.String.class, null); weather.setColumnLabel(1, "Day");
// Add the column for the daily forecast weather.addColumn(2, java.lang.String.class, null); weather.setColumnLabel(2, "Forecast");
// Add the column for the temperature weather.addColumn(3, java.lang.Double.class, null); weather.setColumnLabel(3, "High temperature");
// Add the rows for Friday, Saturday, and Sunday weather.addRow(1); weather.setCell(1, 1, "Friday"); weather.setCell(1, 2, "Sunny"); weather.setCell(1, 3, new Double(76.2));
weather.addRow(2); weather.setCell(2, 1, "Saturday"); weather.setCell(2, 2, "Cloudy"); weather.setCell(2, 3, new Double(57.7));
weather.addRow(3); weather.setCell(3, 1, "Sunday"); weather.setCell(3, 2, "Stormy"); weather.setCell(3, 3, new Double(52.3));
Now that the model has been created the Table Transformation Bean can be used to read the data from the model and format the 2-dimensional table
Example: Use the default properties to format a table
JSP Page:
<html> <body> <% // Create the weather model (shown above) ...
// Create the Table Transformation Bean com.sas.servlet.beans.TableInterface table = new com.sas.servlet.beans.html.Table();
// Set the model table.setModelInterface(weather);
// Output the table table.write(out); %> </body> </html>
HTML output:
<html> <body> <TABLE> <tr><td>Friday</td><td>Sunny</td><td>76.2</td></tr> <tr><td>Saturday</td><td>Cloudy</td><td>57.7</td></tr> <tr><td>Sunday</td><td>Stormy</td><td>52.3</td></tr> </TABLE> </body> </html>
Browser Table:
Friday | Sunny | 76.2 |
Saturday | Cloudy | 57.7 |
Sunday | Stormy | 52.3 |
Example: Use properties to format a table. Shows how the column heading is set to use the label name for each column.
JSP Page:
<html> <body> <% // Create the weather model (shown above) ...
// Create the Table Transformation Bean com.sas.servlet.beans.TableInterface table = new com.sas.servlet.beans.html.Table();
// Set the model table.setModelInterface(weather);
// Set properties table.setUseColumnHeadings(true); table.setColumnHeadingColumnFormat("#META_LABEL# "); table.setBorderWidth(1);
// Output the table table.write(out); %> </body> </html>
HTML output:
<html> <body> <TABLE BORDER=1> <tr><th>Day</th><th>Forecast</th><th>High temperature</th></tr> <tr><td>Friday</td><td>Sunny</td><td>76.2</td></tr> <tr><td>Saturday</td><td>Cloudy</td><td>57.7</td></tr> <tr><td>Sunday</td><td>Stormy</td><td>52.3</td></tr> </TABLE> </body> </html>
Browser Table:
Day | Forecast | High temperature |
---|---|---|
Friday | Sunny | 76.2 |
Saturday | Cloudy | 57.7 |
Sunday | Stormy | 52.3 |
In the previous examples the write() method created the entire table, including the table header, column headings, all of the rows, and the table footer. If more control is necessary it is possible to invoke each of these operations separately:
Example: Create a table by iterating through the model and setting the column format for each row. This example will alternate the background color of each row
JSP Page:
<html> <body> <% // Create the weather model (shown above) ...
// Create the Table Transformation Bean com.sas.servlet.beans.TableInterface table = new com.sas.servlet.beans.html.Table();
// Set the model table.setModelInterface(weather);
// Set attributes table.setCellSpacing(0); table.setCellPadding(5);
// Create an array of colors to use. Must escape the '#' character String[] colors = new String[2]; colors[0] = "\\#FFFFFF"; colors[1] = "\\#CCE5FF";
// Keep track of the row number int rowNumber = 0;
// Write the table header table.writeTableHeader(out);
// Loop through the table until there are no more rows while (true) {
// Get the rowid, mod it with the number of colors, and set the // background color String bgcolor = colors[rowNumber % colors.length];
// Set the column format for all columns table.setColumnFormat("<td bgcolor=\"" + bgcolor + "\">##</td>");
// Increment the row number rowNumber++;
// Write an individual row. The row number is 1-based. try { table.writeRow(rowNumber, out); } catch (java.io.EOFException ex) { // EOFException thrown when the end of file has been reached break; } }
// Write the table footer table.writeTableFooter(out); %> </body> </html>
HTML output:
<html> <body> <TABLE CELLSPACING=0 CELLPADDING=5> <tr><td bgcolor="#FFFFFF">Friday</td><td bgcolor="#FFFFFF">Sunny</td><td bgcolor="#FFFFFF">76.2</td></tr> <tr><td bgcolor="#CCE5FF">Saturday</td><td bgcolor="#CCE5FF">Cloudy</td><td bgcolor="#CCE5FF">57.7</td></tr> <tr><td bgcolor="#FFFFFF">Sunday</td><td bgcolor="#FFFFFF">Stormy</td><td bgcolor="#FFFFFF">52.3</td></tr> </TABLE> </body> </html>
Browser Table:
Friday | Sunny | 76.2 |
Saturday | Cloudy | 57.7 |
Sunday | Stormy | 52.3 |
BaseTable
Field Summary | |
static String |
DEFAULT_BEGIN_ROW_FORMAT
|
static String |
DEFAULT_COLUMN_HEADING_FORMAT
|
static String |
DEFAULT_DATA_FORMAT
|
static String |
DEFAULT_END_ROW_FORMAT
|
Fields inherited from class com.sas.servlet.beans.BaseTable |
MARKER,
MARKER_ESC,
MARKER_META,
MARKER_META_ROWID |
Constructor Summary | |
Table()
Construct a new Table object |
|
Table(String name,
ModelInterface model)
Construct a new Table object |
Method Summary | |
String |
getDefaultBeginRowFormat()
Gets the default format to begin a new table row. |
String |
getDefaultColumnFormat()
Gets the default format to be applied to each data element in a row. |
String |
getDefaultColumnHeadingFormat()
Gets the default format to be applied to each column heading. |
String |
getDefaultEndRowFormat()
Gets the default format to end a table row. |
void |
writeTableFooter(PrintWriter out)
Writes the table footer |
void |
writeTableHeader(PrintWriter out)
Writes the table header |
Methods inherited from class com.sas.servlet.beans.BaseTransformation |
getCustomAttributes,
getDescription,
getName,
getParent,
getRequest,
getResponse,
setCustomAttributes,
setDescription,
setName,
setParent,
setRequest,
setResponse,
toString,
write,
write,
write |
Methods inherited from class java.lang.Object |
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
Field Detail |
public static final String DEFAULT_BEGIN_ROW_FORMAT
public static final String DEFAULT_DATA_FORMAT
public static final String DEFAULT_END_ROW_FORMAT
public static final String DEFAULT_COLUMN_HEADING_FORMAT
Constructor Detail |
public Table()
public Table(String name, ModelInterface model)
name
- The field nameMethod Detail |
public String getDefaultBeginRowFormat()
public String getDefaultColumnFormat()
public String getDefaultEndRowFormat()
public String getDefaultColumnHeadingFormat()
public void writeTableHeader(PrintWriter out) throws IOException
out
- The output streampublic void writeTableFooter(PrintWriter out) throws IOException
out
- The output stream
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |