(Message updated according to the help I got in the meantime)
I'm trying to get my jQuery dataTable to be populated dynamically via Ajax (both th's and td's).
To do so, I'm religiously following a jsfiddle script that I found around here and that seems to work.
The difference is that the person who wrote this script just hardcoded his "dataSet" variable prior to using it while I need to generate it automatically from php and then parse it in jQuery.
When I try to do so, all I get is:
TypeError: e is not an Object. (evaluating '"length"in e').
Islam helped me a great deal by chat in the meantime and it seems that the formatting and console.log(dataSet) are now ok.
Here is my (updated) attempt:
HTML:
<table id="example"></table>
PHP:
<?php
$dataset=array();
....
while($row = mysqli_fetch_assoc($sql)) {
.....
$array_tmp = array();
$array_tmp["Header1"] = $alias;
$array_tmp["Header2"] = $chambres;
$array_tmp["Header3"] = $adresse;
$dataset[] = $array_tmp;
}
....
echo json_encode($dataset, JSON_UNESCAPED_UNICODE);
?>
jQuery:
var my_columns = [];
var dataSet =[];
$.ajax({
type: "GET",
url: "myfile.php",
data: 'value=1',
datatype:'json',
cache: false,
success: function(response){
dataSet=response;
$.each( dataSet[0], function( key, value ) {
var my_item = {};
my_item.data = key;
my_item.title = key;
my_columns.push(my_item);
});
}
});
islam jsFiddled the code with some of my production data and it works but I still get the error message on my side. So, it's really a mystery.
What I show you here is exactly what I have. So there is nothing else different that is changing the game.
I use the same jQuery and dataTables than in the islam jsFiddled. Both are set and working as I use dataTables successfully except for this specific attempt of retrieving server data. I use no other library.
when I "console.log(dataSet)" on my side, here is what I get (which seems to be fine):
[
{
"Header1" : "tyurtyu",
"Header2" : "zertzert",
"Header3" : 123
},
{
"Header1" : "sdfsdfsd" ,
"Header2" : "dsfgsdfg",
"Header3" : 456
}
]
FYI, here is the commented call to dataTables that I don't use yet because I get the error message on the Ajax call already. At least, it'll show where my dataSet array is supposed to be used afterwards.
/*
var dataTable = $('#example').DataTable({
'bInfo' : false,
'paging' : false,
'scrollX': false,
'processing':false,
'sDom' : 'ltipr',
'data' : dataSet,
"columns": my_columns
});
*/
If I uncomment this call to the dataTable, I get another error message on top of the other:
TypeError: e is not an Object. (evaluating '"length"in e')
TypeError: undefined is not an object (evaluating 'e[i].aDataSort')
Any help would be appreciated.
UPDATE: I got rid of the first error message by changing the way I pass from "response" to "dataSet", like this:
success: function(response){
dataSet = JSON.parse(response);
//instead of "dataSet=response;"
$.each(dataSet[0], function(key, value) {
...
});
}
So now, I only have the second error message when trying to pass dataSet to the dataTable.
It seems that it's just a scope issue as console.log(dataSet) outside the ajax call, like here below won't output anything
var my_columns = [];
var dataSet =[];
$.ajax({
type: "GET",
url: "php/ajax/get_table_values.php",
data: 'value1=1',
datatype:'json',
cache: false,
success: function(response){
dataSet = JSON.parse(response);
//instead of "dataSet=response;"
$.each(dataSet[0], function(key, value) {
var my_item = {};
my_item.data = key;
my_item.title = key;
my_columns.push(my_item);
});
}
});
console.log(dataSet);
var dataTable = $('#example').DataTable({
'bInfo' : false,
'paging' : false,
'scrollX': false,
'processing':false,
'sDom' : 'ltipr',
'data' : dataSet,
"columns": my_columns
});
UPDATE: problem solved, see my other post below. Thank you all!