2

私は次のように設定されたテーブルを持っています:

<table id="myTable">
  <thead>
    <tr>
      <td><input type="checkbox" id="selectall" /></td>
      <td>Column 1</td>
      <td>Column 2</td>
      <td>Column 3</td>
      <td>Column 4</td>
      <td>Column 5</td>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

次に、JavaScript:

var myTable = jQuery('#myTable').dataTable({
    /* options */
});

// Ajax request to populate:
jQuery.get('script.php', function(data){
    eval("rows="+data);
    for(var i=0;i<rows.length;i++){
        myTable.fnAddData([
          "<input type=\"checkbox\" id=\""+rows[i].uniqueID+"\" />",
          rows[i].col1Txt,
          rows[i].col2Txt,
          rows[i].col3Txt,
          rows[i].col4Txt,
          rows[i].col5Txt ]);
    }
});

現在、どのチェックボックスが選択されているかに基づいてテーブルを更新するのに問題があります。

チェックされている各行の 5 番目のセルを更新しようとしています。fnUpdatefnGetPosition( http://www.datatables.net/api )の組み合わせを使用しています。

fnGetPositiontdまたは要素が必要trなので、チェックボックスの親を取得するだけだと思いましたtd:

var checkBoxes = jQuery('td > input:checked', myTable);
for(var i=0;i<checkBoxes.length;i++){
    var parentTD = jQuery('#'+checkBoxes[i].id).parent(); //seems wrong?
    var pos = myTable.fnGetPosition(parentTD);
    //alert(pos[0]);
    myTable.fnUpdate('Updated text', pos[0], 5);
}

しかし、値を保持していないように見えるparentTDので、私は間違っているに違いありません。pos

何か案は?

4

1 に答える 1

7

each関数を使用して jQuery オブジェクトを反復処理できます。for ループを使用するよりも簡単です。

また、チェックされた入力を取得する代わりに、セレクターを最適化して td 要素を取得できると思います。

すべての操作で2つのセレクターを削除する必要があるため、パフォーマンスが大幅に向上します。私はそれを試していませんが、このようなものはうまくいくはずです

var checkBoxes = jQuery('td:has(input:checked):not(#selectall)', myTable);
checkboxes.each(function(){
    var pos = myTable.fnGetPosition($(this)); // Im not familiar with the plugin so the extra $() might be overkill
    alert(pos) // maybe run this alert again, check if you get back an object/value? use firebug to debug and see its value?
    myTable.fnUpdate('Updated text', pos[0], 5);
});
于 2010-09-20T14:42:49.130 に答える