Dinamic tables with table-rows that alternate background color between white and grey

1,261 views
Skip to first unread message

Mpedrosa

unread,
Sep 15, 2011, 7:42:19 AM9/15/11
to jodreports-users
Hello. I'm using JODreports to create a OpenOffice odt (writer
document) and generate dinamic tables.
Im having no problem with generating the tables, but im having
problems with getting the formatting that i want.

I want my tables to have a header with black background, and then the
lines of the table alternate between grey and white.

The header is not a problem, i configured a table auto format on the
template that had the formatting i want.
The problem is that in the template when i insert the script:
@table:table-row
[#list rows as row]
@/table:table-row
[/#list]
all the lines generated will stay with the formatting of the line
where that script was inserted (all white), instead of alternating the
background color.... So the auto-format does not do it...

so far as i cant set the color with table-row styles, i must do it on
table-cell on each cell. Also using that script for generating the
lines i cant say what color i want for that cell, since it will just
put the data, the formatting will always stay the same as the line i
put the script in...

I made my own code for extracting the content.xml, changing manually
to what i want, and repacking that into the odt, but it is too damn
slow if there are a lot of data to show. while JODreports generates in
3 seconds, my code to do the alternating colors takes 1~2 minutes to
change what i need...

Is there a way to do this using JODreports?

Thanks on advance for the help
Marco Pedrosa

Ansgar Konermann

unread,
Sep 15, 2011, 4:30:33 PM9/15/11
to jodrepor...@googlegroups.com
Requires a bit of hand-tuning your XML, I guess. The following steps have not been tested, but might eventually bring you where you'd like to go:

- create table with two different row styles, copy row styles XML elements from extracted content.xml -> auto-styles to styles.xml office:styles using text/xml editor. (Re-) Name them so that their names end in 0 or 1.
- have a look at the XML inside content.xml which corresponds to your table row. they will have a style attribute. note the name of this attribute
- in your ODT template, use a counter to count the number of rows you have already rendered. use freemarker's #assign directive to initialize a counter before starting your [#list] and another [#assign] inside the list to increment this counter
- generate the style *attribute* for each table row depending on the row counter. use modulus/modulo operator applied to counter to restrict values to 0 and 1, and append this number to the name prefix of your styles from step 1

Hope this will at least get you the idea how to accomplish it.

References:
Changing tag attributes: https://sourceforge.net/apps/phpbb/jodreports/viewtopic.php?f=2&t=51
Assignment directive: http://freemarker.sourceforge.net/docs/ref_directive_assign.html

Best

Ansgar

Legolith

unread,
Sep 16, 2011, 12:01:20 PM9/16/11
to jodrepor...@googlegroups.com
Thanks for your help, i really appreciate it :)

Your tips were right on target, I tried it, and after some work i managed to make it do what i wanted.

I'm still having a bit of a problem tough... I cannot rename/add the new table-row styles properly...
I mean, i add the new styles manually to the content.xml or the styles.xml (tried both) but the next time I save the document, the extra styles i added get deleted by a garbage collector or something like that... (it reset to the automatic styles and deletes the ones i added)
What I could do was create on the table 2 lines (one has the code for the list) and the other is a blank line of grey background. Then i switch between the table row styles of those 2 lines instead of the "custom" ones like you suggested.
But this way i will always end up with one extra line on my table in the end... Is there a way i can add the extra table-row styles withouth word rewriting them when I save the template?

Thanks again
     Marco Andre Pedrosa
--
-----------------------------------------------------------
In the course of our lifes, we are always so busy
with what we must do, that we never have time for
the IMPORTANT things...

Ansgar Konermann

unread,
Sep 16, 2011, 4:52:20 PM9/16/11
to jodrepor...@googlegroups.com
Am 16.09.2011 18:01, schrieb Legolith:
Thanks for your help, i really appreciate it :)

Your tips were right on target, I tried it, and after some work i managed to make it do what i wanted.

I'm still having a bit of a problem tough... I cannot rename/add the new table-row styles properly...
I mean, i add the new styles manually to the content.xml or the styles.xml (tried both) but the next time I save the document, the extra styles i added get deleted by a garbage collector or something like that... (it reset to the automatic styles and deletes the ones i added)
What I could do was create on the table 2 lines (one has the code for the list) and the other is a blank line of grey background. Then i switch between the table row styles of those 2 lines instead of the "custom" ones like you suggested.
But this way i will always end up with one extra line on my table in the end... Is there a way i can add the extra table-row styles withouth word rewriting them when I save the template?

You could:
- introduce a section into your template with the sole purpose of containing a table with two rows and one row-style each
- sole purpose of this table is to *define* and *reference* the row-styles so they don't get garbage collected
- use the automatic styles of this table in your actual table you'd like to generate, maybe using ternary operator to choose row style name (like: #if counter % 1 == 0 ? "name1" : "name2")
- set the sections 'visible' attribute to false, so you won't see it (this is the real trick :-)

This is a bit ugly, you'll get an extra section in the generated document, but you'll only see it if you actually open the navigator view. AFAIK, when converting this document to PDF, there won't be an extra section at all.

Best regards

Ansgar

Legolith

unread,
Sep 19, 2011, 10:54:57 AM9/19/11
to jodrepor...@googlegroups.com
I tried it, and it works REALLY well!!!
The section is invisible to all but navigator indeed, and navigator is almost never used.

I take this moment to thank you for all your help, and for the quick replys too, and congratulate you on a very useful program. It is powerfull, fast, and does its job very well. :)
I liked JODreports VERY much, you guys rule :)

I included my working template as an attachment, in case you want it for future reference for some other person with the same problem :)

Greetings
     Marco André Pedrosa
teste2.odt

Владимир Морозов

unread,
Mar 11, 2012, 4:45:13 AM3/11/12
to jodrepor...@googlegroups.com
Hi,

I will try render your attached file, and get error: Caused by: freemarker.core.InvalidReferenceException: Expression row is undefined on line 4, column 19499 in content.xml.

What i need to configure or ani thing to do?

PS: I use jooreports 2.4.0 and I can`t get work table in any examples :(

понедельник, 19 сентября 2011 г. 18:54:57 UTC+4 пользователь Mpedrosa написал:
I tried it, and it works REALLY well!!!
The section is invisible to all but navigator indeed, and navigator is almost never used.

I take this moment to thank you for all your help, and for the quick replys too, and congratulate you on a very useful program. It is powerfull, fast, and does its job very well. :)
I liked JODreports VERY much, you guys rule :)

I included my working template as an attachment, in case you want it for future reference for some other person with the same problem :)

Greetings
     Marco André Pedrosa

On Fri, Sep 16, 2011 at 9:52 PM, Ansgar Konermann <ansgar.konermann@googlemail.com> wrote:
Am 16.09.2011 18:01, schrieb Legolith:
Thanks for your help, i really appreciate it :)

Your tips were right on target, I tried it, and after some work i managed to make it do what i wanted.

I'm still having a bit of a problem tough... I cannot rename/add the new table-row styles properly...
I mean, i add the new styles manually to the content.xml or the styles.xml (tried both) but the next time I save the document, the extra styles i added get deleted by a garbage collector or something like that... (it reset to the automatic styles and deletes the ones i added)
What I could do was create on the table 2 lines (one has the code for the list) and the other is a blank line of grey background. Then i switch between the table row styles of those 2 lines instead of the "custom" ones like you suggested.
But this way i will always end up with one extra line on my table in the end... Is there a way i can add the extra table-row styles withouth word rewriting them when I save the template?

You could:
- introduce a section into your template with the sole purpose of containing a table with two rows and one row-style each
- sole purpose of this table is to *define* and *reference* the row-styles so they don't get garbage collected
- use the automatic styles of this table in your actual table you'd like to generate, maybe using ternary operator to choose row style name (like: #if counter % 1 == 0 ? "name1" : "name2")
- set the sections 'visible' attribute to false, so you won't see it (this is the real trick :-)

This is a bit ugly, you'll get an extra section in the generated document, but you'll only see it if you actually open the navigator view. AFAIK, when converting this document to PDF, there won't be an extra section at all.

Best regards

Ansgar

Thanks again
     Marco Andre Pedrosa

Ansgar Konermann

unread,
Mar 11, 2012, 6:12:54 PM3/11/12
to jodreports-users


On Mar 11, 9:45 am, Владимир Морозов <greenhos...@gmail.com> wrote:
> Hi,
>
> I will try render your attached file, and get error: Caused by:
> freemarker.core.InvalidReferenceException: Expression row is undefined on
> line 4, column 19499 in content.xml.
>
> What i need to configure or ani thing to do?

Please show the Java code you use to render the file.

Best regards

Ansgar

>
> PS: I use jooreports 2.4.0 and I can`t get work table in any examples :(
>
> понедельник, 19 сентября 2011 г. 18:54:57 UTC+4 пользователь Mpedrosa
> написал:
>
>
>
>
>
>
>
>
>
> > I tried it, and it works REALLY well!!!
> > The section is invisible to all but navigator indeed, and navigator is
> > almost never used.
>
> > I take this moment to thank you for all your help, and for the quick
> > replys too, and congratulate you on a very useful program. It is powerfull,
> > fast, and does its job very well. :)
> > I liked JODreports VERY much, you guys rule :)
>
> > I included my working template as an attachment, in case you want it for
> > future reference for some other person with the same problem :)
>
> > Greetings
> >      Marco André Pedrosa
>
> > On Fri, Sep 16, 2011 at 9:52 PM, Ansgar Konermann <
> > ansgar.konerm...@googlemail.com> wrote:
>
> >> **

Legolith

unread,
Mar 11, 2012, 8:25:00 PM3/11/12
to jodrepor...@googlegroups.com
That error occurs when use an expression on the .odt and don't set a value for that expression on the java.
The expression rows on my example is supposed to contain the list of values for the table.
So for that error you either didn't add the variable "rows" to the data model, or you added an incorrect object type in it for some reason.

// *** My datamodel is like this: ***
HashMap<String, Object> model = new HashMap<String, Object>();
// objects outside the table
model.put("companyname", "Test Company"); 
model.put("tipoinformacao1", "info 1");  
model.put("tipoinformacao2", "info 2");  
model.put("date", "Monday, 22 of August of 2011");
// the object that contains the data for the table, first level of array is a line, second level is a column object
ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
ArrayList<String> row = new ArrayList<String>();
// 6 columns so 6 objects in each row - i am using strings inside
row.add("1");
row.add("Name 1");
row.add("Address 1");
row.add("Locality 1");
row.add("Postal Code 1");
row.add("Bank Account 1"); 
// add a row of the table to the table object
rows.add(row);
row.add("2");
row.add("Name 2");
row.add("Address 2");
row.add("Locality 2");
row.add("Postal Code 2");
row.add("Bank Account 2"); 
// add a row of the table to the table object
rows.add(row);
// add the table object to the data model
model.put("rows", rows); 

// create the document using that data model
DocumentTemplateFactory documentTemplateFactory = new DocumentTemplateFactory();
DocumentTemplate template = documentTemplateFactory.getTemplate(new File("teste2.odt"));
template.createDocument(model, new FileOutputStream("result.odt"));

I hope this will shed some light on the issue.
Greetings
     Marco André Pedrosa
Reply all
Reply to author
Forward
0 new messages