Prevent a column from ever being exported to CSV, PDF, etc?

Prevent a column from ever being exported to CSV, PDF, etc?

mig_akiramig_akira Posts: 2Questions: 1Answers: 1
edited February 2018 in Free community support

Hello!

I have a table with an "Actions" column, which has buttons to edit, delete, etc. I don't want this to ever be exported to any format, even if it's visible to the user. Also, only visible columns have to be exported.

In the doc (https://datatables.net/extensions/buttons/examples/html5/columns.html), I discovered how to force export a column and how to hide it, but I don't know how to prevent it's export without the user having to hide the column.

My code is like this:

table = $('#merchants_server').DataTable({...
    buttons: [
        {
          extend: 'colvis',
          columns: ':not(.js-no-vis)',
          text: 'CHOOSE COLUMNS',
          className: 'btn btn-outline-primary'
        },
        {
          extend: 'copy',
          text: '<i class="fa fa-copy"></i>',
          className: 'btn btn-outline-primary',
          exportOptions: {
            columns: ':visible'
          }
        },

        {
          extend: 'csv',
          className: 'btn btn-outline-primary',
          exportOptions: {
            columns: ':visible'
          }

Thanks!!

This question has an accepted answers - jump to answer

Answers

  • mig_akiramig_akira Posts: 2Questions: 1Answers: 1
    edited February 2018 Answer ✓

    I found the solution here: https://datatables.net/forums/discussion/29910/prevent-export-of-a-column-with-buttons

    The code would be, for example:

       {
              extend: 'csv',
              className: 'btn btn-outline-primary',
              exportOptions: {
                columns: ':visible :not(:last-child)'
              }
            },
    

    Or, even better, put a class or ID to the column that shouldn't be exported:

    in the view:
    <th class="js-not-exportable">Actions</th>
    
    
    in the code:
        {
              extend: 'csv',
              className: 'btn btn-outline-primary',
              exportOptions: {
                  columns: ':visible :not(.js-not-exportable)'
              }
        },
    
This discussion has been closed.