Vasilen Donchev
Vasilen Donchev

Reputation: 1035

Get indexes of selected columns (DataTables + ColVis)

I am using the jQuery plugin DataTables + ColVis to show tables. I have to get an array of the indexes of the columns which the user has chosen to display (this information will be used for creating a customizable table for export).

For example: A user chooses to display only Browser and Platform(s) from the table here. I need to obtain [1,2].

Any ideas?

Upvotes: 1

Views: 2749

Answers (1)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

EDIT TO PREVIOUS ANSWER

I figured out a better way using datatables API:

    //You have to pass the datatable object. 
//in the case of your example you should pass $('#example').dataTable();
    var fnGetVisibleColumns = function(oTable) {
    var counter = 0;
    aColumns = new Array();
    $.each(oTable.fnSettings().aoColumns, function(c){
        if(oTable.fnSettings().aoColumns[c].bVisible == true){
            aColumns.push(counter)
         }
        counter++;
    });
    return aColumns;
    }

//Now you can do var aVisibleColumns = fnfnGetVisibleColumns($('#example').dataTable());
//aVisibleColumns is [1,2] if the user displays only "browser" and "platform" columns

Upvotes: 3

Related Questions