/** * Get index DataTable * * @param Query $query CakePHP query * * @return Table */ protected function getIndexDataTable(Query $query) { $table = new Table('pages'); $titleCol = new Column(); $titleCol->setTitle('Title')->setData('Pages.title'); $authorCol = new Column(); $authorCol->setTitle('Author')->setData('Users.username')->setFormatter(function ($username, Page $page) { return $this->createView()->Html->link($username, ['author' => $page->getUserId()]); }); $commentsCol = new Column(); $commentsCol->setTitle('Comments')->setData('Pages.comment_count')->isSearchable(false); $dateCol = new Column(); $dateCol->setTitle('Date')->setData('Pages.created_at')->isSearchable(false)->setFormatter(function (Time $cell) { return $cell->nice(); }); $action = new Column\Action(); $action->setManager(function (Column\ActionBuilder $action, Page $page) { $action->addAction('view', __d('pages', 'View'), Router::url(['prefix' => false, 'controller' => 'Pages', 'action' => 'view', $page->getId()]), ['title' => __d('pages', 'View “%d”', $page->getTitle()), 'rel' => 'permalink']); $action->addAction('edit', __d('pages', 'Edit'), Router::url(['controller' => 'Pages', 'action' => 'edit', $page->getId()]), ['title' => __d('pages', 'Edit this item')]); $action->addAction('delete', __d('pages', 'Delete'), Router::url(['action' => 'delete', $page->getId()]), ['title' => __d('pages', 'Delete this item')]); })->setTitle('Action'); $table->addColumn($titleCol)->addColumn($authorCol)->addColumn($commentsCol)->addColumn($dateCol)->addColumn($action); $table->setDataSource(new CakePHP($query, $this->request->here())); return $table; }
/** * Get DataTable * * @return Table * @throws \DataTable\Exception */ protected function getDataTable() { TwigHelper::addCss('file:///Admin/vendor/datatables/media/css/jquery.dataTables.min.css', 100); TwigHelper::addJs('file:///Admin/vendor/jquery/dist/jquery.min.js', 20); TwigHelper::addJs('file:///Admin/vendor/datatables/media/js/jquery.dataTables.min.js', 100); TwigHelper::addJs(' function deleteUser(id) { if (confirm(\'Delete this user?\')) { $.ajax({ type: "DELETE", url: \'users/\' + id, success: function(affectedRows) { if (affectedRows > 0) window.location = \'users\'; } }); } }', 110); $table = new Table(); $col = new Column(); $col->setTitle('Username')->setData('Users.username'); $table->addColumn($col); $col = new Column(); $col->setTitle('Email')->setData('Users.email'); $table->addColumn($col); $col = new Column(); $col->setTitle('Status')->setData('Users.status')->isSearchable(false)->setFormatter(function ($status, User $user) { $statuses = [0 => 'Inactive', 1 => 'Active', 2 => 'Banned']; return $statuses[$status]; }); $table->addColumn($col); $router = $this->getRouter(); $col = new Column\Action(); $col->setManager(function (Column\ActionBuilder $action, User $user) use($router) { /* TODO Admin RBAC */ if (true) { $action->addAction('edit', 'Edit', $router->generateUrl(['users', $user->get('id')])); $action->addAction('delete', 'Delete', 'javascript:deleteUser("' . $user->get('id') . '");'); } })->setTitle('Actions'); $table->addColumn($col); $table->setDataSource(new CakePHP($this->getUsers(), $this->getRequest()->getRequestTarget())); return $table; }
private function generateDatatable() { TwigHelper::addCss('file:///Admin/vendor/datatables/media/css/jquery.dataTables.min.css', 100); TwigHelper::addJs('file:///Admin/vendor/jquery/dist/jquery.min.js', 20); TwigHelper::addJs('file:///Admin/vendor/datatables/media/js/jquery.dataTables.min.js', 100); TwigHelper::addJs(' function deletePage(id) { if (confirm(\'Delete this user?\')) { $.ajax({ type: "DELETE", url: \'pages/\' + id, success: function(affectedRows) { if (affectedRows > 0) window.location = \'pages\'; } }); } }', 110); /** @var \Cake\ORM\Table $pagesTable */ $pagesTable = TableRegistry::get('Pages.Pages'); $pages = $pagesTable->find(); $table = new Table(); $col = new Column(); $col->setTitle('Title')->setData('Pages.title'); $table->addColumn($col); $col = new Column(); $col->setTitle('Slug')->setData('Pages.slug'); $table->addColumn($col); $router = $this->getRouter(); $col = new Column\Action(); $col->setManager(function (Column\ActionBuilder $action, Entity $page) use($router) { if (true) { $action->addAction('edit', 'Edit', $router->generateUrl(['pages', $page->get('slug'), 'edit'])); $action->addAction('delete', 'Delete', 'javascript:deletePage("' . $page->get('slug') . '");'); } })->setTitle('Actions'); $table->addColumn($col); $table->setDataSource(new CakePHP($pages, $this->getRequest()->getRequestTarget())); if ($this->getRequest()->isAjax()) { die($table->getResponse()); } $this->getResponder()->setData('table', $table->render()); $this->getResponder()->setData('pages', $pages); }
/** * Get DataTable * * @return Table * @throws \DataTable\Exception */ protected function getDataTable() { TwigHelper::addCss('file:///Admin/vendor/datatables/media/css/jquery.dataTables.min.css', 100); TwigHelper::addJs('file:///Admin/vendor/jquery/dist/jquery.min.js', 20); TwigHelper::addJs('file:///Admin/vendor/datatables/media/js/jquery.dataTables.min.js', 100); TwigHelper::addJs(' function deleteRole(id) { if (confirm(\'Delete this role?\')) { $.ajax({ type: "DELETE", url: \'roles/\' + id, success: function(affectedRows) { if (affectedRows > 0) window.location = \'users/roles\'; } }); } }', 110); $table = new Table(); $col = new Column(); $col->setTitle('name')->setData('Roles.name'); $table->addColumn($col); $col = new Column(); $col->setTitle('title')->setData('Roles.title'); $table->addColumn($col); $router = $this->getRouter(); $col = new Column\Action(); $col->setManager(function (Column\ActionBuilder $action, Role $role) use($router) { /* TODO Admin RBAC */ if (true) { $action->addAction('edit', 'Edit', $router->generateUrl(['users', 'roles', $role->get('id')])); $action->addAction('delete', 'Delete', 'javascript:deleteRole("' . $role->get('id') . '");'); } })->setTitle('Actions'); $table->addColumn($col); $table->setDataSource(new CakePHP($this->getRoles(), $this->getRequest()->getRequestTarget())); return $table; }
/** * Get DataTable * * @return Table * @throws \DataTable\Exception */ protected function getDataTable() { TwigHelper::addCss('file:///Admin/vendor/datatables/media/css/jquery.dataTables.min.css', 100); TwigHelper::addJs('file:///Admin/vendor/datatables/media/js/jquery.dataTables.min.js', 100); TwigHelper::addJs(' function deleteCategory(id) { if (confirm(\'Delete this category?\')) { $.ajax({ type: "DELETE", url: \'categories/\' + id, success: function(affectedRows) { if (affectedRows > 0) window.location = \'categories\'; } }); } }', 110); $table = new Table(); $col = new Column(); $col->setTitle('Slug')->setData('Categories.slug'); $table->addColumn($col); $col = new Column(); $col->setTitle('Title')->setData('Categories.title'); $table->addColumn($col); $col = new Column(); $col->setTitle('Scope')->setData('Categories.scope'); $table->addColumn($col); $col = new Column(); $col->setTitle('Parent')->setData('Categories.parent_id')->isSearchable(false)->setFormatter(function ($parentId, Category $category) { if (null === $parentId) { return '-'; } return $category->parentCategory->get('title'); }); $table->addColumn($col); $router = $this->getRouter(); $col = new Column\Action(); $col->setManager(function (Column\ActionBuilder $action, Category $category) use($router) { /* TODO Admin RBAC */ if (true) { $action->addAction('edit', 'Edit', $router->generateUrl(['categories', $category->get('id')])); $action->addAction('delete', 'Delete', 'javascript:deleteCategory("' . $category->get('id') . '");'); } })->setTitle('Actions'); $table->addColumn($col); $table->setDataSource(new CakePHP($this->getCategories(), $this->getRequest()->getRequestTarget())); return $table; }