On line guide/help

You are on page 45 of the online help. Navigate sequentially through the guide pages or search for a topic of interest to you.

Index Prev. Next

Table

Print a table with titles, formatted values and totals, on more pages

The table element prints a table: a header row with the column titles, the data rows and an optional totals row with the aggregate functions (sum, average, minimum, maximum, count). The rows have a fixed height: a value that does not fit in its cell is truncated. When the rows do not fit in the page, the table continues on a new page and prints the header row again. Version 1.0.13 or later.

The table is printed in a content, as the other elements. The data rows are read from a datalist (linked to a data provider in the PHP code), from a PHP array or from the template.

A table cannot be printed in a section linked to a data provider that has no page element (a section that prints more data rows on the same page): print it in a section without data provider, eg. the main section of the document, or in a section with a page element (one page for each data row, eg. one invoice for each order). A table cannot be printed in the page_header or page_footer content of another table.

Parameters

Elements / attributes of the <table> element:

<id> Identifier of the table (default table). It is the prefix of the placeholders of the current row, eg. {t1.amount}, and the id used by the SetTableData method and by the after attribute of print_content
<printif> Optional expression: the table is printed only if the expression is true. See the Conditional printing page

Position and size:
<x> (or <x1>) Left position of the table
<y_start> (or <y>, <y1>) Y position of the table on the first page
<width> Width of the table. Instead of the width, the right position can be set with <x2>. If not set, the table is as wide as the sum of the column widths (a column with width * is not allowed)
<row_height> Height of the data rows (default 6)
<header_height> Height of the header row (default: row_height)
<total_height> Height of the totals row (default: row_height)
<vertalign> Vertical alignment of the text in the cells: T top, M middle (default), B bottom

Page breaks (see Multi-page tables below):
<y_end> The rows are printed above this Y position, then the table continues on a new page (default: page height - 10)
<y_start_next> Y position of the table on the next pages (default: y_start)
<rows_count> Maximum number of data rows on each page (default 0: no limit, only y_end)
<page_header> Id of the content printed at the beginning of each page added by the table (eg. the logo and the title of the document)
<page_footer> Id of the content printed at the end of each page left by the table (eg. the footer with the page number)
<page> Settings of the pages added by the table, as the page element (default: the page of the current section, or the default page)

Header, borders and truncated values:
<show_header> yes (default) or no: prints the header row with the column titles
<repeat_header> yes (default) or no: prints the header row again on each new page
<empty_text> Text printed in a row as wide as the table when there are no data rows, eg. No items (default: empty, only the header row is printed)
<ellipsis> Text appended to a truncated value: yes (default, three dots), no (the value is cut without ellipsis) or another text, eg. ~
<border> Lines of the table, any combination of: O outer border, H horizontal lines between the rows, V vertical lines between the columns, S lines below the header row and above the totals row. A or 1 all the lines (same as OHV, default), N or 0 no lines. Eg. OS: outer border and the two separators only
<linestyle> Line style of all the lines (if not set, the default line style is used)
<outerlinestyle> Line style of the outer border
<innerlinestyle> Line style of the inner lines (H, V and S)

Fonts and backgrounds:
<font> Font of the data rows (if not set, the default font is used)
<headerfont> Font of the header row (default: the font of the rows, bold). The attributes not set are taken from the default, eg. <headerfont color="FFFFFF"/> changes only the color
<totalfont> Font of the totals row (default: the font of the rows, bold)
<fill> (or <fillstyle>) Background of the data rows (default: none)
<altfill> Background of the even data rows (alternate rows, zebra stripes)
<headerfill> Background of the header row
<totalfill> Background of the totals row
A background can be an attribute with the RGB hex color, eg. altfill="F5F5F5", or an element with the attributes of the fill style, eg. <headerfill color="1F2D3D"/> (solid fill when the type is not set)

Structure and data:
<columns> (required) The columns of the table: one <column> element for each column (see Columns below)
<rowstyle> Conditional styles of the data rows (see Conditional styles below)
<totals> Label and style of the totals row (see Totals below)
<datalist> Id of the datalist that contains the data rows, eg. <datalist id="lines"/> (see Data rows below)
<rows> Data rows written in the template: one <row> element for each row (see Data rows below)

A simple table with a totals row

The PHP code links a data provider to the datalist of the table:

PHP code

Columns

Elements / attributes of the <column> element (inside the <columns> element):

<field> (or <name>) Name of the data field printed in the column (case insensitive)
<value> Instead of a field: a text with placeholders, eg. {t1.first_name} {t1.last_name}, or {t1.ROWNUM} for the row number. One of field and value is required
<title> (or <header>) Title printed in the header row. Placeholders are replaced, eg. Amount ({CURRENCY})
<width> Width of the column in the units of the document (eg. mm), or * (default): the columns with width * share in equal parts the width of the table left by the other columns. Eg. in a table 180 mm wide with two fixed columns of 30 mm and two columns with width *, each * column is 60 mm wide
<align> (aliases: <horizalign>, <textalign>) Horizontal alignment of the values: L left (default), C center, R right
<headeralign> Horizontal alignment of the title (default: the alignment of the values)
<format> (aliases: <valueformat>, <mask>, <valuemask>) Format mask of the values, eg. I, F2, C2, P1. See the Value formatting page. Empty values are not formatted
<textfit> Value that does not fit in the cell. Default: the value is printed on a single line and truncated, with the ellipsis of the table. The modes of the box element can also be used, eg. Resize to scale down the font (see the Text fit page)
<font> Font of the values (default: the font of the table). The attributes not set are taken from the font of the table, eg. <font style="B"/>
<fill> (or <fillstyle>) Background of the values, eg. to highlight a column (default: the background of the row)
<style> Conditional styles of the cells (see Conditional styles below)

Totals row (see Totals below):
<aggregate> (or <total>) Aggregate function printed in the totals row: sum, avg (or average), min, max, count
<totalformat> Format mask of the total (default: the format mask of the column; I for count)
<totaltext> Text printed in the totals row by a column without aggregate function. Placeholders are replaced

Columns with fixed and * widths, values and totals

Conditional styles

The <rowstyle> elements of the table change the style of a whole data row, the <style> elements of a column change the style of a single cell. Attributes:

<when> (aliases: <condition>, <if>) Condition, with the syntax of printif (see the Conditional printing page), eg. {t1.qty}&lt;=0. The placeholders of the current row are replaced before the condition is evaluated. Empty: the style is always applied
<color> (or <fontcolor>) Text color (RGB hex format)
<fill> (aliases: <fillcolor>, <backcolor>) Background color (RGB hex format)
<fontstyle> Font style: B bold, I italic, U underline, or a combination, eg. BI
<fontsize> Font size (points)

Only the attributes that are set change the style. When more styles of the same element match, they are applied in the order they are written, so the last one wins.
Backgrounds, from the lowest to the highest priority: background of the rows (fill, altfill), background of the column, row style, cell style. Text: font of the column (or of the table), row style, cell style.
In the XML template the character < must be written as &lt;, eg. when="{t1.qty}&lt;10".

Conditional row and cell styles

Data rows

The data rows are read from one of these sources, in order of priority:

1. A PHP array passed with the SetTableData(string $tableId, array $rows) method: each row is an associative array (field name => value). Useful when the rows are already in memory, eg. calculated by the application or read from a REST API.
2. A datalist: the element <datalist id="lines"/> inside the table, linked to a data provider in the PHP code with SetDatalist('lines', $dataProvider). All the data providers can be used (database, CSV, JSON, XML, PHP array). The query can contain placeholders, replaced each time the table is printed: a table printed in a master section (one page for each invoice) prints the rows of the current invoice, eg. WHERE invoice_id = {inv.id}, or the filter invoice_id={inv.id} of DataProviderArray.
3. Rows written in the template: a <rows> element with one <row> element for each row; the fields are the attributes (or the child elements) of the row. Placeholders are replaced, eg. the variables.

Placeholders of the current row, used in the value of the columns and in the conditions of the styles:
{table_id.field} Field of the current row, eg. {t1.amount}. With a datalist, also {datalist_id.field}
{table_id.ROWNUM} Number of the current row (1, 2, 3, ..)

PHP code: datalist and PHP array

A table can be printed in a section linked to a data provider only when the section has a page element (one page for each data row):

Template

Totals

The totals row is printed at the end of the table when at least one column has an aggregate function or a total text. The <totals> element sets its label and style; it can also be a simple text, eg. <totals>Total</totals>.

<label> (aliases: <text>, <value>) Label printed in the columns before the first column with a total, eg. Total. Placeholders are replaced
<align> Horizontal alignment of the label: L (default), C, R
<height> Height of the totals row (default: the total_height of the table, or row_height)
<show> auto (default: printed when a column has a total), yes or no
<font> Font of the totals row (default: the font of the rows, bold)
<fill> Background of the totals row

Aggregate functions (aggregate attribute of the column): sum, avg (average), min and max use only the numeric values, count counts the values that are not empty. They use the values of the data, before the format mask. In an empty table, sum and count are 0, avg, min and max are empty.

After the table, these variables can be printed in the other contents:
{table_id.ROWCOUNT} Number of data rows
{table_id.TOTAL.field} Total of the column of the field (columns with an aggregate function and a field), not formatted: use the format attribute of the box
{table_id.Y_END} Y position below the last row of the table (totals row included)

Totals row

The totals can be printed again in another content, eg. in a summary box:

Variables set by the table

Multi-page tables

The rows are printed from y_start down to y_end. When the next row does not fit above y_end, or rows_count rows have already been printed on the page, the table:
1. prints the page_footer content on the current page (eg. the footer with the page number)
2. adds a new page (page element of the table or of the section)
3. prints the page_header content on the new page (eg. the logo and the title of the document)
4. prints the header row again (repeat_header) and continues from y_start_next

The attributes y_start, y_end, rows_count and page have the same names used by the section element. If the header row and the first data row do not fit on the first page, the whole table starts on a new page; the totals row is printed on a new page if it does not fit above y_end.
The page_header and page_footer contents are printed by the table only on the pages it adds: on the first page and on the last page they are printed by the section, as usual. In the footer, Page {PAGEINDEX} of {PAGECOUNT} counts also the pages added by the table.

Table on more pages, with header and footer on each page

Contents printed after a table

A table ends at a different position depending on the number of its rows. The after attribute of <print_content> prints a content (eg. notes, a summary, the signatures) after the end of a table already printed:

<after> Id of the table
<position> relative (default) or fixed

Relative position: the Y coordinates of the content are relative to the end of the table, eg. y="0" is the first line below the table.
<gap> Space between the end of the table and the content (default 0)
<height> Height of the content: if the content does not fit above the y_end of the table, it is printed on a new page at y_start_next (with the page_footer and page_header contents of the table). Without height, the content is always printed on the same page

Fixed position: the Y coordinates of the content are absolute, eg. the signatures always at the bottom of the last page.
<top> (required) Y position of the top of the content: if the table ends below it, the content is printed on a new page
<height> Height of the content

More contents can be printed after the same table: each one is placed below the previous one (top + height), in the order of the print_content elements. The x and y offsets of print_content are added as usual.

Notes below the table and signatures at a fixed position

Complete example

The Stock inventory report template is a complete example: a warehouse stock list of 64 items read from a CSV file, printed on two pages with the header row repeated, zebra rows, conditional row and cell colors, truncated descriptions and a totals row. After the table, a summary prints {inv.ROWCOUNT}, {inv.TOTAL.qty} and {inv.TOTAL.value}, and the approval signatures are printed at a fixed position at the bottom of the last page. Download the XML template and the CSV data.

PHP code (the full code sets more variables)

The table element of the template:

inventory-report.en.xml (table element)