问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/6062492/get-indexes-of-selected-columns-datatables-colvis