|
Technical Note
Formatting Report Output in Oracle HTML DB Using Report Templates
Author: Raj Mattamal
Date: August 2004
In HTML DB, the layout of data displayed in report regions is ultimately controlled by report templates. In addition to facilitating basic formatting, however, report templates in HTML DB give application developers the opportunity to reference the data in a current row while determining how it is to be displayed. This ability to easily reference report data makes report templates in HTML DB a very powerful feature.
This Technical Note will provide an overview of report templates while illustrating how they can be used to achieve specific implementation goals.
Overview of HTML DB Report Templates
HTML DB offers two kinds of report templates: Report Column Templates and Report Row Templates. Report Column templates are the simpler of the two types; they specify the HTML to be used when rendering columns in a report row. Because each column in a report column template is handled the same way, these template types are sometimes called "Generic Column" templates. In the case of Report Row Templates, each column to be displayed must be referenced. Because row templates allow the developer to reference individual columns, even more control over look and feel is possible.
What follows is a set of five examples in which report templates are used to achieve specific report layouts. To follow along with the examples, log into an HTML DB workspace with access to a copy of the SCOTT.EMP table. The examples covered will include:
- Displaying Data Vertically as Opposed to Horizontally
- Displaying Single Records Across Multiple Rows
- Color-Coding Data
- Using Session State Values when Conditionally Rendering Report Row Buttons
- Controlling Text Wrapping in Lengthy Report Columns
Displaying data vertically as opposed to horizontally
Although report data is most often displayed using a single row for each record, developers will at times wish to display each column as its own row. This formatting gives the report a vertical look that's most often used when single records are being displayed at a time. The easiest way to achieve this look is to use the built-in "vertical" style report templates that are available for every report region in HTML DB. These templates include:
- default: vertical report, look 1 (exclude null columns)
- default: vertical report, look 1 (include null columns)
- default: vertical report, look 2 (exclude null columns)
- default: vertical report, look 2 (include null columns)
Selecting any one of these report templates from the Report Attributes page of an existing report region will format its data to render vertically. As opposed to using a built-in report template to achieve this look, however, let's create a column template that acts similarly. First, start by creating a very simple column template:
Creating a basic column template
- Log into HTML DB
- Click the Application Builder icon
- From the HTML DB Application Builder home, click the Templates standard tab
- In the "Report Templates:(Row and Column templates)" region, click on Create/Copy
- Choose "From Scratch" and click Next
- From the "Report Type" list of values, select "Generic Columns (column template)" and click Next>
- Name the new template "Simple Column", and click Create
To call your report template from a report region, navigate to the Report Attributes screen for your report region and select "Simple Column" in the "Report Template" field as shown in Figure 1, below.
|
| Figure 1: Changing the Report Template Selection for a Report Region |
To make this new template render data vertically, one simple change is necessary. First go to the "Edit Report Template" screen for the new "Simple Column" template by clicking the edit icon next to its name in the "Report Templates: (Row and Column templates)" region. A quick glance over this screen show that only three fields are populated:
- Before Rows = <table>
- Column Template 1 = <td>#COLUMN_VALUE#</td>
- After Rows = </table>
These, of course, are the most basic attributes necessary for a report column template to render data. To change the template to render data vertically, you would need to change the "Column Template 1" code to display one row per column value as well as the name of the current column. To address these concerns, update "Simple Column" as shown in Figure 2 by entering the pattern:
<tr><td>#COLUMN_HEADER#</td><td>#COLUMN_VALUE#</td></tr>
into the "Column Template 1" field.
|
| Figure 2: Modifying the HTML Pattern Used in a Report Column Template |
As explained in the attribute-level help for that "Column Template 1" field, the substitution string, #COLUMN_HEADER#, returns the name of the current column. Being generic in nature, your "Simple Vertical" template can be used to display the data from any HTML DB report region in a vertical fashion.
Displaying Single Records Across Multiple Rows
To display a single record across two or more rows, it is necessary to define a Report Row Template in which you define your line breaks. Say, for instance, you were creating a report on the EMP table in the SCOTT schema that used a query like this:
select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp
If you wanted to show the last three columns of the query on a second line for each returned row, you could do so by first creating a simple Report Row Template for this particular query (remember that Report Row Templates are generally specific to a query since the columns referenced need to match those of the query region).
Creating a basic row template
- From the HTML DB Application Builder home, click the "Templates" standard tab
- In the "Report Templates: (Row and Column templates)" region, click Create/Copy
- Choose "From Scratch" and click Next
- Select "Named Column (row template)" from the "Report Type" list of values.
- As shown in Figure 2, enter the HTML pattern to be used for each row into the "Template 1" field:
<tr>
<td>#EMPNO#</td>
<td>#ENAME#</td>
<td>#JOB#</td>
<td>#MGR#</td>
<td>#HIREDATE#</td>
<td>#SAL#</td>
<td>#COMM#</td>
<td>#DEPTNO#</td>
</tr>
- Click Next
- Name the new template "Simple Row" and click Create
Now that you have a Simple Row template, copy it and modify that copy to become the split row template that we're after:
Copy the "Simple Row" template and name it "Split Row"
- From the HTML DB Application Builder home, click the "Templates" standard tab
- In the "Report Templates: (Row and Column templates)" region, click Create/copy
- Choose "As a Copy of an Existing Template", and click Next
- Indicate that you would like to copy a template from the current application, and click Next
- As shown in Figure 3, select "Yes" in the row showing the "Simple Row" template, and enter "Split Row" in its "To Name" field
- Click Copy Report Template
|
| Figure 3: Copying the "Simple Row" Template for Modification |
Next, edit "Split Row" to render each returned record set as two rows:
- From the main Templates page, click the edit icon next to your "Split Row" template.
- Add the HTML table row break, </tr><tr>, into the pattern for "Row Template 1":
<tr>
<td>#EMPNO#</td>
<td>#ENAME#</td>
<td>#JOB#</td>
<td>#MGR#</td>
<td>#HIREDATE#</td>
</tr>
<tr>
<td>#SAL#</td>
<td>#COMM#</td>
<td>#DEPTNO#</td>
</tr>
- Click Apply Changes
- Update your report region to use the new "Split Row" row template by setting the "Report Template" field on the Report Attributes screen to "Split Row" and applying that change.
Color-Coding Data
In some cases, developers need to color-code data based on selected values. Although it is possible to do so by selecting the required HTML out in the report query, it is most cleanly implemented using a report template.
Say, for example, you'd like to have your report on EMP render salaries over $2500 in red. You could do this by copying and modifying your Simple Row template as follows:
Copy the "Simple Row" template and call it "Color-Coded Salaries"
- From the HTML DB Application Builder home, click the "Templates" standard tab
- In the "Report Templates: (Row and Column templates)" region, click Create/copy
- Choose "As a Copy of an Existing Template", and click Next
- Indicate that you would like to copy a template from the current application, and click Next
- Select "Yes" in the row showing the "Simple Row" template, and enter "Color-Coded Salaries" in its "To Name" field
- Click Copy Report Template
Next, edit "Color-Coded Salaries" to render salaries greater than $2500 in red:
- From the main Templates page, click the edit icon next to your "Split Row" template.
- Copy the entry from the "Row Template 1" field into the "Row Template 2" field.
- Select "Use Based on PL/SQL Expression" in the "Row Template 1 Condition" field.
- As shown in Figure 4, enter the expression #SAL# > 2500 into the "Row Template 1 Expression" field.
- Add the color formatting, style="background:RED;", into the table data tag for the HIREDATE column in "Row Template 1":
<tr>
<td>#EMPNO#</td>
<td>#ENAME#</td>
<td>#JOB#</td>
<td>#MGR#</td>
<td>#HIREDATE#</td>
<td style="background:RED;">#SAL#</td>
<td>#COMM#</td>
<td>#DEPTNO#</td>
</tr>
- Click Apply Changes
- Update your report region to use the new "Color-Coded Salaries" row template by setting the "Report Template" field of your region's Report Attributes screen to "Color-Coded Salaries" and applying that change.
|
| Figure 4: Adding a Condition to "Row Template 1 Expression" |
When you run this report now, you'll find your salary values greater than $2500 to be rendered in red. The mechanics behind this implementation are quite simple: If you review the template definition screen for "Color-Coded Salaries", you'll see that HTML DB tries to use your "Row Template 1" pattern if your "Row Template 1 Condition" resolves to TRUE. if not, it uses "Row Template 2". Notice, too, that HTML DB currently allows for four such conditionally displayed row patterns in a given report template.
Using Session State Values When Conditionally Rendering Report Row Buttons
Often, developers will want to display buttons in each row of a report to allow users to perform actions on the displayed data. In some cases developers will need to render different buttons for different rows depending on certain conditions. To explore this case, let's imagine that our management has decided that "Finance" users in our system will be allowed to adjust salaries higher than $2500. All other users will simply be give the ability to view the available emps. Management further decided this functionality should be available as button links shown in our EMP report. Assuming we have an item in our application called CURRENT_USER_TYPE, and we want only those users with CURRENT_USER_TYPE = 'Finance' to see our Edit buttons, this requirement could be implemented as follows:
Copy the "Color-Coded Salaries" template while naming it "Adjustable Color-Coded Salaries"
- From the HTML DB Application Builder home, click the "Templates" standard tab
- In the "Report Templates: (Row and Column templates)" region, click Create/copy
- Choose "As a Copy of an Existing Template", and click Next
- Indicate that you would like to copy a template from the current application, and click Next
- Select "Yes" in the row showing the "Color-Coded Salaries" template, and enter "Adjustable Color-Coded Salaries" in its "To Name" field
- Click Copy Report Template
Next, edit "Adjustable Color-Coded Salaries" to render the appropriate buttons for each row:
- From the main Templates page, click the edit icon next to your "Adjustable Color-Coded Salaries" template.
- Immediately after the open table row tags in both "Row Template 1" and "Row Template 2", place the table data cell that will contain the View button which links to an update page (hypothetically page 2, requiring P2_EMPNO)
<td>
<a href="f?p=&APP_ID.:4:::::P2_EMPNO:#EMPNO#"><img src="#IMAGE_PREFIX#view.gif" border="0" alt="View Icon"></a>
</td>
The resulting row templates should look like this:
Report Row Template 1:
<tr>
<td><a href="f?p=&APP_ID.:2:&APP_SESSION.::::P2_EMPNO:#EMPNO#"><img src="#IMAGE_PREFIX#view.gif" border="0" alt="View Icon"></a></td>
<td>#EMPNO#</td><td>#ENAME#</td>
<td>#JOB#</td>
<td>#MGR#</td>
<td>#HIREDATE#</td>
<td style="background:RED;">#SAL#</td>
<td>#COMM#</td>
<td>#DEPTNO#</td>
</tr>
Report Row Template 2:
<tr>
<td><a href="f?p=&APP_ID.:2:&APP_SESSION.::::P2_EMPNO:#EMPNO#"><img src="#IMAGE_PREFIX#view.gif" border="0" alt="View Icon"></a></td>
<td>#EMPNO#</td>
<td>#ENAME#</td>
<td>#JOB#</td>
<td>#MGR#</td>
<td>#HIREDATE#</td>
<td>#SAL#</td>
<td>#COMM#</td>
<td>#DEPTNO#</td>
</tr>
- Adjust the Row Template 1 Expression to check for CURRENT_USER_TYPE by adding AND '&CURRENT_USER_TYPE.' != 'Finance'. The resulting expression should appear as:
#SAL# > 2500 AND '&CURRENT_USER_TYPE.' != 'Finance'
- Add a condition so Row Template 2 is only used when the current SAL is less than or equal to $2500. Before entering a condition into Row Template 2 Expression, though, you must first set the "Row Template 2 Condition" field to "Use Based on PL/SQL Expression". The
#SAL# =< 2500
- Copy the pattern, expression, and condition from Report Row Template 1 into Report Row Template 3.
- Change the CURRENT_USER_TYPE check in the "Report Template 3 Expression" field to its converse. The resulting expression should be:
#SAL# > 2500 AND '&CURRENT_USER_TYPE.' = 'Finance'
- Add the Edit button to Row Template 3, which is the case where the employee's salary is greater than $2500 and the current user is a Finance one. Assuming our Edit page for EMP is page 4 of our application, and it expects to be passed P4_EMPNO, your adjusted Row Template 3 should look like this:
<tr>
<td><a href="f?p=&APP_ID.:2:&APP_SESSION.::::P2_EMPNO:#EMPNO#"><img src="#IMAGE_PREFIX#view.gif" border="0" alt="View Icon"></a>
<a href="f?p=&APP_ID.:4:&APP_SESSION.::::P4_EMPNO:#EMPNO#"><img src="#IMAGE_PREFIX#edit.gif" border="0" alt="Edit Icon"></a></td>
<td>#EMPNO#</td><td>#ENAME#</td>
<td>#JOB#</td><td>#MGR#</td>
<td>#HIREDATE#</td>
<td style="background:RED;">#SAL#</td>
<td>#COMM#</td>
<td>#DEPTNO#</td>
</tr>
- To clean things up a little, add report column headers and borders to this template by adding table headers and the border attribute to the "Before Rows" field. The following HTML should be sufficient:
<table border="1">
<th style="background:GRAY;">Details</th>
<th style="background:GRAY;">#1#</th>
<th style="background:GRAY;">#2#</th>
<th style="background:GRAY;">#3#</th>
<th style="background:GRAY;">#4#</th>
<th style="background:GRAY;">#5#</th>
<th style="background:GRAY;">#6#</th>
<th style="background:GRAY;">#7#</th>
<th style="background:GRAY;">#8#</th>
Note, too, that the attribute-level help for this "Before Rows" field shows you how to add in substitution strings for pagination info and a csv download link should you wish to do so.
- Click Apply Changes
- Update your report region to use the new "Adjustable Color-Coded Salaries" row template by setting the "Report Template" field on the Report Attributes screen to "Adjustable Color-Coded Salaries" and applying that change. For a Finance user, the resulting report should appear as the one in Figure 5
|
| Figure 5: Conditionally Displayed Column Links Based on Session State |
Controlling Text Wrapping in Lengthy Report Columns
Sometimes developers want the text within report columns to not wrap even if that means the user has to scroll horizontally to see all the data. This simple example shows how to control such wrapping attributes from the report template level. First, let's update the JOB column to contain much more text than it already does:
- Click the "SQL" icon in the Navigation Bar from anywhere in the HTML DB development environment to go the SQL Workshop.
- Click the "SQL Command Processor" icon
- Enter the command alter table EMP modify (job varchar2 (4000)); into the text area and click Run SQL
- Enter the command update emp set job = rpad (job, 2000, ' this is extra text for the ' || job || ' position. '); and click Run SQL
If you run the report now, you'll find that the text in the JOB column wraps in such a way that all the columns of the report region fit cleanly within the screen's width. This rendering is default html behavior. To force your data to not wrap the text in that JOB column, specify nowrap for the white-space style attribute for the table data cell that renders that column. For simplicity's sake, make this change to a copy of your "Basic Row" template:
Copy the "Simple Row" template and call it "Unwrapped Job Rows"
- From the HTML DB Application Builder home, click the "Templates" standard tab
- In the "Report Templates: (Row and Column templates)" region, click Create/copy
- Choose "As a Copy of an Existing Template", and click Next
- Indicate that you would like to copy a template from the current application, and click Next
- Select "Yes" in the row showing the "Simple Row" template, and enter "Unwrapped Job Rows" in its "To Name" field
- Click Copy Report Template
Next, edit "Unwrapped Job Rows" to not wrap the JOB column:
- From the main Templates page, click the edit icon next to your "Unwrapped Job Rows" template.
- Set the white-space style attribute to nowrap in the table cell that renders your JOB column. The resulting Row Template 1 field should look like this:
<tr>
<td>#EMPNO#</td>
<td>#ENAME#</td>
<td style="white-space:nowrap;">#JOB#</td>
<td>#MGR#</td>
<td>#HIREDATE#</td>
<td>#SAL#</td>
<td>#COMM#</td>
<td>#DEPTNO#</td>
</tr>
- Click Apply Changes
When you run this report now, you will find your JOB column no longer wraps its data to fit to your screen's width.
Summary
In previous these five examples, you've hopefully gained a better appreciation for the power and flexibility provided by the Report Template features within HTMLDB. To recap, you've used HTML DB Report Templates to:
- Display report rows vertically as opposed to horizontally
- Split returned records into multiple report rows
- Color-code selected data
- Reference session state values when conditionally displaying button links
- Control text wrapping in report regions.
|