将表单元格的索引附加到自身,考虑行跨度和行跨度

Append index of table cell to itself considering rowspans and colspans

本文关键字:单元格 表单 索引      更新时间:2023-09-26

我想将表格单元格的索引附加到该单元格的文本中。我可以使用标准表执行此操作,但是当有合并的单元格时,我的代码不起作用。

代码如下:

$("td").each( function() {
    $(this).append($(this).index());
});

以下是标准表的结果:

|---|---|---|---|
| 0 | 1 | 2 | 3 |
|---|---|---|---|
| 0 | 1 | 2 | 3 |
|---|---|---|---|
| 0 | 1 | 2 | 3 |
|---|---|---|---|

下面是包含合并单元格的表的结果:

|---|---|---|---|
|   | 1 | 2 | 3 |
| 0 |---|---|---|
|   | 0 | 1 | 2 |
|---|---|---|---|
| 0 |   1   | 2 |
|---|---|---|---|

以下是我想为具有合并单元格的表格实现的目标:

|---|---|---|---|
|   | 1 | 2 | 3 |
| 0 |---|---|---|
|   | 1 | 2 | 3 |
|---|---|---|---|
| 0 |   1   | 3 |
|---|---|---|---|

有什么想法吗?

这是一个小提琴...http://jsfiddle.net/many_tentacles/5PQcF/

一种方法是:

var $highest;
$("table.writeIndex tr").each(function(){
$highest=$('td',this).length;
})
$("table.writeIndex tr").each( function() {
    var $td=$('td',this).length;  
    var count=$td;
    $("td",this).each(function(){
     var attr = $(this).attr('colspan');  
     if(typeof attr !== 'undefined' && attr !== false){
        count=count+parseInt(attr)-1;
         var previous=$(this).prev();
         while(previous.length>0){
         previous.html(previous.html()-1);
         previous=previous.prev();
         }
        $(this).append($highest-count);
        count=count-parseInt(attr);
        }else{
        $(this).append($highest-count);
        count=count-1;
        }
    })
});

工作演示小提琴

我找到的一个解决方案是使用 cellPos 插件从这个问题中获取

/*  cellPos jQuery plugin
    ---------------------
    Get visual position of cell in HTML table (or its block like thead).
    Return value is object with "top" and "left" properties set to row and column index of top-left cell corner.
    Example of use:
        $("#myTable tbody td").each(function(){ 
            $(this).text( $(this).cellPos().top +", "+ $(this).cellPos().left );
        });
*/
(function($){
    /* scan individual table and set "cellPos" data in the form { left: x-coord, top: y-coord } */
    function scanTable( $table ) {
        var m = [];
        $table.children( "tr" ).each( function( y, row ) {
            $( row ).children( "td, th" ).each( function( x, cell ) {
                var $cell = $( cell ),
                    cspan = $cell.attr( "colspan" ) | 0,
                    rspan = $cell.attr( "rowspan" ) | 0,
                    tx, ty;
                cspan = cspan ? cspan : 1;
                rspan = rspan ? rspan : 1;
                for( ; m[y] && m[y][x]; ++x );  //skip already occupied cells in current row
                for( tx = x; tx < x + cspan; ++tx ) {  //mark matrix elements occupied by current cell with true
                    for( ty = y; ty < y + rspan; ++ty ) {
                        if( !m[ty] ) {  //fill missing rows
                            m[ty] = [];
                        }
                        m[ty][tx] = true;
                    }
                }
                var pos = { top: y, left: x };
                $cell.data( "cellPos", pos );
            } );
        } );
    };
    /* plugin */
    $.fn.cellPos = function( rescan ) {
        var $cell = this.first(),
            pos = $cell.data( "cellPos" );
        if( !pos || rescan ) {
            var $table = $cell.closest( "table, thead, tbody, tfoot" );
            scanTable( $table );
        }
        pos = $cell.data( "cellPos" );
        return pos;
    }
})(jQuery);
jQuery(function(){
    $('td').html(function(){
        return $(this).cellPos().left
    })
})

演示:小提琴

试试这个

$("td").each( function(index) { // use index in function
    $(this).append(index);
});