Select columns to export!

Select columns to export!

Mauro26Mauro26 Posts: 17Questions: 7Answers: 0

I'm having a problem with the export, it works perfect but i don't know how to do what i want to do.

 var table = $('#ID_TrackOrderoverview').dataTable( {
        "dom": 'Brtip',
        "stateSave": true,
        "buttons": [
        {
            "extend": 'excelHtml5',
            "autoFilter": true,
            "sheetName": 'Exported data',
            "exportOptions": {
                    columns: '.show'
                }
        },
        {
            "extend": 'columnsToggle',
            columns: '.show'
        }],

I'm using a class "Show" to have the buttons that i need in the columnsToggle.
But now i want to show only some colmns and i cannot use index or names cause if the user has less privileges the columns available will change. I want to use a class like class:export but i'm already using it to show specific buttons.
I need to show 2 hidden values and several other columns and i cannot use index or names as reference.

I searched a lot and i couldnt find an answer, a class will be great like i do with tooglebuttons.
Regards.

Answers

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    You need to apply the class to the column headers before initialising the DataTable for that to work - Buttons column visibility actually initialises before the column classes are added by columns.className which is why you are running into an issue here.

    So you could use:

    $('#myTable thead th').eq(0).addClass('show');
    $('#myTable').DataTable( ... );
    

    Allan

  • Mauro26Mauro26 Posts: 17Questions: 7Answers: 0

    Hi allan,
    I'm actually defining all the columns during he initialization of the datatable. And it works perfectly the export and the ToogleButtons, the problem is that i'm using the Class attribute for showing the ToogleButtons, now i need something similar that let me export some columns but i cannot use index or name since the table changes if u have more rights.
    Right now is exporting perfect but the oes that i'm using with the class show, Is there anyway to do something similar?
    Thanks for ur replay

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    The only way would be to initialise Buttons after the DataTables initialisation. Which is quite possible.

    Allan

  • Mauro26Mauro26 Posts: 17Questions: 7Answers: 0

    Hi allan,
    if i do it like that how i'm going to identify the columns that i want?

    "exportOptions": {
                       columns: '.show'
                   }
    

    is going to keep bringing me the columns with the show class i thought about adding a property called Export: true to the columns i want to use and somehow use it there,

    Regards

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    It should work exactly that like. Your Buttons initilaisation is the same (although you pass it in as the second parameter to the Buttons constructor rather than using buttons in the table configuration object).

    Allan

  • Mauro26Mauro26 Posts: 17Questions: 7Answers: 0

    Hi allan thanks for ur replies.

    new $.fn.DataTable.Buttons( table, {
            buttons: [{
                "extend": 'excelHtml5',
                "text":      '<i class="fa fa-file-excel-o"></i> Excel',
                "autoFilter": true,
                "sheetName": 'Exported data',
                "exportOptions": {
                           ##columns:  '.show'
                           columns: function (idx, data, node) {
                                 return table.columns[idx].bExport === true;
                }
                }
            }]
        });
    
        table.buttons( 1, null ).container().appendTo(
            table.table().container() 
        );
    

    I don't know if i'm doing it right but i create an attribute in aoColumns called bExport and now i'm trying to get it to have the columnsthat i want to show in the excel. Is this the rigt way, anyway i'm getting an error table.columns is not working.
    Regards

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Is this the rigt way, anyway i'm getting an error table.columns is not working.

    That suggests it isn't the right way ;). And no - I wouldn't expect this method to work - table.columns is a function, not an array.

    Before you were using columns: '.show' and you said that was being assigned by columns.className. That's the way to do it.

    Allan

This discussion has been closed.