Get indexes of selected columns (DataTables + ColVis)

I am using the jQuery plugin to create DataTables + ColVis tables. I need to get an array of column indices that the user selected to display (this information will be used to create a custom table for export).

For example: the user chooses to display only the browser and platform from the table here . I need to get [1,2].

Any ideas?

+1
source share
1 answer

EDIT PREVIOUS ANSWER

I figured out the best way to use the data APIs:

//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 
+3
source

Source: https://habr.com/ru/post/1498945/


All Articles