Datatables trying to render from JS Array
Datatables trying to render from JS Array
ChrisF
Posts: 15Questions: 7Answers: 0
I am using CodeIgniter and saving a the results of a query into a php array which then I JSON encode and save it to a JS var from ajax. But whenever I try to retrieve the value my entire DataTable stops showing. What could be the issue?
Model:
public function get_casy($id) {
$this->db->select('SUM(Sekundy) AS Sekundy');
$this->db->from('cas_sledovania');
$this->db->join('vykres_has_ukon','vykres_has_ukon.ID_cas = cas_sledovania.ID_cas');
$this->db->where('ID_vykres', $id);
$query = $this->db->get();
return $query->row()->Sekundy;
}
Controller:
public function get_casy($id) {
$output = $this->Vykresy_model->get_casy($id);
$output = json_encode( $output );
$this->output->set_output( $output );
}
View:
"columnDefs": [
{
"targets": -6,
"render": function (data, type, full, meta) {
$.ajax({
url: "<?php echo site_url("vykresy/get_casy/")?>"+full[11],type : 'GET',
success: function( aResponse ) {
var response = $.parseJSON( aResponse );
}
});
return "";
}
}
]
I tried returning response[0][full[11]], response[0].Sekundy but nothing worked.
alert(response) does show the good values.
This discussion has been closed.
Answers
Hi @ChrisF,
I suspect the problem is because of the asynchronous nature of the Ajax call. By the time the Ajax request gets its response,
columns.render
would have already returned the empty string.If you could link to the page, or create an example demonstrating the problem, it would be easier to diagnose and make suggestions.
Cheers,
Colin
Hello @colin ,
thank you for your reply.
I believe I´ve shown all of the code I am using to make it work. Basically I am trying to add a new column for my table "Designs", each design is joined with the table "Times" and I want the SUM of seconds from the Times table where ID_design = that from row ( at column 11 thats why I am sending full[11 a parameter), I guess I better approach would be to have the join on my serverside script. But whenever I join 2 tables and I want a data from the 2nd table I get a response that it cant find it, even tho I followed other people´s ideas. (Using subquery, and removing ' ' around $table in my SSP class). Either way I am posting my SSP Class query aswell.
Table b has a field called "Name", but whenever I am calling this in my columns ( array( 'db' => 'Name', 'dt' => 12 ) it cant find it.
Yes.
You really don't want to be doing an Ajax call in the
columns.render
callback as Colin says. Either you need to disable the async nature which will kill performance, or you need to find a way of addressing the cell once the Ajax response is complete (possible, but you'd still be DDoSing your own server!).I would strongly encourage you to modify the server-side script to get the extra data. In this case it sounds like you'll need to loop over the data obtains by the SSP class and then execute an extra query for each row to get the required count. Not ideal, but better than using a new Ajax request. Your other option is to figure out how to get that one-to-many join information back from the SQL server (again possible, but that is very specific to whatever SQL engine you are using).
Allan