jquery数据表更新单元格

标签 jquery datatables

我有一个表格,如下所示:

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

现在,我在根据选中的复选框更新表格时遇到问题:

我正在尝试更新检查的每一行中的第五个单元格。我使用 fnUpdatefnGetPosition 的组合 ( http://www.datatables.net/api )。

fnGetPosition 需要 tdtr 元素,所以我想我应该捕获父 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/

相关文章:

javascript - 使用 moment.js 将日期转换为字符串 "MM/dd/yyyy"

javascript - 我的 jQuery 悬停代码在悬停时重复

javascript - 如何在Jquery DataTable中添加按钮

javascript - 如何在ajax响应后重绘数据表

jquery - float 元素上的 Bootstrap Popover

jquery - 如何以编程方式设置 jQuery UI 切换?

php - 如何在jquery代码中传递数组值?

javascript - Jquery 数据表 : sort number with comma doesn't work

php - 在数据表 php 中获取复选框字段时出现问题

datatables - 如何以编程方式重新加载 jquery 数据表?