Javascript datatable add hidden column and export

Javascript datatable add hidden column and export

When you are working with javascript datatable to display the data you might need to hide some columns, but need to export them. This post describes how to Javascript datatable add hidden column and export in easy method. This is tested and confirmed method. For an example I have a column name called “Banner URL”. I need to show only thumbnail of it and hide the image long url from the cell. But allows to export the specific columns(here url).

My datatable version is 1.10.13

 

 

$('.dt-dataTables-data').dataTable({
                    paging: true,
                    lengthChange: true,
                    searching: true,
                    ordering: true,
                    "columnDefs": [
                        //hide the 2nd column. it's index is "1"
                        { 'visible': false, 'targets': [1] }  /// COLUMN INDEX HERE
                    ]
});

If you need to export the specific column put the column index as following.

$('.dt-dataTables-data').dataTable({
                    buttons: [
                        {
                            extend: 'collection',
                            text: 'Export',
                            buttons: [
                                {
                                    extend: 'copy',
                                    exportOptions: {
                                        columns: [1] /// COLUMN INDEX HERE TO EXPORT
                                    }
                                },
                                {
                                    extend: 'csv',
                                    exportOptions: {
                                        columns: [1]  /// COLUMN INDEX HERE TO EXPORT
                                    }
                                },
                                {
                                    extend: 'excel',
                                    exportOptions: {
                                        columns: [1]  /// COLUMN INDEX HERE TO EXPORT
                                    }
                                },
                                {
                                    extend: 'pdf',
                                    exportOptions: {
                                        columns: [1]  /// COLUMN INDEX HERE TO EXPORT
                                    }
                                },
                                {
                                    extend: 'print',
                                    exportOptions: {
                                        columns: [1]  /// COLUMN INDEX HERE TO EXPORT
                                    }
                                }
                            ]
                        }
                    ],
});

That’s all.

Reference:
Javascript datatable API

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments