javascript - DataTables:使用 JQuery 更改所选单元格的值

标签 javascript jquery datatables

我有一个从数据库填充的数据表。我想更改所选单元格的值。

我不知道如何使用单元格和行索引更改单元格的值。我怎样才能做到这一点?

这就是我所拥有的:

  $('#dtBasicExample').on('click', 'tbody td', function() {

  var table = $('#dtBasicExample').DataTable();

  //Content I want to insert i the cell
  var NewValue= 'NewValue';

  //get cell index
  var CellIndex=table.cell( this ).index().columnVisible;

  //get row index
  var RowIndex= table.cell( this ).index().row;
})

最佳答案

要更改单元格中的数据,您需要 DataTables API 中的 cell().data() 函数:https://datatables.net/reference/api/cell().data()

$(document).ready(function() {
  var table = $('#example').DataTable();
  $('#example tbody').on('click', 'td', function() {
    var colIndex = table.cell(this).index().column;
    var rowIndex = table.cell(this).index().row;
    table.cell(rowIndex, colIndex).data("new")
  });
});

更简单的方法:

$(document).ready(function() {
  var table = $('#example').DataTable();
  $('#example tbody').on('click', 'td', function() {
     table.cell(this).data("new");
   });
});

关于javascript - DataTables:使用 JQuery 更改所选单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57103672/

相关文章:

javascript - Google Maps API v3 热图错误 : "Cannot read property ' HeatmapLayer' of undefined"

javascript - 在javascript中对对象的关联数组进行排序

jquery - 使用带有 Bootstrap 选项卡的固定列的数据表问题

javascript - 动态图像上传到备用 gridster 小部件

javascript - 在 javascript 图像 slider 中在图像上显示文本的问题

javascript - 窗口卸载时更新 mySQL 表

javascript - ie中的jquery标题选择器错误

javascript - 不支持 svg 的浏览器的 CSS hack

javascript - 使用ajax创建新的表行和表单提交

javascript - 在数据表中添加事件监听器和国际化