Ajax call inside mRender function
Ajax call inside mRender function
ndethi
Posts: 3Questions: 0Answers: 0
I am looking to call an ajax function inside the Datatables mRender function that will use an id from the present ajax source to get data from a different ajax source.
To put this into perspective, I have:
A listing of requisitions from different clients in one Datatable.
In this listing I have a client id. I want to get the client Name to be shown on the table instead of the client Id. However the Client Name and other client details are in the clients table hence these details are not in the requisitions json data. But I have the Client Id.
How do I use this to get client name inside the mrender function or Otherwise ? See what I have tried below:
[code]
{"sTitle":"Client", "sClass":"client",
"mRender":function(data, type, row){
$.getJSON('<?php echo base_url().'requisitions/getClientInfo/'?>' + row.client_id, function(clientdata){
var cdata = clientdata;
for(var i =0; i < cdata.length; i++){
var object = cdata[i];
for(var key in object){
var attrName = key;
var attrValue = object[key];
switch(attrName){
case 'Name':
var cname = attrValue;
break;
}
}
}
})
return cname;
}
[/code]
Thanks.
To put this into perspective, I have:
A listing of requisitions from different clients in one Datatable.
In this listing I have a client id. I want to get the client Name to be shown on the table instead of the client Id. However the Client Name and other client details are in the clients table hence these details are not in the requisitions json data. But I have the Client Id.
How do I use this to get client name inside the mrender function or Otherwise ? See what I have tried below:
[code]
{"sTitle":"Client", "sClass":"client",
"mRender":function(data, type, row){
$.getJSON('<?php echo base_url().'requisitions/getClientInfo/'?>' + row.client_id, function(clientdata){
var cdata = clientdata;
for(var i =0; i < cdata.length; i++){
var object = cdata[i];
for(var key in object){
var attrName = key;
var attrValue = object[key];
switch(attrName){
case 'Name':
var cname = attrValue;
break;
}
}
}
})
return cname;
}
[/code]
Thanks.
This discussion has been closed.
Replies
Can't you just include the extra information in your data source?
Allan
I added the extra info in the ajax data source. Thanks for that, had to learn a bit of using relations and foreign keys in Doctrine ORM. That works well.
However now the JSON has changed. Its now nested e.g as below:
[code]
[
{
"id":"78",
"packaging": "0",
"created_at": "2013-01-28 13:46:41",
"updated_at": "2013-01-28 13:46:41",
"Clients": {
"id": "6",
"Name": "Some Name",
"Address": "0000000",
"Email":"email@somedomain.com"
}
},
{
"id":"79",
"packaging": "0",
"created_at": "2013-01-28 13:46:41",
"updated_at": "2013-01-28 13:46:41",
"Clients": {
"id": "9",
"Name": "Some Other Name",
"Address": "0000001",
"Email":"email@someotherdomain.com"
}
}
]
[/code]
Checked the above on http://jsonlint.com . Says Valid.
How do I reference , say, the Client Name using mData?
Thanks.