0

I need to generate a docx from a data set in which there are variable tag names:

{
    "month": "January",
    "year": "2020",
    "workers": {
        "John": [
            {
                "days": "12",
                "shop": "Shop #1"
            }
        ],
        "Bob": [
            {
                "days": "11",
                "shop": "Shop #1"
            },
            {
                "days": "4",
                "shop": "Shop #2"
            }
        ]
    }
}

Of course I can neither know the number nor the names of the workers.

It might be simple but I can't really get to produce the template-side code to parse such data. Any tips?

Seether
  • 1,524
  • 1
  • 14
  • 28

1 Answers1

0

You could use the angular-expressions package option, see here on how to install it :

https://docxtemplater.readthedocs.io/en/latest/angular_parse.html

Then you could do, in your code :

expressions.filters.loopObject = function(input) {
    return Object.keys(input).map(function(key) {
             return { key , value : input[key]};
    });
}

And in your template, you can now write :

{#workers | loopObject}
Name : {key}
Shop : {value.shop}
Days : {value.days}
{/}
edi9999
  • 19,701
  • 13
  • 88
  • 127
  • Yeah, that's more or less what I thought I had to do anyway. Basically I've rethought my server-side data-producing function to directly return non-associative values, so that I can easily process data within the template. Like how you suggested, but without need of angular-expressions. Still, thanks for your help. – Seether Mar 27 '20 at 14:43