2009-09-02 5 views
1

私はクリックされたセルの行と列を強調表示するJavaScript関数を書いています。この関数は、前の行のセルを考慮に入れて、rowspanを使用して選択した行に下ろします。これにより、セルインデックスが「見た目」のインデックスと異なるためです。 I.表の第2列の各セルは必ずしもcellIndex==1である必要はありません。FireFox/JQuery/Dom:返されない 'rowIndex

補償するために、影響を受ける各セルの「オフセット」を計算する次の関数を記述しました。

function OffsetCells($tbody) { 
// if already indexed, we don't need to re-do it 
if (!$tbody.data('isOffset').value) { 
    // set offset for all cells to zero 
    $tbody.find('td').data('offset', { value: 0 }); 
    // it's not already indexed, so get the cells that span multiple rows 
    // capitalization of 'rowSpan' is important for IE 
    var $rowSpanners = $tbody.find('td[rowSpan!=1]'); 
    $rowSpanners.each(function() { 
     var $rowSpanningCell = $(this); 
     // we need to select all the cells to the 'apparent' right of this cell, 
     // so we need this cell's apparent position 
     // multiplying by one is easier than parseInt() to ensure conversion 
     $rowSpanningCell.data('apparentIndex', { value: this.cellIndex * 1 +  $rowSpanningCell.data('offset').value }); 
     // we also need to know what row this cell is in 
/*???*/  $rowSpanningCell.data('rowIndex', { value: $rowSpanningCell.parent('tr').get(0).rowIndex }); 
     // down to business: 
     $tbody.parent('table') // get the whole table 
     .find('tr')   // get all the rows in the table 
     .slice($rowSpanningCell.data('rowIndex').value + 1, $rowSpanningCell.data('rowIndex').value + this.rowSpan) // narrow selection to the applicable rows 
     .find('td')    // get the cells in the chosen rows 
     .filter(function(index) { // get the cells to the apparent right of this one. 
      return index + $(this).data('offset').value >= $rowSpanningCell.data('apparentIndex').value; 
     }).each(function() { 
      $(this).data('offset', { value: $(this).data('offset').value + 1 }); 
     }); 
    }); 
    $tbody.data('isOffset', { value: true }); 
} 
} 

このコードはIEでうまく機能しますが、/*???*/行では自動的に機能しません。私はそれを$rowSpanningCell.parent('tr').get(0).rowIndexの部分に絞り込んだ。私は考えることができるすべてを試しましたが、それでもrowIndexの値を返すことはできません。コードをalert($rowSpanningCell.parent('tr').get(0).nodeName)に変更すると、予想通りの<TR>が得られるので、自分の選択が正しいことがわかります。行のOTHERプロパティごとに1つおきの値が返されているようですが、rowIndexはコードを冷たくします。

答えて

1

あなたは、インデックス

+0

にあなたに私が見つけた問題を回避する別の方法を与える

$rowSpanningCell.parent('tr').prevAll().length

は$を使用するrowSpanningCell.parent()[0] .sectionRowIndexで試すことができます。 SectionRowIndexはサポートされており、両方で動作します。 –

+0

しかし、それは上記よりもはるかに判読不能です – redsquare

関連する問題