Using a text element
You can place a text element directly on the page or in any of the container elements. When you insert a text element, the layout editor displays the text editor, as shown in Figure 7‑9.
Figure 7‑9 Edit Text Item
First, decide what type of text you want to create. You have three choices:
*Plain text
HTML enables you to create highly formatted text using HTML tags or CSS properties. The text can contain placeholders for data set field values and expressions, which enable you to mix static text with dynamically generated values. Plain text and auto, on the other hand, cannot contain internal formatting or dynamic values. A text element that is set to plain text or auto functions like a label element.
After selecting the text type, type the text to display in the report. If you selected HTML, you can use HTML tags or CSS properties in the text. Type the tags manually, or insert the commonly used HTML tags that the text editor provides.
The following sections provide a few examples of text you can create using a text element of HTML type.
Applying multiple style formats to text
Using HTML, you can format individual words and lines in a text element. The following example shows two lines with different font sizes and styles.
Text that you supply:
<CENTER><B><span style="font-size: larger">
Shipped Orders Report
<FONT size="small">For the month of March</FONT></CENTER>
Combining a JavaScript expression and static text
BIRT Report Designer provides a useful tag, VALUE-OF, which you use to insert a dynamic value in a text element. Using the VALUE-OF tag, you can insert any JavaScript expression.
The following example shows static text combined with a value that a JavaScript function returns.
Text that you supply:
Report generated on <VALUE-OF>new Date()</VALUE-OF>
Report generated on Jan 19, 2010 12:30 PM
The following example shows static text combined with a conditional expression and a field value expression.
Text that you supply:
Dear <VALUE-OF>row["Sex"] == "M" ? "Mr." : "Ms."</VALUE-OF>
Dear Mr. Scott Johnson,
Dear Ms. Ella Parker,
As the example shows, a conditional expression can have one of two values based on a condition. The syntax for this conditional expression is:
condition ? value1 : value2
If the condition is true, the expression has the value of value1; otherwise, it has the value of value2.
Alternatively, you can use an if...else statement within the VALUE-OF tag. The following text displays the same results as the preceding conditional expression:
Dear <VALUE-OF>if(row["Sex"] == "M"){
Title = "Mr."
Title = "Ms."
Combining a value from a data set field and static text
The following example shows how to use the VALUE-OF tag to insert dynamic values that data set fields return.
Text that you supply:
Dear <VALUE-OF>row["contact_firstname"]</VALUE-OF>,
Thank you for your recent order. Order <VALUE-OF>
row["orderID"]</VALUE-OF> will be shipped by <VALUE-OF>
Dear Bob,
Thank you for your recent order. Order 1115 will be shipped
by Apr 17, 2009 12:00AM.
You can display field values in a text element only if the following requirements are met:
*The text element has access to the data set that contains the fields.
*If you place the text element directly on the page, you must bind the text element to the data set that contains the field value. To do so, select the text element, choose the Binding tab in the property editor, then select the data set to which to bind. Placing the text element directly on the page, however, displays only one value.
*If you place the text element in the detail row of a table or a list to display all values of a data set field, bind the table or list to the data set.
*A column binding is created for each data set field. The column binding refers to the data set field. The VALUE-OF tag refers to the column binding.
Formatting dynamic values in a text element
The previous examples show how to use the VALUE-OF tag to insert dynamic values that a JavaScript function or a field returns. Sometimes the returned values are not in the desired format. You can reformat the values using the format attribute, as shown in the following example.
Text that you supply:
<VALUE-OF format="MM-dd-yy">new Date()</VALUE-OF><br>
<VALUE-OF format="$#,###.00">row["orderTotal"]</VALUE-OF><br>
<VALUE-OF format="(@@@) @@@-@@@@">row["phone"]</VALUE-OF>
(415) 123-5555
The format pattern must be enclosed in quotation marks. You can use any format pattern that the Format Number, Format DateTime, and Format String properties support. For information about these properties, see Formatting report content.
Displaying data set field values that are stored as HTML text
Sometimes values in a data set field contain HTML text. If you insert such a field in a report, BIRT Report Designer displays the content of the field exactly as it appears in the data source, including the HTML tags. To display the text with its intended formatting, use a text element or a dynamic text element instead of a data element. As described earlier in this chapter, the text element enables you to add static text to the dynamic text, whereas the dynamic text element displays all the HTML tags if you add static text.
To use the text element, select HTML as the text type, then use the VALUE‑OF tag to insert the value of the field, and set the format attribute to HTML, as shown in the following example.
Text that you supply:
Notes: <VALUE-OF format="html">row["CustomerNotes"]</VALUE-OF>
Notes: The customer wants email confirmation for his orders.