I have used https://datatables.net/reference/api/rows.add%28%29 link working but the data showing the table as [object,object]. How to show the object to string
I came across this problem too - I found the documentation to be less than clear. Their example on https://datatables.net/reference/api/rows.add() does not work when I pass my own dynamically created array of objects.
In order to get it working, you have to specify the columns' names when instantiating DataTables.
In any case, the below is a working solution.
var DataTable = $('#tableName').DataTable({
iDisplayLength: 15, // number of rows to display
columns: [
{ data: 'id' },
{ data: 'name' },
{ data: 'car' },
]
});
// assume this is a dynamically created array of objects
var persons = [
{
id: 1,
name: 'John',
car: 'Mercedes',
},
{
id: 2,
name: 'Dave',
car: 'BMW',
},
{
id: 3,
name: 'Ken',
car: 'Jeep',
},
];
DataTable.rows.add(persons).draw();