ypnos-web / cakephp-datatables

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

Got error Column ordering requested after composer update #43

Closed suhardiyan closed 6 years ago

suhardiyan commented 6 years ago

Hi, I got problem when I run composer-update :

Updating ypnos-web/cakephp-datatables dev-php5 (c38275a => 3224608): Checking out 3224608f2b

After that, whenever I do filtering or searching datatable got error and shows :

code:500
file:"/Applications/XAMPP/xamppfiles/htdocs/sis-pm-nfbsl/vendor/ypnos-web/cakephp-datatables/src/Controller/Component/DataTablesComponent.php"
line:65
message:"Column ordering requested, but no column definitions provided."

Please help because the error comes after I update, thankyou

ypnos-web commented 6 years ago

This breaking change is a security measure, please see the README.

See here on how to fix this.

suhardiyan commented 6 years ago

Ok thanks solved now, I miss that readme,

lorro commented 6 years ago

Hi,

I stumbled upon the same error and read the reference to the README. However I wonder how we should use a callback if we need to define the columns in the controller. The DataTables component doesn't have a 'callback' function as that is a part of the datatables helper?

Before I did something like this in the view to make a column with a view and edit link:

[
    'data' => 'id',
    'searchable' => false,
    'sortable' => false,
    'width' => 75,
    'class' => 'text-center',
    'render' => $this->DataTables->callback('helpers.convertDataIds', [
        $this->Html->link($this->Html->icon_fa5('eye', ['type' => 'regular']), ['action' => 'view', '-dataid-'], ['class' => 'm-portlet__nav-link m-btn--icon-only', 'escape' => false, 'data-toggle' => 'tooltip', 'title' => __('View {0}', [strtolower(__('User'))])]) . SP . SP .
        $this->Html->link($this->Html->icon_fa5('pencil', ['type' => 'regular']), ['action' => 'edit', '-dataid-'], ['class' => 'm-portlet__nav-link', 'escape' => false, 'data-toggle' => 'tooltip', 'title' => __('Edit {0}', [strtolower(__('User'))])])
    ])
]
lorro commented 6 years ago

Apparently it's possible with adding this latest column manually at the back of the columns array in the view. Not really ideal I think, but that's probably the way to go now?

ypnos-web commented 6 years ago

Hi lorro,

I am sorry that the documentation is lacking in this regard (see #50).

The difficulty you are facing is due to separation of concerns. The controller should not really care about how a column is rendered, but on the other hand it needs to control which columns are provided.

If you are using version 2.x/3.x of the plugin, you can do it very conveniently. Example code:

Controller

        /* set up columns */
        $this->DataTables->columns()
            ->add('id', 'Users.id')->notVisible()->notSearchable() // for links
            ->add('firstname', 'Users.firstname')
            ->add('lastname', 'Users.lastname')
            ->add('username', 'Users.username')
            ->add('department.name', 'Departments.name')
            ->add('systemgroup.name', 'Systemgroups.name');

        /* query */
        $order = ['username' => 'asc'];
        $options = [
            'contain' => ['Departments', 'Systemgroups'],
            'conditions' => ['Users.active' => $showActive],
            'order' => $order
        ];
        $data = $this->fetchForDatatables('Users', 'all', $options);

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

View template

// accentuate usernames
$columns['username']['className'] = 'text-primary';
// add links
$columns['id']->set([
    'width' => 75,
    'class' => 'text-center',
    'render' => $this->DataTables->callback('helpers.convertDataIds', [
        $this->Html->link($this->Html->icon_fa5('eye', ['type' => 'regular']), ['action' => 'view', '-dataid-'], ['class' => 'm-portlet__nav-link m-btn--icon-only', 'escape' => false, 'data-toggle' => 'tooltip', 'title' => __('View {0}', [strtolower(__('User'))])]) . SP . SP .
        $this->Html->link($this->Html->icon_fa5('pencil', ['type' => 'regular']), ['action' => 'edit', '-dataid-'], ['class' => 'm-portlet__nav-link', 'escape' => false, 'data-toggle' => 'tooltip', 'title' => __('Edit {0}', [strtolower(__('User'))])])
    ]),
]);
…
// pass $columns and $order in options array to DataTable helper method for table rendering
lorro commented 6 years ago

Thanks for the feedback! Will give that a go. Looks better indeed that way.