[Template Components] Table

A Table can read records from any datasource. Within the header, body, and footer of a table you can insert any kind of component and connect it to any datasource (eg. the master datasource or the table datasource).

Simple Table
For a simple table, define a datasource called Simple in the report and connect it to one of your datasource files in the repository. Create a table and set it’s datasource to be Simple. Insert a field in the body and set the value to be a field from Simple. Render the report and you should see the table generated with one row for each record in Simple, with the value of your chosen field displayed in each row.

Master-Detail Filtered Table
For a table that only shows records matching the current master record (master-detail), there are two ways to proceed:

*Filtered table with memory cache
You can cache the table datasource in memory and filter it dynamically to extract the rows to insert in the table. This approach only requires one datasource access, but requires additional memory. Hence it may not be suitable if the secondary datasource is large.

To use a filtered datasource, you need two datasources defined in the report. Both datasources, called All and Filtered here, should point to the same datasource file.

In the OnRenderBegin of the table, insert the following script:

=Code Snippet=
var all = DataCacheManager.getCache("All");
var filtered = all.filter("Column",Value); // substitute appropriate column names and values
DataCacheManager.putCache("Filtered",filtered);

*Note: Don’t use the variable name cache as the table has a field called cache and the conflict will cause problems.

Then set the table datasource to be Filtered. The first call to getCache(“All”) will load the cache. Subsequent calls will read it from memory. The All cache is then filtered and inserted into the DataCacheManager with the name " Filtered ". This means it is accessible to the table. We created the Filtered datasource in the report so that we can use the UI to refer to the fields in the schema. The Filtered datasource never gets loaded explicitly, for each iteration, it takes on the appropriate subset of the All datasource.

If you need to apply multiple filters, just cascade calls to the filter method:

=Code Snippet=
var filtered = all.filter("Column",Value);
filtered = filtered.filter("Column2",Value2);

*Filtered table with datasource parameter
You can pass filter parameters to the datasource and have it provide the filtering for you. This requires more datasource accesses but uses less memory.

If your datasource supports filtering internally, for example, JDBC supports SQL WHERE syntax, then add a parameter to the WHERE clause, eg. WHERE COLUMN = ${VALUE}. Remember that if the value is a string, you should insert the quotes: WHERE COLUMN = '${VALUE}'.

If your datasource doesn’t support filtering directly, wrap it with a Composite DataSource, and add the filtering parameter there.

In your table OnRenderBegin, you need to set the parameter that will be used to filter the table datasource.

=Code Snippet=
Renderer.getRawReport().setParameter("VALUE",Value);

In this example, the first “VALUE” is the name of the parameter required by the datasource, and the second “Value” is the field value from the master datasource.