-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathgrid.php
More file actions
108 lines (87 loc) · 3.77 KB
/
grid.php
File metadata and controls
108 lines (87 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace Atk4\Ui\Demos;
use Atk4\Data\Model;
use Atk4\Ui\App;
use Atk4\Ui\Button;
use Atk4\Ui\Form;
use Atk4\Ui\Grid;
use Atk4\Ui\Js\Jquery;
use Atk4\Ui\Js\JsBlock;
use Atk4\Ui\Js\JsExpression;
use Atk4\Ui\Js\JsReload;
use Atk4\Ui\Js\JsToast;
use Atk4\Ui\Message;
use Atk4\Ui\Table;
use Atk4\Ui\UserAction\BasicExecutor;
use Atk4\Ui\View;
/** @var App $app */
require_once __DIR__ . '/../init-app.php';
$grid = Grid::addTo($app);
$model = new Country($app->db);
$model->addUserAction('test', static function (Model $entity) {
return 'test from ' . $entity->getTitle() . ' was successful!';
});
$grid->setModel($model);
// add country flag column
$grid->addColumn('flag', [
Table\Column\CountryFlag::class,
'codeField' => $model->fieldName()->iso,
'nameField' => $model->fieldName()->name,
]);
// adding Quicksearch on Name field using auto query
$grid->addQuickSearch([$model->fieldName()->name], true);
if ($grid->stickyGet('no-ajax')) {
$grid->quickSearch->useAjax = false;
}
$grid->menu->addItem(['Add Country', 'icon' => 'add square'], new JsExpression('alert(123)'));
$grid->menu->addItem(['Re-Import', 'icon' => 'power'], new JsReload($grid));
$grid->menu->addItem(['Delete All', 'icon' => 'trash', 'class.red active' => true]);
$grid->addColumn(null, [Table\Column\Template::class, 'hello<b>world</b>']);
// creating a button for executing model test user action
$grid->addExecutorButton($grid->getExecutorFactory()->createExecutor($model->getUserAction('test'), $grid));
$grid->addActionButton('Say HI', static function (Jquery $j, WrappedId $id) use ($grid) {
$model = Country::assertInstanceOf($grid->model);
return new JsToast('Loaded "' . $model->load($id)->name . '" from ID=' . $id->getId());
});
$grid->addModalAction(['icon' => 'external'], 'Modal Test', static function (View $p, WrappedId $id) {
Message::addTo($p, ['Clicked on ID=' . $id->getId()]);
});
// creating an executor for delete action
$deleteExecutor = $grid->getExecutorFactory()->createExecutor($model->getUserAction('delete'), $grid);
$deleteExecutor->onHook(BasicExecutor::HOOK_AFTER_EXECUTE, static function () {
return [
(new Jquery())->closest('tr')->transition('fade left'),
new JsToast('Simulating delete in demo mode.'),
];
});
// TODO button is added not only to the table rows, but also below the table!
// $grid->addExecutorButton($deleteExecutor, new Button(['icon' => 'times circle outline']));
$grid->addSelection();
$grid->addBulkAction(['Show selected', 'icon' => 'binoculars'], static function (Jquery $j, array $ids) use ($grid) {
return new JsToast('Selected: ' . implode(', ', array_map(static fn ($id) => $grid->getApp()->uiPersistence->typecastSaveField($grid->model->getIdField(), $id), $ids)) . '#');
});
// executing a modal on a bulk selection
$grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], '', static function (View $modal, array $ids) use ($grid) {
Message::addTo($modal, [
'The selected records will be permanently deleted: ' . implode(', ', array_map(static fn ($id) => $grid->getApp()->uiPersistence->typecastSaveField($grid->model->getIdField(), $id), $ids)) . '#',
'type' => 'warning',
'icon' => 'warning',
]);
$form = Form::addTo($modal);
$form->buttonSave->set('Delete');
$form->buttonSave->icon = 'trash';
$form->onSubmit(static function (Form $form) use ($grid, $ids) {
$grid->model->atomic(static function () use ($grid, $ids) {
foreach ($ids as $id) {
$grid->model->delete($id);
}
});
return new JsBlock([
$grid->jsReload(),
$form->jsSuccess(),
]);
});
});
// setting ipp with an array will add an ItemPerPageSelector to paginator
$grid->setIpp([10, 100, 1000]);