ypnos-web / cakephp-datatables

CakePHP3 Plugin for DataTables plug-in for jQuery
MIT License
27 stars 24 forks source link

Call to undefined method DataTables\Controller\Component\DataTablesComponent::callback() #45

Closed michelmotta closed 6 years ago

michelmotta commented 6 years ago

I followed the Quick Start Tutorial and everything is working fine. The search and filter are also working fine. I'm having a problem when I try to use 'render' => $this->DataTables->callback('dt.render.date').

For some reason I'm getting an error.

Call to undefined method DataTables\Controller\Component\DataTablesComponent::callback()

Is there something else to do?

msanatan commented 6 years ago

Can you share some code? The callback function is available through the DataTables Helper which is available in the templates, not the controller. So if you're defining render in the controller you won't find that function. If you're like me, loading columns in the controller and want to render, put something like this in the view before creating the table: $columns[4]['render'] = $this->DataTables->callback('showImage');

Of course, replacing 4 with the appropriate index for your code.

michelmotta commented 6 years ago

You're perfectly right @msanatan I'm defining all columns in the controller. That's the reason I can't find the callback function. Your suggestion to add $columns[4]['render'] = $this->DataTables->callback('showImage'); inside the view solved the problem. Thanks for your help.

ypnos-web commented 6 years ago

If you have some free time, you can try the current alternative in devel branch, e.g.:

Controller:

        /* set up columns */
        $this->DataTables->columns()
            ->add('id', 'Experiments.id')->notVisible()->notSearchable()
            ->add('name', 'Experiments.name')
            ->add('users')->notSearchable()->notOrderable()
            ->add('created', 'Experiments.created')->notSearchable()
            ->add('modified', 'Experiments.modified')->notSearchable();

        … // finder call (columns need not be passed here)

        $this->set('columns', $this->DataTables->columns());

In Template:

$columns->setTitles([null,
    __('Name'), __('Team'), __('Started'), __('Last Update')
]);
$columns['users']->render('dt.render.users', [$user_id]);
$columns['created']->render('dt.render.date');
$columns['modified']->render('dt.render.lastupdate', [$user_id]);

… // pass 'columns' => $columns in table options

The new interface is a help for keeping a good separation between controller and view, e.g. by allowing you to use column identifiers instead of a numerical index when altering the column in the view template.

See also this commit, allowing you to set order once in controller and pass it on as a view variable to set as a table option.

msanatan commented 6 years ago

You're welcome @michelmotta! And thanks of the share @ypnos-web, new interface looks much cleaner.