jquery 数据表更新单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3752226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:02:28  来源:igfitidea点击:

jquery datatables updating a cell

jquerydatatables

提问by tombh

I have a table set out like:

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

<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>

Then, the javascript:

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

Now, I am having trouble with updating the table based on which checkboxes are selected:

现在,我无法根据选中的复选框更新表格:

I am trying to update the 5th cell in each row that is checked. I am using a combination of fnUpdateand fnGetPosition(http://www.datatables.net/api).

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

fnGetPositionexpects the tdor trelement, so I thought I'd just grab the parent tdof the checkboxes:

fnGetPosition期望tdortr元素,所以我想我只需要获取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);
}

But I must be doing parentTDwrong since posnever seems to hold a value.

但我一定是做错parentTD了,因为pos似乎从来没有价值。

Any ideas?

有任何想法吗?

回答by DannyLane

you can use the eachfunction to iterate over a jQuery object, its easier than using the for loop.

您可以使用each函数遍历 jQuery 对象,这比使用 for 循环更容易。

Also, I think that you can optomise your selector to get you the td elements instead of geting the checked inputs.

另外,我认为您可以优化选择器来获取 td 元素,而不是获取已检查的输入。

It will be a lot more performant as it should remove 2 selectors in every operation. I haven't tried it but something like this should work

它的性能会更高,因为它应该在每个操作中删除 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);
});