javascript 数据表列数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10808651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:06:22  来源:igfitidea点击:

datatables number of columns

javascriptjquerydatatables

提问by Astronaut

How do i get the number of columns in a datatable?

如何获取数据表中的列数?

I can get the number of rows by doing oTable.fnGetData().length but how do I get the number of columns so I can Iterate over each cell like this:

我可以通过执行 oTable.fnGetData().length 来获取行数,但是如何获取列数以便我可以像这样迭代每个单元格:

var numColumns = //How to get the number of columns?
    for(var r=0;r<oTable.fnGetData().length;r++){
        for(var c=0;c <= numColumns;c++){
            console.log("[R]: "+r+ "[C]: "+c+ " data: "+oTable.fnGetData(r,c));   
        }
    }

采纳答案by c0deNinja

The fnGetData can be used to get a single data row. So, you can simply check the length of the data row for the number of columns.

fnGetData 可用于获取单个数据行。因此,您可以简单地检查数据行的长度以获取列数。

Example:

例子:

oTable.fnGetData(someRowNumber).length;

The above will return the number of columns in that data row.

以上将返回该数据行中的列数。

回答by Gyrocode.com

GET NUMBER OF COLUMNS

获取列数

  • DataTables 1.9+

    // Get number of all columns
    var numCols = $('#example').dataTable().fnSettings().aoColumns.length;
    
    // Get number of visible columns
    var numCols = $('#example thead th').length;
    

    See this jsFiddlefor demonstration.

  • DataTables 1.10+

    // Get number of all columns
    var numCols = $('#example').DataTable().columns().nodes().length;
    
    // Get number of visible columns
    var numCols = $('#example').DataTable().columns(':visible').nodes().length;
    
    // Get number of visible columns (alternative way)
    var numCols = $('#example thead th').length;
    

    See this jsFiddlefor demonstration.

  • 数据表 1.9+

    // Get number of all columns
    var numCols = $('#example').dataTable().fnSettings().aoColumns.length;
    
    // Get number of visible columns
    var numCols = $('#example thead th').length;
    

    请参阅此 jsFiddle进行演示。

  • 数据表 1.10+

    // Get number of all columns
    var numCols = $('#example').DataTable().columns().nodes().length;
    
    // Get number of visible columns
    var numCols = $('#example').DataTable().columns(':visible').nodes().length;
    
    // Get number of visible columns (alternative way)
    var numCols = $('#example thead th').length;
    

    请参阅此 jsFiddle进行演示。

ITERATE OVER EACH CELL

迭代每个单元格

  • DataTables 1.10.6+

    $('#example').DataTable().cells().every( function (rowIndex, colIndex) {
        var data = this.data();
    
        console.log("Row:", rowIndex, "Col:", colIndex, "Data:", data);
    });
    
  • 数据表 1.10.6+

    $('#example').DataTable().cells().every( function (rowIndex, colIndex) {
        var data = this.data();
    
        console.log("Row:", rowIndex, "Col:", colIndex, "Data:", data);
    });