我需要在datatable
Symfony代码:
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响应:
[
[ "3","***@gmail.com","***"],
["4","**@gmail.com","43534654"]
]
twig文件:
var dataSet={{json_data}}
$(document).ready(function() {
$('#example').dataTable( {
"data": dataSet,
"columns": [
{ "title": "Engine" },
{ "title": "Browser" },
{ "title": "Platform" }
]
} );
} );
</script>
参考文献:
https://www.datatables.net/examples/data_sources/js_array.html
它正在接受:
var dataSet = [
['Misc','IE Mobile','Windows Mobile 6','-','C'],
['Misc','PSP browser','PSP','-','C'],
['Other browsers','All others','-','-','U']
];
我查看了viewsource
<script>
var dataSet=[["3",
"hhh@gmail.com",
"24354"],["4",
"25435@gmail.com",
"43534654"]]
$(document).ready(function() {
$('#example').dataTable( {
"data": dataSet,
"columns": [
{ "title": "Engine" },
{ "title": "Browser" },
{ "title": "Platform" }
]
} );
});
我试过的解决方案
var dataSet= {{ json_data|json_encode() }}
欢迎提出任何建议
更新树枝代码
<table id="example" class="table">
</table>
$(document).ready(function() {
$('#example').dataTable( {
"data": {{ json_data|raw }},
"columns": [
{ "title": "Id" },
{ "title": "Email" },
{ "title": "Phone Number" }
]
} );
});
答案 0 :(得分:1)
尝试在twig json变量中添加|raw
,例如var dataSet={{ json_data|raw }}
这不会逃避输出。 https://twig.sensiolabs.org/doc/2.x/filters/raw.html