french_dev
french_dev

Reputation: 2177

Symfony2/Datatable Bundle - error: set a datatable id in your action with "setDatatableId" using the id from in your view

I import the Bundle AliDatatableBundle on github following the doc step one by one except the part $ bin/vendor install in the installation because with Symfony2.6, there's no need to do this.

When I create the function in my controller, and the rendering in my twig, I have this error:

An exception has been thrown during the rendering of a template ("No instance found for datatable, you should set a datatable id in your action with "setDatatableId" using the id from your view ") in MySpaceGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig at line 37.

Here's the code for the controller:

/**
     * set datatable configs
     * 
     * @return \Ali\DatatableBundle\Util\Datatable
     */
    private function _datatable()
    {
        $controller_instance = $this;
        return $this->get('datatable')
                    ->setDatatableId('batiments')
                    //->setEntity("MySpaceDatabaseBundle:Batiments", "b")                          
                    // replace "XXXMyBundle:Entity" by your entity
                    ->setFields(
                            array(
                                "Nom"          => 'b.nom',                        
                                // Declaration for fields: 
                                "Reférence"        => 'b.referencebatiment',                    
                                // "label" => "alias.field_attribute_for_dql"
                                "Ensembles"          => 'b.ensembles',  
                                "_identifier_"  => 'b.id')                          
                                // you have to put the identifier field without label. Do not replace the "_identifier_"
                            )

                    //->setWhere(                                                     
                    // set your dql where statement
                         //'x.address = :address',
                         //array('address' => 'Paris') 
                    //)

                    //->setOrder("x.created", "desc")                                 
                    // it's also possible to set the default order
                    ->setHasAction(true);                                           
                    // you can disable action column from here by setting "false".
    }

    /**
     * Grid action
     * @return Response
     */
    public function gridAction()
    {   
        return $this->_datatable()->execute();                                      
        // call the "execute" method in your grid action
    }

    /**
     * Lists all entities.
     * @return Response
     */
    public function indexAction()
    {
        $this->_datatable();                                                        
        // call the datatable config initializer
        return $this->render('MySpaceGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig');                 
        // replace "XXXMyBundle:Module:index.html.twig" by yours
    }

And then the code for my twig:

{% extends "MySpaceWelcomeBundle::layout.html.twig" %}
{% block content %}
<div class="jumbotron">
              <h4><u>Rechercher un bâtiment et ses affectations spécifiques:</u></h4>
              <br>                

              <div>
                {{ datatable({ 
                      'id' : 'batiments',
                      'js' : {
                          'sAjaxSource' : path('mySpace_formulaire_recherche_batiments')
                      }
                  })
              }}
              </div>
            </div>
{% endblock %}

I really don't understand, someone could help me to fix this?

I have already clear the cache, install assets and took over again from the beginning the doc, I do not understand.

Thank you in advance.

Upvotes: 1

Views: 932

Answers (1)

user4460771
user4460771

Reputation:

It seems like everything was good. I can't see the problem here, if you followed the doc of aliBundle. Try using the Jquery datatable here, the installation is easier I think and better for your project if it's to manage your entities in Symfony.

If you have questions for Jquery DataTables, don't hesitate to ask, I'm using it for a Symfony project too.

Upvotes: 1

Related Questions