Finding column index using jQuery when table contains column-spanning cells

后端 未结 5 1353
日久生厌
日久生厌 2020-12-01 12:08

Using jQuery, how can I find the column index of an arbitrary table cell in the example table below, such that cells spanning multiple columns have multiple indexes?

相关标签:
5条回答
  • 2020-12-01 12:48

    Slightly modified version is here: http://jsfiddle.net/Lijo/uGKHB/13/

    //INDEX
    alert ( GetNonColSpanIndex ('Type'));
    
    function GetNonColSpanIndex(referenceHeaderCellValue) {
    
        var selectedCell = $("th").filter(function (i) {
            return ($.trim($(this).html()  )) == referenceHeaderCellValue;
    
        });
    
        alert(selectedCell.html());
    
        var allCells = $(selectedCell).parent('tr').children();
        var normalIndex = allCells.index($(selectedCell));
        var nonColSpanIndex = 0;
    
        allCells.each(
        function (i, item) {
            if (i == normalIndex)
                return false;
    
            var colspan = $(selectedCell).attr('colspan');
            colspan = colspan ? parseInt(colspan) : 1;
            nonColSpanIndex += colspan;
        }
        );
    
        return nonColSpanIndex;
    };
    

    0 讨论(0)
  • 2020-12-01 12:56

    Here's a plugin which can calculate the 'noncolspan' index.

    $(document).ready(
            function()
            {
            console.log($('#example2').getNonColSpanIndex()); //logs 4
            console.log($('#example1').getNonColSpanIndex()); //logs 2
        }
    
    );
    
    $.fn.getNonColSpanIndex = function() {
        if(! $(this).is('td') && ! $(this).is('th'))
            return -1;
    
        var allCells = this.parent('tr').children();
        var normalIndex = allCells.index(this);
        var nonColSpanIndex = 0;
    
        allCells.each(
            function(i, item)
            {
                if(i == normalIndex)
                    return false;
    
                var colspan = $(this).attr('colspan');
                colspan = colspan ? parseInt(colspan) : 1;
                nonColSpanIndex += colspan;
            }
        );
    
        return nonColSpanIndex;
    };
    
    0 讨论(0)
  • 2020-12-01 12:57

    You could do something like this:

     var index = 0;
     cell.parent('tr').children().each( 
         function(idx,node) { 
               if ($(node).attr('colspan')) {
                 index+=parseInt($(node).attr('colspan'),10); 
               } else {
                  index++;
               }
    
             return !(node === cell[0]);
         }
     );
     console.log(index);
    

    It'd probably make sense to do it as a plugin or via extend.

    0 讨论(0)
  • 2020-12-01 13:01

    Mine is quite similar to SolutionYogi's, minus the creation of a plugin. It took me a bit longer... but I'm still proud of it so here it is :)

    cell = $("#example2");
    var example2ColumnIndex2 = 0;
    
    cell.parent("tr").children().each(function () {
        if(cell.get(0) != this){
            var colIncrementor = $(this).attr("colspan");
            colIncrementor = colIncrementor ? colIncrementor : 1;
            example2ColumnIndex2 += parseInt(colIncrementor);
        }
    });
    console.log(example2ColumnIndex2);
    
    0 讨论(0)
  • 2020-12-01 13:06

    There is a more concise answer here: Get Index of a td considering the colspan using jquery

    In short:

    var index = 0;
    $("#example2").prevAll("td").each(function() {
        index += this.colSpan;
    });
    console.log(index);
    
    0 讨论(0)
提交回复
热议问题