Skip to content

DocxReportingJavaMainListFieldAdvancedTable

Marcus edited this page Jan 18, 2016 · 3 revisions

Loop for fields in table row with template engine script

Docx Reporting Java Main List Field In Table is the easiest mean to generate table row from a Java object collection, because when you design your report with MS Word you need not to write some loop script (Freemarker, Velocity....): XDocReport use FieldsMetadata to generate loop script with lazy mode.

However Docx Reporting Java Main List Field In Table mean is limited if you wish generate more powerful report like:

  • manage a color for odd/even row in a table :

  • manage a table in a loop (ex : loop for developers and generate a table to display roles per developer):

(http://wiki.xdocreport.googlecode.com/git/screenshots/DocxAdvancedTableOddEven.png))

Advanced Table is the mean where you write loop script (Freemarker, Velocity) in the report. This mean is more powerful than Docx Reporting Java Main List Field In Table but more complex to write (this mean is not for a secretary).

Since XDocReport 0.9.2, XDocReport provides @before-row and @after-row features to insert template engine script before/after a table row.

Problem: before/after row loop

To manage loop, XDocReport use template engine script. Loop for fields use syntax of the template engine. For instance if you wish generate list of name of developers you can write that with Velocity :

#foreach($d in $developers)
  $d.Name
#end

And write that with Freemarker :

[developers as d](#list)
  ${d.name}
[

Problem with loop for table row is that it's not possible to set the start/end directive loop of the template engine before and after the table row with MS Word.

Solution: use @before-row @after-row

To resolve the previous problem, XDocReport provides 2 tokens :

  • @before-rowSCRIPT : if a MergeField which is inserted in a table cell, contains at first this token, XDocReport will move the SCRIPT before the row. For instance with Velocity you can write :
@before-row#foreach($d in $developers)

And write that with Freemarker :

@before-row[#list developers as d](/#list])
  • @after-row**SCRIPT : if a MergeField which is inserted in a table cell, contains at first this token, XDocReport will move the SCRIPT before the row. For instance with Velocity you can write :
@after-row#end

And write that with Freemarker :

@after-row[

Token configuration

If you don't like the notation @before-row and @after-row, you can customize it with FieldsMetadata. Here a sample to use @row instead of @before-row and @/row instead of @after-row :

FieldsMetadata metadata = new FieldsMetadata();
metadata.setBeforeRowToken("@row");
metadata.setAfterRowToken("@/row");

IXDocReport report = ...			
report.setFieldsMetadata(metadata);

After that you can write:

@row#foreach($d in $developers)
@/row#end

instead of writing:

@before-row#foreach($d in $developers)
@after-row#end

UseCases

You can find following samples in the http://code.google.com/p/xdocreport/downloads/list docxandvelocity-*-sample.zip and docxandfreemarker-*-sample.zip or see the docx report DocxTableWithoutFieldsMetadataWithVelocity.docx and DocxTableWithoutFieldsMetadataWithFreemarker.docx.

Simple loop for table row

If you wish generate simple table row of Developer (without FieldsMetadata) :

[You must write this report if you wish use Velocity :

(http://wiki.xdocreport.googlecode.com/git/screenshots/DocxAdvancedTableSimpleTable.png))

Here Velocity scripts used in this sample :

------------------------------------------------------------------------
@before-row#foreach($d in $developers)         | $d.LastName   | $d.Mail
$d.Name                                        |               |
@after-row#end                                 |               |
------------------------------------------------------------------------

Each Velocity scripts must be inserted in a Mergefield. After XDocReport processing, you will do that:

#foreach($d in $developers)
------------------------------------------------------------------------
$d.Name         | $d.LastName   | $d.Mail
------------------------------------------------------------------------
#end

Here Freemarker scripts used in this sample :

------------------------------------------------------------------------
@before-row[developers as d](#list) | ${d.lastName}   | ${d.mail}
${d.name}                          |                 |
@after-row[|                 |
------------------------------------------------------------------------

Each Freemarker scripts must be inserted in a Mergefield. After XDocReport processing, you will do that:

[#list developers as d](/#list])
------------------------------------------------------------------------
${d.name}         | ${d.lastName}   |  ${d.mail}
------------------------------------------------------------------------
[

Manage a color for odd/even row

If you wish generate odd/even color for table row of Developer :

(/#list])

The table contains 2 rows (a blue and white row) which are displayed or not according the row index.

You must write this report if you wish use Velocity :

[Here Velocity scripts used in this sample :

------------------------------------------------------------------------
@before-row#foreach($d in $developers) #if( 0 == $velocityCount%2) | $d.LastName   | $d.Mail
$d.Name                                                            |               |
@after-row#else                                                    |               |
------------------------------------------------------------------------
$d.Name                                                            | $d.LastName   | $d.Mail
@after-row#end #end                                                |               |
------------------------------------------------------------------------

Each Velocity scripts must be inserted in a Mergefield. The Velocity if condition :

#if( 0 == $velocityCount%2)

is used to check if row is odd or even and generate the blue or white row. After XDocReport processing, you will do that:

#foreach($d in $developers) #if( 0 == $velocityCount%2)
------------------------------------------------------------------------
$d.Name         | $d.LastName   | $d.Mail
------------------------------------------------------------------------
#else
------------------------------------------------------------------------
$d.Name         | $d.LastName   | $d.Mail
------------------------------------------------------------------------
#end #end

Here Freemarker scripts used in this sample :

------------------------------------------------------------------------
@before-row[#list developers as d](http://wiki.xdocreport.googlecode.com/git/screenshots/DocxAdvancedTableOddEvenReport.png)) [d_index % 2 == 0](#if) | ${d.lastName}   | ${d.mail}
${d.name}                                                 |                 |
@after-row[|                 |
------------------------------------------------------------------------
${d.name}                                                 | ${d.lastName}   | ${d.mail}
@after-row[/#if](#else])[|                 |
------------------------------------------------------------------------

Each Freemarker scripts must be inserted in a Mergefield. The Freemarker if condition :

 [#if d_index % 2 == 0](/#list])

is used to check if row is odd or even and generate the blue or white row. After XDocReport processing, you will do that:

[developers as d](#list) [d_index % 2 == 0](#if)
------------------------------------------------------------------------
${d.name}         | ${d.lastName}   |  ${d.mail}
------------------------------------------------------------------------
[------------------------------------------------------------------------
${d.name}         | ${d.lastName}   |  ${d.mail}
------------------------------------------------------------------------
[/#if](#else])[

Manage a table in a loop

If you wish generate a table in a loop (ex : loop for developers and generate a table to display roles per developer) :

(/#list])

You must write this report if you wish use Velocity :

[Here Velocity scripts used in this sample :

#foreach($d in $developers)

Identity
Name : $d.Name
Last Name : $d.LastName
Email : $d.Mail
Roles

------------------------------------------------------------------------
Roles
------------------------------------------------------------------------
@before-row#foreach($r in $d.Roles)
$r.Name
@after-row#end
------------------------------------------------------------------------

#end

After XDocReport processing, you will do that:

#foreach($d in $developers)

Identity
Name : $d.Name
Last Name : $d.LastName
Email : $d.Mail
Roles

------------------------------------------------------------------------
Roles
#foreach($r in $d.Roles)
------------------------------------------------------------------------
$r.Name
------------------------------------------------------------------------
#end
#end

Here Freemarker scripts used in this sample :

[#list developers as d](http://wiki.xdocreport.googlecode.com/git/screenshots/DocxAdvancedTableTableInLoopReport.png))

Identity
Name : ${d.name}
Last Name : ${d.lastName}
Email : ${d.mail}
Roles

------------------------------------------------------------------------
Roles
------------------------------------------------------------------------
@before-row[d.roles  as r](#list)
${r.name}
@after-row[------------------------------------------------------------------------

[/#list](/#list])

After XDocReport processing, you will do that:

[developers as d](#list)

Identity
Name : ${d.name}
Last Name : ${d.lastName}
Email : ${d.mail}
Roles

------------------------------------------------------------------------
Roles
[d.roles  as r](#list)
------------------------------------------------------------------------
${r.name}
------------------------------------------------------------------------
[[/#list](/#list])
Clone this wiki locally