4

For row count using DOM, we have tablename.rows.length to get number of rows, but we don't have 'cols.length' for column count.

How can we find the number of columns (only using the DOM)?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
mano
  • 113
  • 2
  • 2
  • 7

6 Answers6

7

I think you can use cells to calculate the column, assuming that the number of column of first row will be same for all

tablename.rows[0].cells.length;
senK
  • 2,782
  • 1
  • 27
  • 38
7

I would use the table's rows property and the first row's cells property and total the colSpan property of each cell in the row. This will work in all major browsers back to IE 4 and should be pretty fast.

Demo: http://jsfiddle.net/Gtdru/

Code:

function getTableColumnCount(table) {
    var columnCount = 0;
    var rows = table.rows;
    if (rows.length > 0) {
        var cells = rows[0].cells;
        for (var i = 0, len = cells.length; i < len; ++i) {
            columnCount += cells[i].colSpan;
        }
    }
    return columnCount;
}
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • This should be the correct answer. jQuery version here - http://stackoverflow.com/questions/6683882/jquery-how-to-count-table-columns – web-nomad Jan 03 '13 at 11:43
2

There isn't such a concept in the DOM.

You could try and count the max number of td and th in tr :

var max = 0;
$('#tableId tr').each(function(){max=Math.max(max, $('td,th', this).length)});

Demonstration

If you want to take into account the colspan, it's a little heavier :

var max = 0;
$('#tableId tr').each(function(){
    var inTr = 0;
    $('td,th', this).each(function() { inTr += parseInt($(this).attr('colspan')) || 1;});
    max = Math.max(max,inTr);
});    

Demonstration

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

This will work with complex table headers :

$($('#table_id_here tbody tr')[0]).find('td').length
italo.portinho
  • 121
  • 1
  • 5
0

Here you go:

$(function() {
    var colCount = 0;
    $('tr:nth-child(1) td').each(function () {
        if ($(this).prop('colspan')) {
            colCount += +$(this).prop('colspan');
        } else {
            colCount++;
        }
    });
    alert(colCount);
});​

jsfiddle

palaѕн
  • 72,112
  • 17
  • 116
  • 136
0

A very simple way to get the number of possible cols in any table is using the following (vanillaJS) function:

/**
 * Calculates the number of columns based on any row using colSpan attribute.
 *
 * @param {HTMLElement} table : The table element to be count.
 *
 * @return {int} The number of columns this table has.
 */
var getTableColSpan = function getTableColSpan(table) {

    var colSpan = 0; // initialize counter
    var trs = table.querySelectorAll('tr'); // get firt tr cells.

    for (var j = 0; j < trs.length; j++) {

        var tr = trs[j];
        var tds = tr.cells;

        var trColSpan = 0; // initialize counter

        // loops between columns and gets each one's colSpan
        for (var i = 0; i < tds.length; ++i) {
            trColSpan += tds[i].colSpan;
        }

        colSpan = trColSpan > colSpan ? trColSpan : colSpan;
    }

    return colSpan;
};
Machado
  • 8,965
  • 6
  • 43
  • 46