jQuery dataTables - TableTools: hide row and columns while export

1.7k Views Asked by At

How can I exclude certain column and row while exporting from DataTables using TableTools.

| id | data      | date           | Status |
|----|-----------|----------------|--------|
| 1  | some data | october 2014   | ok     |
| 2  | some data | september 2014 |        |

I want to hide the status column and the row which doesn't have status as "ok". Any tips to solve this problem would be appreciated.

1

There are 1 best solutions below

0
On

There is as far as I know not a "magic" option that solve both problems. And the options to solve each issue seems to exclude each other (would be nice if anyone can disprove that). But I have found a solution (to avoid redundant code I only show it for the copy button) :

var table = $("#example").DataTable({
    dom: 'T<"clear">lfrtip',
    tableTools: {
      sSwfPath: "http://cdn.datatables.net/tabletools/2.2.3/swf/copy_csv_xls.swf",
      aButtons: [
            {
                sExtends : "copy",
                mColumns : [0, 1, 2], 
                fnInit : function( nButton, oConfig ) {
                    $(nButton).on('mousedown', function() {
                        table.column(3).search('ok').draw();
                    });
                    $(nButton).on('mouseup', function() {
                        table.column(3).search('').draw();
                    });
                },
                oSelectorOpts : { filter: 'applied', order: 'current' },
            }
    ]}
});

Explanation :

mColumns : [0, 1, 2] -> export only the id, data and date columns

fnInit -> be able to do something with the button when it is initialized

$(nButton).on('mousedown', function() { -> filter rows where status is "ok"

$(nButton).on('mouseup', function() { -> unset the "ok" filter when the export is done

oSelectorOpts : { filter: 'applied', order: 'current' } -> set tabletools to only export filtered rows in the order they appear for the user.

The reason for using mousedown / mouseup is that the above does not work with click or the button function fnComplete.

Add this functionality to all your buttons, eg xls, csv, pdf and print. I think there is a problem with the print button. As far as I know, it earlier had "problems" by following the button-instructions. Dont know if this has been fixed recently.