Ajax call inside mRender function

Ajax call inside mRender function

ndethindethi Posts: 3Questions: 0Answers: 0
edited April 2013 in DataTables 1.8
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.

Replies

  • allanallan Posts: 61,663Questions: 1Answers: 10,095 Site admin
    Personally I would say that this would be a very bad idea. DataTables can call mRender 3 or 4 times for a single cell on first draw, and if you have say only 10 rows and 5 columns, that's up to 200 Ajax calls just to draw the page. Not good.

    Can't you just include the extra information in your data source?

    Allan
  • ndethindethi Posts: 3Questions: 0Answers: 0
    I see, thanks. Checked output on Firebug, too many Ajax calls, its inefficient. Let me try your suggestion, see how that works out. Thank you.
  • ndethindethi Posts: 3Questions: 0Answers: 0
    Hi,

    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.
This discussion has been closed.