Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update dataTables after adding raw html for a row

I'm adding a row to my table by passing raw html since I need some [data-] values in a couple of cells.

and I want that DataTables updates after this insertion, but I can't find how to do that.

I'm globally activating any table that has the class

$('.dataTable').DataTable(options);

then at some point I will add some row to a specific table:

let dataRow = '<tr data-bmid="' + j.id + '">';
dataRow = dataRow + '<td class="bmtitle">' + j.title + '</td>';
dataRow = dataRow + '<td data-color="'+j.colorCode+'">"+j.colorName + "</td>";
dataRow = dataRow + '</tr>';
$('#myTable tbody').append(dataRow);

I've tried:

$('#myTable').dataTable().draw();


$('#myTable').dataTable().update();


let dt = oTable.dataTable().api();
dt.row.add(dataRow);
dt.draw();

Hope you can help me find a solution to this

Thanks in advance!

like image 559
Abraham Romero Avatar asked Feb 03 '26 07:02

Abraham Romero


1 Answers

I have solved it.

the code is:

let dt = oTable.dataTable().api();
dt.row.add($(dataRow));
dt.draw();

I had been pretty close, all I was missing was enclose the html I had created in a jquery call: dataRow to $(dataRow)

Thanks to everybody

like image 98
Abraham Romero Avatar answered Feb 05 '26 21:02

Abraham Romero