How to update data in datatable column

2019-05-31 00:02发布

Can someone help me loop through all the rows in a datatable column and update the data in it? So far I've found a way to loop through a column:

var table = $("#my_table").DataTable();
table.column(2)
    .data()
    .each(function(value, index) {
        console.log(value);
        value = 'abc123'; //this does not work
    });

However, I'm not sure how to update that value.. Can someone help?

Thanks in advance!

1条回答
狗以群分
2楼-- · 2019-05-31 01:03

column returns aggregated data for the entire table. Loop through rows instead. There is a every() helper function that makes life easier :

table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
   var data = this.data();
   data[2] += ' >> updated in loop' //append a string to every col #2
   this.data(data)
} )

Demo -> http://jsfiddle.net/qx84jw55/

查看更多
登录 后发表回答