2009-07-22 3 views
22

jQueryを使用すると、複数の列にまたがるセルに複数のインデックスがあるように、下のサンプルテーブルの任意のテーブルセルの列インデックスを見つける方法はありますか?それはおそらく、プラグインとして、あるいは延長を経由して、それを行うには意味をなさないと思いますテーブルに列スパニングセルが含まれているときにjQueryを使用して列インデックスを検索する

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); 

HTML

<table> 
    <tbody> 
    <tr> 
     <td>One</td> 
     <td>Two</td> 
     <td id="example1">Three</td> 
     <td>Four</td> 
     <td>Five</td> 
     <td>Six</td> 
    </tr> 
    <tr> 
     <td colspan="2">One</td> 
     <td colspan="2">Two</td> 
     <td colspan="2" id="example2">Three</td> 
    </tr> 
    <tr> 
     <td>One</td> 
     <td>Two</td> 
     <td>Three</td> 
     <td>Four</td> 
     <td>Five</td> 
     <td>Six</td> 
    </tr> 
    </tbody> 
</table> 

jQueryが

var cell = $("#example1"); 
var example1ColIndex = cell.parent("tr").children().index(cell); 
// == 2. This is fine. 

cell = $("#example2"); 
var example2ColumnIndex = cell.parent("tr").children().index(cell); 
// == 2. It should be 4 (or 5, but I only need the lowest). How can I do this? 
+1

あなたは自分で何かを書く必要があると思います(下のセスのように)。ただし、行スパンに注意すると、計算が難しくなります。 – aquinas

+3

@aquinas - rowspans ... ewww。 – seth

+0

このコードは私の探求に役立ちました! – mjbates7

答えて

29

ここでは、 'noncolspan'インデックスを計算できるプラグインがあります。

$(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; 
}; 
+1

関数の最初の行を(テーブルヘッダーをサポートするために)変更することを検討してください if(!$(this)).is( 'td')&&!$(this).is( 'th')) –

+1

良いアイデアAndre 。コードを更新しました。 – SolutionYogi

+0

@SolutionYogi、あなたの更新コードは と書いてあります。もし(!$(this) ).is( 'td')&&!$(this).is( 'th')) –

2

あなたはこのような何かを行うことができます。

5

鉱山はSolutionYogi年代と非常によく似て、マイナスのプラグインの作成。それは私に少し時間がかかりました...しかし、私はそれが

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

少し変更したバージョンはこちらです:)であるので、ここではまだそれを誇りに思っている:そこ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; 
}; 

関連する問題