Specifying and ordering columns for Exporting/Printing

Specifying and ordering columns for Exporting/Printing

jLinuxjLinux Posts: 981Questions: 73Answers: 75

I have a table with a number of columns, which can be toggled via columnsToggle, and a hidden column named status, which is the 4th column.

The status values are either Unlocked or Locked, and while the column isn't visible, the value of it will determine the style of the row, (If its locked, then it will have a slight red tint to it).

I want the Status column to be visible when the table is exported or printed, and keep it in the 4th position, and only export the other columns that are visible.

I use the DT API to add the buttons to a container via a function manage_buttons(this) which is executed via initComplete, heres the relevant code

function manage_buttons($dt) {
    var $api = $dt.api();

    // Configure Export Buttons (Copy, csv, xls, pdf)
    new $.fn.dataTable.Buttons( $api, {
        buttons: [
            {
                extend: 'copy',
                exportOptions: {
                    columns: [ 4, ':visible' ]
                }
            }, {
                extend: 'csv',
                exportOptions: {
                    columns: [ 4, ':visible' ]
                }
            }, {
                extend: 'excel',
                exportOptions: {
                    columns: ':visible'
                }
            }, {
                extend: 'pdf',
                exportOptions: {
                    columns: [ 4, ':visible' ]
                }
            }
        ]
    } );

    // Add the Export buttons to the toolbox
    $api.buttons( 0, null ).container().appendTo( '#export-assets' );


    // Configure Print Button
    new $.fn.dataTable.Buttons( $api, {
        buttons: [
            {
                extend: 'print',
                exportOptions: {
                    columns: [ 4, ':visible' ]
                }
            }
        ]
    } );

    // Add the Print button to the toolbox
    $api.buttons( 1, null ).container().appendTo( '#print-assets' );
}

The problem I'm running into, is it will always put the 4th column (status) first... is there a way to have them exported in the same order as they are in the table? Meaning, have it between whatever columns its visibly between on the table. And export the Status column.

Heres the columns handed to the columns, just for an example..

[
  {
    "data":"creator",
    "name":"creator",
    "visible":true
  },
  {
    "data":"created",
    "name":"created",
    "visible":true
  },
  {
    "data":"status",
    "name":"status",
    "visible":false
  },
  {
    "data":"field_one",
    "name":"field_one",
    "visible":true
  },
  {
    "data":"field_two",
    "name":"field_two",
    "visible":true
  },
  {
    "data":"field_three",
    "name":"field_three",
    "visible":true
  }
]

So basically, just need to keep the status column in place, and always export that, and export the other visible columns.

Thanks!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin
    Answer ✓

    Yes - but you need to use the column-selector as a function. For example:

      columns: function ( idx, data, node ) {
        return idx === 4 || table.column( idx ).visible();
      }
    

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    So that will run for every column? So if its col #4, return true, for everything else, just return true if its visible?

    I didnt know using an enclosure as the parameter would run for each individual column

    Thanks!

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    That works great! You should put that in an example/demo somewhere, lol

This discussion has been closed.