Search code examples
javascriptjqueryhtmlcssdatatables

Keep CSS of certain column in DataTable for print


I'am using DataTables with print plugin and in one column I have some kind of shema which looks good on desktop but when I print it CSS is removed.

My JS looks like this

$('table').DataTable({
  dom: 'Bfrtip',
  buttons: [{
      extend: 'print',
      customize: function(win) {
        $(win.document.body)
          .css('font-size', '10pt');

        $(win.document.body).find('table')
          .addClass('compact')
          .css('font-size', 'inherit');
      },
      exportOptions: {
        columns: ':visible'
      }
    },
    'colvis'
  ]
});

Full example is here https://jsfiddle.net/eza27o58/

Shema is in User column under users name.

Is there a way to keep CSS of that column for print so that whole shema is visible?


Solution

  • Set stripHtml to false in the exportOptions. The CSS was not removed, it's the div- and strong-tags that were removed.

    $('table').DataTable({
      dom: 'Bfrtip',
      buttons: [{
          extend: 'print',
          customize: function(win) {
            $(win.document.body)
              .css('font-size', '10pt');
    
            $(win.document.body).find('table')
              .addClass('compact')
              .css('font-size', 'inherit');
          },
          exportOptions: {
            columns: ':visible',
            stripHtml: false
          }
        }
      ]
    });
    

    Fork of your example: https://jsfiddle.net/snxb9ay0/