Reputation: 4716
I need to append response in datatable
Symfony code:
public function ListUsersDataAction() {
$result = $this->getDoctrine()->getManager();
$person_data = $result->createQueryBuilder()
->select('u.id,u.email, u.phone_number,u.buy_min,u.buy_max,u.sell_min,u.sell_max,u.status,u.start_date,u.end_date')
->from('AcmeBitcoinBundle:AlertData', 'u')
->orderBy('u.id', 'asc')
;
//$sql=$results->getQuery();
//echo $sql->getSql();
$data = $person_data->getQuery()->getResult();
$json = '[';
$first = 0;
foreach ($data as $v) {
if ($first++)
$json .= ',';
$json .= '["' . $v['id'] . '",
"' . $v['email'] . '",
"' . $v['phone_number'] . '"]';
}
$json .= ']';
return $this->render('AcmeBitcoinBundle:Datatable:list.html.twig', array(
'json_data' => $json
));
}
JSON response:
[
[ "3","***@gmail.com","***"],
["4","**@gmail.com","43534654"]
]
twig file:
var dataSet={{json_data}}
$(document).ready(function() {
$('#example').dataTable( {
"data": dataSet,
"columns": [
{ "title": "Engine" },
{ "title": "Browser" },
{ "title": "Platform" }
]
} );
} );
</script>
References:
https://www.datatables.net/examples/data_sources/js_array.html
It's accepting:
var dataSet = [
['Misc','IE Mobile','Windows Mobile 6','-','C'],
['Misc','PSP browser','PSP','-','C'],
['Other browsers','All others','-','-','U']
];
i look into viewsource
<script>
var dataSet=[["3",
"[email protected]",
"24354"],["4",
"[email protected]",
"43534654"]]
$(document).ready(function() {
$('#example').dataTable( {
"data": dataSet,
"columns": [
{ "title": "Engine" },
{ "title": "Browser" },
{ "title": "Platform" }
]
} );
} );
solution i tried
var dataSet= {{ json_data|json_encode() }}
any suggestion is most welcome
update twig code
<table id="example" class="table">
</table>
$(document).ready(function() {
$('#example').dataTable( {
"data": {{ json_data|raw }},
"columns": [
{ "title": "Id" },
{ "title": "Email" },
{ "title": "Phone Number" }
]
} );
} );
Upvotes: 0
Views: 1469
Reputation: 1217
Try to add |raw
in your twig json variable, like var dataSet={{ json_data|raw }}
This will not escape output. https://twig.sensiolabs.org/doc/2.x/filters/raw.html
Upvotes: 1