waldo2188 / DatatableBundle

Symfony2 Ajax Datatable Bundle to simplify the use of http://datatables.net/ (and Doctrine entity inside)
MIT License
17 stars 16 forks source link

You should set a datatable id in your action with "setDatatableId" using the id from your view #7

Closed InternalServerError closed 8 years ago

InternalServerError commented 8 years ago

Hi, I've an issue when implementing the datatable bundle. I've followed the doc, set up the bundle with composer etc.

In my controller :

public function userInformationsAction(Request $request, $mode = null') {
        $this->initManagerAndSession();
        $user = $this->getUser();

        if ($mode == 'showInvoices') {
            return $this->render('NutriFitBundle:User:userInformations.html.twig', [
                'user' => $user
            ]);
        } else {
            throw $this->createNotFoundException();
        }
    }

/**
     * Create DataTable for invoices list
     * 
     * @return unknown
     */
    private function computeDataTable() {
        $dt = $this->get('datatable')
                    ->setEntity('NutriFitBundle:Facture', 'f')
                    ->setDatatableId('invoice')
                    ->setFields(
                            [
                                'Reference' => 'f.reference',
                                'Date' => 'f.creationDate',
                                'Amount' => 'f.amount',
                                'Shipping Data' => 'f.shippingDate',
                                'Payment Type' => 'f.paymentType',
                                "_identifier_"  => 'f.id'
                            ]
                    )
                    ->setWhere('f.user = :user',
                            ['user' => $this->getUser()]
                    )
                    ->setOrder('f.creationDate', 'desc');

        return $dt;
    }

    /**
     * Build datatable view for user invoices
     * 
     * @param Request $request
     */
    public function invoicesDataTableAction(Request $request) {
        return $this->computeDataTable()->execute();
    }

In my twig :

{{ datatable({
                    'js' : {
                        'ajax' : path('invoices_datatables')
                    }
                })
            }}

When I'm accessing to this view, I've the following 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")

When I'm trying to query the url action directly, I've a correct JSON :

{"draw":0,"recordsTotal":"1","recordsFiltered":"1","data":[["jrgoireg-21",{"date":"2012-01-01 00:00:00","timezone_type":3,"timezone":"Europe\/Berlin"},25.5,{"date":"2012-01-02 00:00:00","timezone_type":3,"timezone":"Europe\/Berlin"},"CB",1]]}`

Could you help me for doing it work please ?

Thanks by advance,

Regards,

waldo2188 commented 8 years ago

Hey !

Just add an id in the datatable definition in your Twig template, like you can see below :

{{ datatable({
                    'id' : 'invoice'
                    'js' : {
                        'ajax' : path('invoices_datatables')
                    }
                })
            }}
InternalServerError commented 8 years ago

Hi,

Thanks for your answer. I've already tried it, without no success. However, with adding ->setHasAction(false) in controller, that seems to solve the problem :)

waldo2188 commented 8 years ago

Ok, if you have time, you can maybe try to reproduce your error on this repo https://github.com/waldo2188/datatable-sandbox and share the result. If you do, I will try to solve this problem.