docxTable with dynamic columns and formatting
-
Hi,
I'm trying to understand how
docxTable
is supposed to work with dynamic columns while keeping specific formatting for header row and normal rows. I'm successfully using simpledocxTable
with only rows data putting the{{#docxTable}
and{{/docxTable}}
tags respectively in the first and last cell of the first non-header row.
In which cells are the{{#docxTable}
and{{/docxTable}}
tags supposed to be placed for dynamic columns?I've set up a sample here: https://playground.jsreport.net/w/pdecat/GmHy17Ev
But right now it's failing with:Error: Error while executing docx recipe. Error while executing templating engine. Must pass iterator to #each.
I've based this sample on what I found in the existing test cases in the jsreport-docx source repository, e.g.:
-
hi! https://playground.jsreport.net/ uses jsreport 2.8.0 which does not use latest version of jsreport-docx, the dynamic columns was introduced in the latest version, which is jsreport 2.10.0. you need to install jsreport 2.10.0 and try it.
Note: you can find the version of jsreport that the playground uses here in the "About":
-
Oh, that explains why I did not find any examples of this in the playground!
I'm indeed using jsreport 2.10 locally, I just created the above playground to showcase my issue.
Anyway, when generating a report locally with the same template and data, here is what I get:
The template:
The data:
{ "rowsItems": [ ["Jan", "jan.blaha@foo.com"], ["Boris", "boris@foo.met"], ["Pavel", "pavel@foo.met"] ], "columnsItems": ["Name", "Email"] }
-
The only thing that seems to work is to put everything in the same cell but then we loose the ability to have a different formatting to header row and other rows:
-
Oh, I must have missed something:
https://github.com/jsreport/jsreport-docx/issues/12#issuecomment-681044772
-
The only thing that seems to work is to put everything in the same cell
yes, it should be done only in one cell
https://github.com/jsreport/jsreport-docx/issues/12#issuecomment-681044772
we don't do anything special about the styles, so my comment in that issue is still true, that the table should have styles defined, i see that word also let you generate custom styles so this should not be a problem. one easy way to verify that your table styles are working is by inserting more cells and rows to your table to test that it gets the different colors.
-
Yeah, it works with a single cell and a generic table style thanks!
Looks like I lost my table's original style at some point, probably while editing the document with Word Online.
-
And if on the contrary:
"rowsItems": [
["Jan", "jan.blaha@foo.com"],
["Boris", "boris@foo.met"],
["Pavel", "pavel@foo.met"]
],I had one for each line an object with a name and email? How do I access each attribute?
-
if you have an array of objects then you can easily write a helper function that convert the object to the array of values.
for example the helper function can be something like this:
function dataArrayToCellValues (items) { return items.map((item) => { return [item.name, item.email] }) }
then in the docx file you can update the call to be:
{{docxTable rows=(dataArrayToCellValues items) columns=columnsItems}}
-
I understood. I did it in a different way ...
Let's assume that my data is configured like this:{
"rowsItems": [
[
{"name":"jan", "email": "jan.blaha@foo.com "},
{"name":"Boris", "email": "boris@foo.met"},
{"name":"Pavel", "email": "pavel@foo.met"}
],
],
"columnsItems": ["", ""]
}I can access the contents of the object by doing this.atributename:
{{this.name}}
-
Another question...
If I want to create a table dynamic with columns (4 columns, for example), but that has a single header that involves the entire table. How do I do this?
-
@Thiago-Medeiros let's continue the conversation here where you posted a comment