Get indexes of selected columns (DataTables + ColVis)

大城市里の小女人 提交于 2019-12-31 04:05:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!