Mohan Srinivas

Mohan Srinivas

  • NA
  • 147
  • 52.9k

How to solve datatable warning cannot reinitialise datatable

May 23 2017 5:44 AM
I am created html  table using jquery and bind the data using json.Now i need pagination for that table so i added follow script:
 
function new1(){
$('#tblDynamic').DataTable({
initComplete: function() {
this.api().columns().every(function() {
debugger;
var column = this;
var select = $('')
.appendTo($(column.footer()).empty())
.on('change', function() {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
//to select and search from grid
debugger;
column
.search(val ? '^' + val + '$' : '', true, false)
.draw();
});
column.data().unique().sort().each(function(d, j) {
select.append('')
});
});
}
});
}
Call this function in generate table
 
function generateTable(json) {
var $table = $('#tblDynamic');
 
$table.find('thead').empty()
$table.find('tbody').empty()
if (json && json.length > 0) {
var header = json[0];
var columns = [];
for (var col in header) {
columns.push('' + col + '');
}
$table.find('thead').append('' + columns.join('') + '');
var rows = [];
for (var i = 0; i < json.length; i++) {
var row = json[i];
var tds = [];
for (var col in row) {
tds.push('' + row[col] + '');
// tds.push('' + row.city_name + '');
// tds.push('' + row.population + '');
//tds.push('' + row.year + '');
}
rows.push('' + tds.join() + '');
}
$table.find('tbody').append(rows.join(''));
} new1();
 
Getting error  datatable warning cannot reinitialise datatable,and displaying records are not correctly with out refresh the page
 

Answers (2)