未捕获的SyntaxError:意外的令牌'&'在DataTable中使用Symfony2

时间:2017-07-03 07:20:14

标签: jquery symfony datatables

我需要在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>

参考文献:

  • 我试过Loading data array into datatables using ajax
  • 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=[[&quot;3&quot;,
          &quot;hhh@gmail.com&quot;,
    
       &quot;24354&quot;],[&quot;4&quot;,
       &quot;25435@gmail.com&quot;,
    
       &quot;43534654&quot;]]
      $(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" }
    
        ]
    } );   
    

    });      

1 个答案:

答案 0 :(得分:1)

尝试在twig json变量中添加|raw,例如var dataSet={{ json_data|raw }}

这不会逃避输出。 https://twig.sensiolabs.org/doc/2.x/filters/raw.html