Reynier
Reynier

Reputation: 2478

Getting JSON error while trying to use the AliDatatableBundle

I'm trying to use the AliDatatableBundle and I follow every step as docs say, so this is how my controller looks like:

public function indexAction(Request $request) {
    $this->_datatable();
    return $this->render('PICommonBundle:Default:index.html.twig');
}

private function _datatable() {
    return $this->get('datatable')
                    ->setEntity("PIProyectoBundle:Proyectos", "p")
                    ->setFields(
                            array(
                                "Nombre" => 'p.nombre',
                                "Centros" => 'c.descripcion',
                                "Unidades" => 'u.descripcion',
                                "_identifier_" => 'p.id')
                    )
                    ->addJoin('p.centros', 'c', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN)
                    ->addJoin('p.unidades', 'u', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN);
}

public function gridAction() {
    return $this->_datatable()->execute();
}

And this is my template:

{% extends '::base.html.twig' %}
    {% block content %}
        <link href="{{ asset('bundles/alidatatable/css/demo_table.css') }}" type="text/css" rel="stylesheet" />
        <link href="{{ asset('bundles/alidatatable/css/smoothness/jquery-ui-1.8.4.custom.css') }}" type="text/css" rel="stylesheet" />
        <script type="text/javascript" src="{{ asset('bundles/alidatatable/js/jquery.datatable.inc.js') }}"></script>
        <script type="text/javascript" src="{{ asset('bundles/alidatatable/js/jquery.dataTables.min.js') }}"></script>    

        {{ 
            datatable({
                'edit_route' : 'editar-asistencia',
                'delete_route' : 'eliminar-asistencia',
                'js' : {
                    'sAjaxSource' : path('informes')
                }
            }) 
        }}
    {% endblock %}  

But any time I try to execute the page I get this error:

DataTables warning (table id = 'ali-dta_98f13708210194c475687be6106a3b84'): DataTables warning: JSON data from server could not be parsed. This is caused > by a JSON formatting error.

Any advice? I'm using latest jQuery 1.10.2.

Upvotes: 0

Views: 496

Answers (1)

Javier dc
Javier dc

Reputation: 575

are you spanish? (i´m too !!) I think your problem is whit sAjaxSource.

Read this tip: Ali en github

At the end of post comment:

"I took a look on your code and even tested it: your error is too simple , the "sAjaxSource" have to include the route for the grid action and not the index action "

In my case this solved:

In (bundle) routing.yml:

empleados_grid: pattern: /empleados_grid defaults: { _controller: MyBundle:Empleados:grid }

and in twig template:

{{ datatable({ 
    'edit_route' : 'empleados_update',
    'delete_route' : 'empleados_delete',
    'js' : {
        'sAjaxSource' : path('empleados_grid')
    }
})

}}

Upvotes: 1

Related Questions