Closed suhardiyan closed 6 years ago
Ok thanks solved now, I miss that readme,
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'))])])
])
]
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?
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:
/* 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);
// 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
Thanks for the feedback! Will give that a go. Looks better indeed that way.
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 :
Please help because the error comes after I update, thankyou