How to display Datatable tabletools (copy, csv, excel, pdf, save) in ruby on rails

Maruthi042 picture Maruthi042 · Feb 6, 2013 · Viewed 9.9k times · Source

I am using Datatable in my ruby on rails application. I follow the same one which is here..

https://github.com/rweng/jquery-datatables-rails

And My datatable sorting and searching working properly. But I can't see my table tool option (eg - copy, csv, excel, pdf, save ) in my table header.

I want to show my table just like this....

enter image description here

Please help.

Answer

Tomanow picture Tomanow · Apr 30, 2013

Update (2016):

Although they are retiring the TableTools for Buttons and Select extensions (source), this is a slightly more recent version of the dom option example:

var oTable = $('#my-table').dataTable({
    autoWidth: false,
    autoHeight: false,
    paging: false,
    dom: 'TCfrtip', // <-- Update letters for whichever extensions you want to use
    responsive: false,
    searching: true,
    ordering: true,
    stateSave: true,
    scrollY: 550,
    scrollX: true,
    scrollCollapse: true,
    fixedHeader: false,
    buttons: [
        'copyHtml5',
        'csvHtml5',
        'excelHtml5',
        'pdfHtml5'
    ],
    columnDefs: [{
        targets: 'no-sort', // disable sorting on class="no-sort"
        orderable: false
    }],
    drawCallback: function (settings) { }
});

Previous Answer (2013):

The solution is to add this:

"sDom": '<"H"TCfr>t<"F"ip>'

Inside your javascript. It will work with show/hide columns nicely as well. If you are not using show/hide columns you can remove the capital "C".

Example (with show/hide columns):

// Users
$("#users-datatable").dataTable({
    "bStateSave": true,
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": $('#users-datatable').data('source'),
    "bScrollInfinite": true,
    "bScrollCollapse": true,
    "iDisplayLength": 100,
    "sScrollY": "500px",
    "sScrollX": "100%",
    "sDom": '<"H"TCfr>t<"F"ip>',
    "oTableTools": {
        "aButtons": [
            "copy",
            "csv",
            "xls",
            {
                "sExtends": "pdf",
                "sPdfOrientation": "landscape",
                "sPdfMessage": "Your custom message would go here."
            },
            "print"
        ]
    }
});

Hopefully this will help someone.