jQuery: How to count table columns?

前端 未结 15 1393
天命终不由人
天命终不由人 2020-12-28 13:20

Using jQuery, how would you figure out how many columns are in a table?



相关标签:
15条回答
  • 2020-12-28 13:33
    $("table").find("tr:first td").length;
    

    I edited as I didn't realize you were counting the colspan's.

    If you want to include using colspan try a loop through the td's in the first row:

    var cols = $("table").find("tr:first td");
    var count = 0;
    for(var i = 0; i < cols.length; i++)
    {
       var colspan = cols.eq(i).attr("colspan");
       if( colspan && colspan > 1)
       {
          count += colspan;
       }else{
          count++;
       }
    }
    
    0 讨论(0)
  • 2020-12-28 13:33

    To be robust..I'd do something like this

    alert(numCol("table") + " is the max number of cols");
    
    function numCol(table) {
        var maxColNum = 0;
    
        var i=0;
        var trs = $(table).find("tr");
    
        for ( i=0; i<trs.length; i++ ) {
            maxColNum = Math.max(maxColNum, getColForTr(trs[i]));
        }
    
        return maxColNum;
    }
    
    function getColForTr(tr) {
    
        var tds = $(tr).find("td");
    
        var numCols = 0;
    
        var i=0;
        for ( i=0; i<tds.length; i++ ) {
            var span = $(tds[i]).attr("colspan");
    
            if ( span )
                numCols += parseInt(span);
            else {
                numCols++;
            }
        }
        return numCols;
    }
    

    Just in case we have some funkiness going on between different rows.

    0 讨论(0)
  • 2020-12-28 13:34

    You have to set an ID to the header row:

    <table>
        <tr id="headerRow">
            <td>spans one column</td>
            <td colspan="2">spans two columns</td>
            <td colspan="3">spans three columns</td>
        </tr>
    </table>
    

    And then you can use the following function:

    function getColumnCount(headerRowId) {
        var columnCount = 0;
        $('#' + headerRowId + ' > td').each(function() {
            var colspanValue = $(this).attr('colspan');
            if (colspanValue == undefined) {
                columnCount++;
            } else {
                columnCount = columnCount + parseInt(colspanValue);
            }
        });
        return columnCount;
    }
    
    0 讨论(0)
  • 2020-12-28 13:39

    I simplified answer of Craig M. And modified to apply to both td and th tag.

    function GetColumnCount($Table)
    {
        var ColCount = 0;
        $Table.find("tr").eq(0).find("th,td").each(function ()
        {
            ColCount += $(this).attr("colspan") ? parseInt($(this).attr("colspan")) : 1;
        });
    
        return ColCount;
    }
    
    0 讨论(0)
  • 2020-12-28 13:40

    To circumvent the td/th issue (and also fix a potential issue where attr('colspan') was giving me strings) I went with this:

    var colspan = 0;
    $('#table').find('tr:first').children().each(function(){
        var cs = $(this).attr('colspan');
        if(cs > 0){ colspan += Number(cs); }
        else{ colspan++; }
    });
    
    0 讨论(0)
  • 2020-12-28 13:46

    Here you go:

    jsFiddle

    $(function() {
        var colCount = 0;
        $('tr:nth-child(1) td').each(function () {
            if ($(this).attr('colspan')) {
                colCount += +$(this).attr('colspan');
            } else {
                colCount++;
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题