DataTables - 如何 get/set 一行中某个单元格的值?

DataTables - How do I get/set the value of a cell in a row?

我正在使用我在文档中找到的示例遍历 DataTable 的行,但我想获取第二列的数据,分析该值,然后在该单元格上设置我的处理值。

tablaProgDetalle.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
 // I suppose here goes the processing and the setting of the cells values.
});

解决方案

您可以在匿名函数中使用row().data() 来获取和设置行的数据。请注意,匿名函数中的变量 this 指的是正在迭代的行。

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

table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
    var data = this.data();        

    data[0] = '* ' + data[0];

    this.data(data);
});

演示

有关代码和演示,请参阅 this jsFiddle

您可以使用 rowID 来执行此操作;我们可以在 rowCreated: Datatable 的回调中设置 rowID,或者在 table 创建时手动插入行。在这里,我根据项目中的某些事件增加第 3 列的值。

//DOM only addition
var rowID = your rowID goes here; 

var cols = Table.row(rowID).data();

var count = parseInt(cols[2]);
count = count + 1
cols[2] = count.toString();
Table.row(rowID).data(cols);