row.add and render function
row.add and render function
Hello,
I'm initializing a datatable using AJAX, createdRow and some render functions on some columns
var tableDONNEES = $("#tableDONNEES").DataTable( {
"ajax": {
url: "myscript.php",
type: "GET",
data:{
'param' : "list"
}
},
...
"createdRow": function( row, data, dataIndex ) {
if (data['eds']=="NO") $('td:first-child', row).addClass('some_class');
},
...
"columns": [{name: 'id', data:"id", type: "num" },
{name: 'nom', data:"nom"},
{name: 'ville', data:"ville", render:function ( data, type, row ) {
if (data['link']=="") return data['CP']+" " + data['nom'];
return data['link'];
}},
{name: 'direction-nomprenom', data:"direction",
render:function ( data, type, row ) { return data['nomprenom']; }
},
{name: 'direction-tel', data:"direction",
render:function ( data, type, row ) { return data['tel'];}
}
],
...
It works perfectly but now, I'm trying to add a row using an AJAX call with success function here :
success: function(response) {
var rowNode = tableDONNEES.row.add({
"id": response.id,
"nom": nom,
"ville": $('select[name="ville"] option:selected', '#form').text(),
"direction-nomprenom": response.direction,
"direction-tel" : response.direction
} )
.draw();
}
The script which returns the "response" variable is like this :
$dataArray["id"] = $somenumber;
$dataArray["nom"] = $somenumber;
$dataArray["ville"] = $something;
$dataArray["direction"] = array();
$data =array();
$data["data"] = array("nomprenom"=>"something1", "tel"=>"something2");
$dataArray["direction"] = $data;
$dataArray["eds"]=$something; // "YES" or "NO"
echo json_encode($dataArray);
The add function does not work. Of course, I expect that the array returned by my script to the success function is not correctly formed. But I don't know how to correct it.
Could you help me please ?
Many thanks in advance !
T.
Answers
In what way does it not work? Any error messages or anything shown on the console?
Thanks,
Allan
Oups, sorry... There is the following message in the console :
TypeError: undefined is not an object (evaluating 'data['nomprenom']')
and the line where this error occurs is
render:function ( data, type, row ) { return data['nomprenom']; }
In the PHP code above you have:
But
$data
isn't actually used anywhere else - it isn't being output.I also don't see a
nomprenom
property in the data that you pass torow.add()
.Allan
$data is being used on line 7 : $dataArray["direction"] = $data;
The column named "direction-nomprenom" is supposed to get data named "direction" (because of 'data:"direction"' in the column declaration) and it's the data['nomprenom'] element which is used... So I thought that I had to output a json array with an item named "direction" who has a sub-array named "nomprenom"...
Doh - I missed that - sorry.
In that case, yes I agree it looks like it should work!
Can you give me a link to a page so I can debug the issue?
Thanks,
Allan
Hello Allan,
Thank you very much for your help.
I sent you a private message with login & password credentials.
Best regards,
T.
Hello Allan,
Did you find my private email with login informations ?
Thank you very much for your valuable help !
T.
Hi,
Yes, sorry I haven't got back to you about this yet. I've not had a chance to look into it yet, but will do so as soon as possible (probably Monday now) and let you know.
Allan
Thank you... :-)
T.
Thanks for the link.
If I put a break point into the rendering function where the error is happening I can see that the row's data structure after I've clicked the button is:
That gives an error because the column is configured as
data: "direction",
and as you can see above there is nodirection
object in that object.So the data being used for
row.add()
is not in the same format as the data that is being used to initially populate the table.Allan
Wow... I got it ! Thank you very, very much !!