Error when selecting rows with checkbox in datatables

Error when selecting rows with checkbox in datatables

NanoDevNanoDev Posts: 3Questions: 1Answers: 0

Hi, I have the following problem: I am following these tutorials to be able to save the data in array through each row selected by a checkbox.
Https://www.gyrocode.com/articles/jquery-datatables-checkboxes/
Https://jsfiddle.net/gyrocode/abhbs4x8/
As in the provided link, I receive data in JSON to load my dataTable, now I am seen in need of, by a button to bring new data to my dataTable for it and added some more functions to the code that is in the link that Provide, and are as follows:
To delete the datatable
function destroy_data_table(){
var table = $('#example').DataTable({
if (table != undefined){
table .fnDestroy();
}
}

In the click of the button I destroy my datatable and call the function that loads a new one.
$("#btnNewDatatable").click(function (){
destroy_data_table();
new_data();
});

And this is the function that reloads the datatable
function new_data(){
var table = $('#example').DataTable({
"sAjaxSource": //Here I return in json what my server sends,
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ){
oSettings.jqXHR = $.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": function(result){
fnCallback(result);
}
});
},
responsive: true,
'columnDefs': [{
'targets': 0,
'searchable':false,
'orderable':false,
'width':'1%',
'className': 'dt-body-center',
'render': function (data, type, full, meta){
return '<input type="checkbox">';
}
}]
});
}
My problem is that when loading the new databale, and when selecting a row, this continues to show me the data from the previous datatable, I made a console.log a var rows_selected = []; Which is in charge of storing the data of each selected row, I am badly destroying the table or what could be my mistake?

This discussion has been closed.