clarketm / TableExport

The simple, easy-to-implement library to export HTML tables to xlsx, xls, csv, and txt files.
https://tableexport.travismclarke.com/
Apache License 2.0
888 stars 289 forks source link

Loop through a nested table #152

Open harryadel opened 6 years ago

harryadel commented 6 years ago

Hello there, we had this three nested tables within a big table where we provided data for it using datatable, now when we download it, all table headers would be listed consecutively underneath each other followed by the rows of each table in order.

<table class="col-md-12" id="tablesContainer"> 
        <div class="loader" style="margin:0 auto;" hidden></div>
        <table class="panel panel-visible table table-hover report-table table-bordered display" id="constHighLights-0" style="width:100%">
        </table>
        <table class="panel panel-visible table table-hover report-table table-bordered display" id="constHighLights-1" style="width:100%">
        </table>
        <table class="panel panel-visible table table-hover report-table table-bordered display" id="constHighLights-2" style="width:100%">
        </table>
      </table>
// It'd look like so
name age add
name age add
name age add
Clark x x
Mark x x
Park x x

So, digging into your package we found a little workaround and decided to create a merge request for it

var context = {};

            var _setContextRows = function (element) {
                var container = {};
                container.rows = _nodesArray(element.querySelectorAll('tbody > tr'));
                container.rows = settings.headers ? _nodesArray(element.querySelectorAll('thead > tr')).concat(container.rows) : container.rows;
                container.rows = settings.footers ? container.rows.concat(_nodesArray(element.querySelectorAll('tfoot > tr'))) : container.rows;
                container.thAdj = settings.headers ? element.querySelectorAll('thead > tr').length : 0;
                return container
            }
            context.rows = [];
            context.thAdj = 0;
            function recurse(element) {
                var wrapperElement = element || el;
                if (wrapperElement.querySelectorAll('table').length) {
                    wrapperElement.querySelectorAll('table').forEach((value, index) => {
                        recurse(value);
                    });
                } else {
                    Array.prototype.push.apply(context.rows, _setContextRows(wrapperElement).rows);
                    context.thAdj += _setContextRows(wrapperElement).thAdj;
                }
            }

            recurse();

                context.filename = settings.filename === 'id'
                    ? (el.getAttribute('id') ? el.getAttribute('id') : self.defaultFilename)
                    : (settings.filename ? settings.filename : self.defaultFilename);
                context.uuid = _uuid(el);

It has got some jagged edges that needs to be smoothed out, so we're definitely open for feedback/edits.

EDIT: Another commit has been added to fix CI, so it's better to squash into single commit.

harryadel commented 6 years ago

@clarketm Hi, I hope you can checkout my PR, thanks!

umamahesh0596 commented 5 years ago

Hi,

I also have multiple tables inside main table. Request your support to exclude inside tables in the excel.

Expecting immediate response.

harryadel commented 5 years ago

Request your support to exclude inside tables in the excel.

Uhh, could you please elaborate on what you mean by that? I don't get you!