afeef
afeef

Reputation: 4716

Uncaught SyntaxError: Unexpected token '&' in DataTable using Symfony2

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:

Upvotes: 0

Views: 1469

Answers (1)

Rey0bs
Rey0bs

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

Related Questions