Unable to load a Datatable on click of a button
Unable to load a Datatable on click of a button
dwai
Posts: 1Questions: 1Answers: 0
I am trying to load a datatable on click of a button outside the datatable.
below is my piece of code
$('#buttonToLoadDatatable').on('click', function() {
$.ajax({
type: "GET",
url:"Called Rest API",
}).done(function (result) {
var table = $('#example').DataTable( {
"sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-
12 hidden-xs'l>r>"+
"t"+
"<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
"oLanguage": {
"sSearch": '<span class="input-group-addon"><i
class="glyphicon glyphicon-search"></i></span>'
},
"bDestroy": true,
"data":result,
"iDisplayLength": 15,
"columns": [
{
"class": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data":"email" }
],
rowCallback: function (row, data) {},
filter: false,
info: false,
ordering: false,
processing: true,
retrieve: true,
"fnDrawCallback": function( oSettings ) {
runAllCharts()
}
// rowCallback: function (row, data) {}
} );
console.log( result );
}).fail(function (jqXHR, textStatus, errorThrown) {
});
});
When I print the value in the browser console, it comes correctly as {"email": "triedandtest@gmail.com"}, but it does not display in the datatable
There is no error also in the console or at the backend
EDIT: Updated code formatting using Markdown
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Datatables is expecting the data to be in an array. As described in the data docs whether you are using arrays or objects they need to be in an array, even if just one. Your data should look like this:
[{"email": "triedandtest@gmail.com"}]
Kevin