Buttons icon indicating copy to clipboard operation
Buttons copied to clipboard

add colspan support for footer

Open chipco opened this issue 9 years ago • 5 comments

When printing a table with colspan in footer the cell is repeated with no colspan.

Example:

<tfoot>
    <tr>
        <th colspan="3">a</th>
        <th>b</th>
    </tr>
</tfoot>

results in

a a a b

chipco avatar Jan 04 '16 19:01 chipco

I think this is a duplicate of #60. The issue is that there is no way to represent the colspan in CSV or TSV.

DataTables avatar Jan 05 '16 09:01 DataTables

Indeed, but it should work for print and pdf.

Atm I'm using a customize for print:

customize: function (win) {
    var footer = $('tfoot');
    if (footer.length > 0) {
        var exportFooter = $(win.document.body).find('thead:eq(1)');
        exportFooter.after(footer.clone());
        exportFooter.remove();
    }
}

Note the thead:eq(1) should be replaced by tfoot when the fix is released.

chipco avatar Jan 05 '16 09:01 chipco

In my pull request: https://github.com/DataTables/Buttons/pull/74 I introduce the ability to modify the data before it is exported, for instance in CSV and TSV. In my example I show one approach to dealing with colspans. There, it fills blank cells for trailing columns, but feel free to use the code if it helps.

While it does not automate things for the developer, it at least releases some control to the developer, to modify the exported data. In the future you can add formatting options; for instance:

  • _colspans-repeat_ (boolean) :
    optional format; when a colspan is encountered, its value will be repeated for n number of columns in the span (alternative is to fill columns with empty strings, as in my example)
  • _colspans-col-value-first_ | _colspans-col-value-last_ (boolean) ;
    _colspans-col-value-position_ (integer) :
    optional format; when colspan value does not repeat, let the user decide what cell the value should appear in.
    • If first is true, then the value will appear in the first column, followed by blank strings.
    • If last is true, then the value will appear in the last column, with empty strings appearing before it.
    • If the position is selected the value will appear in that respective column within the colspan, with empty strings in the columns that come before and after for the duration of the span.

vol7ron avatar Mar 05 '16 19:03 vol7ron

Thanks @vol7ron, that's a great idea. Being able to customize the data in geneal is the way to go in my opinion. For example in some cases I want to export footers with more than one row, but the default behavior is to export only the first. Similarly for headers, only the last table row is exported now.

chipco avatar Mar 24 '16 19:03 chipco

Indeed, but it should work for print and pdf.

Atm I'm using a customize for print:

customize: function (win) {
    var footer = $('tfoot');
    if (footer.length > 0) {
        var exportFooter = $(win.document.body).find('thead:eq(1)');
        exportFooter.after(footer.clone());
        exportFooter.remove();
    }
}

Note the thead:eq(1) should be replaced by tfoot when the fix is released.

For pdf this is not working, "Uncaught TypeError: Cannot read property 'body' of undefined" this error is coming in "var exportFooter = $(win.document.body).find('tfoot:eq(1)');"

inspira-repo avatar Mar 02 '21 08:03 inspira-repo

This has finally been done and will ship in Buttons 3 alongside DataTables 2.

AllanJard avatar Oct 17 '23 14:10 AllanJard