Reputation: 1731
I'm trying to populate this HTML table with DataTable jquery plugin, using an AJAX call receiving this JSON from the php script,
Response from the server:
{
"newsletters": [{
"anio": "2016",
"mes": "1",
"quincena": "1"
}, {
"anio": "2016",
"mes": "1",
"quincena": "2"
}]
}
HTML file:
<div id="tabla_newsletters" >
<table id="newsletter_datatable">
<thead>
<tr>
<th>anio</th>
<th>mes</th>
<th>quincena</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
var table = $('#newsletter_datatable').DataTable( {
ajax: {
url: '/newsletter/getNewsletters',
dataSrc: 'newsletters'
},
columns:[
{ 'newsletters': 'anio' },
{ 'newsletters': 'mes' },
{ 'newsletters': 'quincena' }
],
} );
});
</script>
and then my php service(made in symfony 1.4), as I stated before this is returning a correct JSON according to a JSON online validator:
public function executeGetNewsletters(sfWebRequest $request){
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
$qry=$conn->execute("Select anio,mes,quincena from newsletters");
$newsletters = $qry;
$dataNews = array();
$i=0;
foreach ($newsletters as $news)
{
$dataNews[$i] = array(
"anio" => $news['anio'],
"mes" => $news['mes'],
"quincena" => $news['quincena'],
);
++$i;
}
$output = array(
"newsletters" => $dataNews,
);
$json=$this->renderText(json_encode($output));
return $json;
}
Datatable throws the following error:
"DataTables warning: table id=newsletter_datatable - Requested unknown parameter '0' for row 0. For more information about this error, please see http://datatables.net/tn/4"
I look at other cases but they were different cases at a first glance.....
EDIT: I already solved it....the problem was on the script on the server, the indexes needed to be numbers....now it's populating the table correctly
Final code:
-Response from the server now is(apparently the plugin accepts only this format from what I've seen with other people's bugs):
{"newsletters":[["2016","1","1"],["2016","1","2"]]}
-HTML file code is:
<div id="tabla_newsletters" >
<table id="newsletter_datatable">
<thead>
<tr>
<th>anio</th>
<th>mes</th>
<th>quincena</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
var table = $('#newsletter_datatable').DataTable( {
ajax: {
url: '/newsletter/getNewsletters',
dataSrc: 'newsletters'
}
} );
});
</script>
and php service code is(I've only changed the indexes of the associative arrays from field names to numbers):
public function executeGetNewsletters(sfWebRequest $request){
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
$qry=$conn->execute("Select anio,mes,quincena from newsletters");
$newsletters = $qry;
$dataNews = array();
$i=0;
foreach ($newsletters as $news)
{
$dataNews[$i] = array(
"0" => $news['anio'],
"1" => $news['mes'],
"2" => $news['quincena'],
);
++$i;
}
$output = array(
"newsletters" => $dataNews,
);
$json=$this->renderText(json_encode($output));
return $json;
}
Upvotes: 2
Views: 84
Reputation: 85578
FYI : You could have solved your original problem simply by doing it right :
columns:[
{ data: 'anio' }, //NOT { 'newsletters': 'anio' }
{ data: 'mes' },
{ data: 'quincena' }
],
Use data
to define which attribute from each newsletter item that should go into the column.
Upvotes: 1