8

I just started out using jQuery DataTables.

using the tableTools of DataTables, is it possible to only export visible rows instead of all the rows? If for example the pagination was set to 10 I would expect only 10 rows to be exported. The same goes for a search result.

Here's part of the code:

$(document).ready(function() {
      var table = $('#example').DataTable({
        "pagingType": "full_numbers",
          "iDisplayLength" : 10,
                dom: 'T<"clear">lfrtip',
                 "oTableTools": {
            "aButtons": [
{ "sExtends": "copy", "mColumns": "visible",      "bSelectedOnly": true },
{ "sExtends": "xls", "mColumns": "visible" },
{ "sExtends": "print", "mColumns": "visible" }

], "sRowSelect": "multi"},


             "order": [[ 0, "asc" ]]
    } ) ;...

Thank you.

Tony Clifton
  • 703
  • 3
  • 14
  • 27

4 Answers4

19

I used this solution and it worked. Try this:

<script>
$(document).ready(function() {
    var table = $('#example').DataTable( {
        "pagingType": "full_numbers",
        "iDisplayLength": 10,
        "dom": 'T<"clear">lfrtip',
        "oTableTools": {
          "aButtons": [
            {'sExtends':'copy',
              "oSelectorOpts": { filter: 'applied', order: 'current' },
            },
            {'sExtends':'xls',
              "oSelectorOpts": { filter: 'applied', order: 'current' },
            },
            {'sExtends':'print',
              "oSelectorOpts": { filter: 'applied', order: 'current' },
            }
          ]
        },
    });
});
</script>
xpros
  • 2,166
  • 18
  • 15
saghar.fadaei
  • 3,205
  • 1
  • 17
  • 10
  • Why is this the solution? Clarify your answer and document what has changed or is different from the question. – Luceos Jul 28 '14 at 11:00
  • 2
    I use "oSelectorOpts" for every button , so it's only export visible rows base on filter(pagination - search ... ) – saghar.fadaei Aug 03 '14 at 04:31
3

You may set the selection of the page to current page for specific export.

Ref: http://datatables.net/docs/DataTables/1.9.4/#$

{ "sExtends": "xls", "mColumns": "visible", "oSelectorOpts": { page: "current" } }
StartingFromScratch
  • 628
  • 3
  • 10
  • 23
3

If you are using flash to export, need to mention swf path to work.

$("#example").dataTable( {
    "sDom": 'T<"clear">lfrtip',
    "oTableTools": {
        "sSwfPath": "Path to your copy_csv_xls_pdf.swf files comes with TableTools",
        "aButtons": [
            {
                "sExtends": "copy",
                "sButtonText": "Copy to clipboard",
                "oSelectorOpts": { filter: "applied", order: "current" }
            },
            {
                "sExtends": "csv",
                "sButtonText": "Export to CSV",
                "oSelectorOpts": { filter: "applied", order: "current" }
            },
            {
                "sExtends": "print",
                "sButtonText": "Print",
                "oSelectorOpts": { filter: "applied", order: "current" }
            }
        ]
    }
} );

There are few additional options also available to aButtons object.

"mColumns": [1, 2,...] - List of columns to include in export result

"sTitle": "filename" - desire filename for export file

------------------Update---------------------------

In the newer version of datatable - datatableTools is retired

Please use buttons extension

buttons: [
        {
            extend: 'copyHtml5',
            exportOptions: {
                columns: [ 0, ':visible' ]
            }
        },
        {
            extend: 'excelHtml5',
            exportOptions: {
                columns: ':visible'
            }
        },
        {
            extend: 'pdfHtml5',
            exportOptions: {
                columns: [ 0, 1, 2, 5 ]
            }
        },
    ]
Bheru Lal Lohar
  • 880
  • 9
  • 17
2

You can achieve that behavior by selecting all visible rows before saving, then deselecting them after saving completed.

$(document).ready(function() {
    $('#example').DataTable( {
        dom: 'T<"clear">lfrtip',
        "oTableTools": {
            "sRowSelect": "multi",
            "aButtons": [
                {
                    "sExtends": "csv",
                    "bSelectedOnly": true,
                    "fnComplete": function ( nButton, oConfig, oFlash, sFlash ) {
                        var oTT = TableTools.fnGetInstance( 'example' );
                        var nRow = $('#example tbody tr');
                        oTT.fnDeselect(nRow);
                    }
                }
            ]
        }
    } );

    $('a.DTTT_button_csv').mousedown(function(){
        var oTT = TableTools.fnGetInstance( 'example' );
        var nRow = $('#example tbody tr');
        oTT.fnSelect(nRow);
    });
} );
Walid Ammar
  • 4,038
  • 3
  • 25
  • 48
  • 1
    Hi Mohammad, nice solution you gave. Unfortunately it seems to be not working in FF 30, it exports everything. Do you have any solution for this issue? Thanks. – zdtorok Jul 07 '14 at 10:52
  • @zdtorok make sure, you added `"sRowSelect": "multi"` to `"oTableTools"`. Works great, thanks! – mrohnstock Jun 26 '15 at 10:08
  • @Monty : I've added the mentioned line as well so it's still not working as expected :( – zdtorok Jun 29 '15 at 12:38
  • @zdtorok could you please update your example with your currented code? Maybe there is something wrong... – mrohnstock Jul 01 '15 at 13:41