Skip to content

Commit

Permalink
RowPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
paveljanda committed May 13, 2019
1 parent 5c885c0 commit 69db06e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contributte-datagrid/app/Presenters/ActionsPresenter.php
Expand Up @@ -9,7 +9,7 @@
use Ublaboo\DataGrid\Column\Action\Confirmation\StringConfirmation;
use Ublaboo\DataGrid\DataGrid;

final class ActionsPresenter extends Presenter
class ActionsPresenter extends Presenter
{

/**
Expand Down
Expand Up @@ -8,7 +8,7 @@
use Nette\Application\UI\Presenter;
use Ublaboo\DataGrid\DataGrid;

final class GroupActionsPresenter extends Presenter
class GroupActionsPresenter extends Presenter
{

/**
Expand Down
95 changes: 95 additions & 0 deletions contributte-datagrid/app/Presenters/RowPresenter.php
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace App\Presenters;

use Dibi\Connection;
use Nette\Application\UI\Presenter;
use Ublaboo\DataGrid\Column\Action\Confirmation\StringConfirmation;
use Ublaboo\DataGrid\DataGrid;

final class RowPresenter extends Presenter
{

/**
* @var Connection
* @inject
*/
public $dibiConnection;


public function createComponentGrid(): DataGrid
{
$grid = new DataGrid;

$grid->setDataSource($this->dibiConnection->select('*')->from('users'));

$grid->setItemsPerPageList([20, 50, 1]);

$grid->setRowCallback(function($item, $tr) {
$tr->addClass('super-' . $item->id);
});

$grid->addColumnNumber('id', 'Id')
->setAlign('left')
->setSortable();

$grid->addColumnText('name', 'Name')
->setSortable();

$grid->addColumnDateTime('birth_date', 'Birthday');

$grid->addAction('detail', '', 'this')
->setIcon('sun')
->setTitle('Detail');

$grid->addAction('delete', '', 'delete!')
->setIcon('trash')
->setTitle('Delete')
->setClass('btn btn-xs btn-danger ajax')
->setConfirmation(
new StringConfirmation('Do you really want to delete example %s?', 'name')
);

$grid->addGroupAction('Delete')->onSelect[] = [$this, 'groupDelete'];

$grid->allowRowsGroupAction(function($item): bool {
return $item->id % 2 === 0;
});

$grid->allowRowsAction('delete', function($item) {
return $item->id % 3 === 0;
});

$grid->allowRowsAction('detail', function($item) {
return $item->id % 4 === 0;
});

return $grid;
}


public function handleDelete(): void
{
$this->flashMessage('Deleted!', 'info');
$this->redrawControl('flashes');
}


public function groupDelete(array $ids): void
{
$this->flashMessage(
sprintf('These items: [%s] are being deleted', implode($ids, ',')),
'info'
);

if ($this->isAjax()) {
$this->redrawControl('flashes');
$this['grid']->redrawControl();
} else {
$this->redirect('this');
}
}
}

3 changes: 2 additions & 1 deletion contributte-datagrid/app/templates/@layout.latte
Expand Up @@ -42,7 +42,8 @@
'Columns',
'Filters',
'Actions',
'Group Actions'
'Group Actions',
'Row',
]}

{foreach $sections as $section}
Expand Down
3 changes: 3 additions & 0 deletions contributte-datagrid/app/templates/Row/default.latte
@@ -0,0 +1,3 @@
{block content}
{control grid}
{/block}

0 comments on commit 69db06e

Please sign in to comment.