docxTable with dynamic columns and formatting



  • 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:

    Image of report

    The template:

    Image of 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:

    image




  • administrators

    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?


  • administrators

    hi @Thiago-Medeiros

    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?


  • administrators

    @Thiago-Medeiros let's continue the conversation here where you posted a comment


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.