我有一个表格,如下所示:
<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 ]);
}
});
现在,我在根据选中的复选框更新表格时遇到问题:
我正在尝试更新检查的每一行中的第五个单元格。我使用 fnUpdate
和 fnGetPosition
的组合 ( http://www.datatables.net/api )。
fnGetPosition
需要 td
或 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
似乎从来没有值。
有什么想法吗?
最佳答案
您可以使用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);
});
关于jquery数据表更新单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3752226/