【问题标题】:jquery datatables updating a cell更新单元格的jquery数据表
【发布时间】:2010-09-20 14:07:05
【问题描述】:

我有一张桌子,如下所示:

<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) 的组合。

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 似乎从来没有持有价值。

有什么想法吗?

【问题讨论】:

    标签: jquery datatables


    【解决方案1】:

    您可以使用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);
    });
    

    【讨论】:

    • 不错,很管用。我知道 each(),我只是无法收集有限的 jQuery 知识来获取所有这些 td。非常感谢。
    • @tombh:没问题,很高兴我能帮上忙。
    • 新的 API 现在怎么样了?
    猜你喜欢
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2011-01-27
    • 2017-11-20
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多