docx-templates icon indicating copy to clipboard operation
docx-templates copied to clipboard

Please add a nested table rows generation support

Open t0lik opened this issue 5 years ago • 13 comments

изображение

In this examle we will see no table at all in the result docx even if we have objects in the "table" array.

Please add support for nested tables,

t0lik avatar Aug 27 '19 12:08 t0lik

+1 for this request. I've tried to fill nested table also.

gkiszczak avatar Aug 27 '19 12:08 gkiszczak

+1 for this request. I've tried to fill nested table also. nead a image

wlydhub avatar Sep 11 '19 10:09 wlydhub

+1 please add nested table support

achaird1 avatar Jan 30 '20 16:01 achaird1

👍 Thanks for the lib by the way

bfo2ses avatar Apr 09 '20 13:04 bfo2ses

+1 to add this. Thanks

tomspeed22 avatar Jun 20 '20 09:06 tomspeed22

+1 bump

jeopratama avatar Sep 28 '20 18:09 jeopratama

+1 , i want this feature, thanks

chaosforfun avatar Mar 07 '22 08:03 chaosforfun

In case it helps anyone, I got nested table working. There is a catch though, You need to have some space at the bottom of the nested table.

Here is the template I used:

image

Below is the result:

image

As you can see, the first 2 tables rendered just fine, but the last one did not render at all.

I am not sure if this is a limitation with docx-templates or MS Word.

pkozul avatar Mar 10 '22 00:03 pkozul

I modified the code and got it working. Not sure if this change would affect other scenarios, but now all 3 of the tables are being rendered fine. I added the code outlined in red (in the file processTemplate.js).

image

Below is the code I added, if anyone needs it for copy/paste:

// If the last generated output node is a table row, and it is set to be deleted,
// don't delete if it has exactly one nested row (i.e. within nested table)
if (tag === 'w:tr' && fRemoveNode) {
    fRemoveNode = (nodeIn._children.filter(child => child._tag === 'w:tr').length !== 1);
}

Below is the output:

image

pkozul avatar Mar 10 '22 01:03 pkozul

@pkozul thanks for looking into this! Can you make a PR with this change? I'd like to look into it further. If you can also add that template so I can make it into a test case, that would also be great.

jjhbw avatar Mar 10 '22 11:03 jjhbw

@jjhbw Bummer. Although my code change worked in this example I included above, it doesn't work in the more complex scenario I have at work. I'm not familiar enough with how the entire code works, so maybe you'll have some ideas on what else we can try?

I think it really comes down to the code I have outlined in the red box, and determining the exact condition required to NOT remove the w:tr node if it contains (dynamically generated) content.

pkozul avatar Mar 15 '22 06:03 pkozul

I'm out of ideas honestly. The docx spec is just too complex and too dependent on MS Word's secret sauce (MS trustbusters should have paid attention to this long ago).

At work we have had great success in achieving roughly what you want by inserting complex tables like the one below as HTML (using docx-templates). MS Word supports a good ~80% of the HTML spec surrounding <table/> elements and associated style rules. Note that support is not guaranteed (or nonexistent) with other docx readers, like LibreOffice.

I'm sorry to dissappoint. Happy to entertain ideas on how to solve this, though, but I'm all out myself.

jjhbw avatar Mar 19 '22 10:03 jjhbw