我有一个从数据库填充的数据表。我想更改所选单元格的值。
我不知道如何使用单元格和行索引更改单元格的值。我怎样才能做到这一点?
这就是我所拥有的:
$('#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/