From c5997b716d831915e32c7400e3bc55a51cadd7b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Wed, 23 May 2018 16:50:02 +0200 Subject: [PATCH 01/17] typo --- src/Datagrid.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index 01a315a..4fb9f0c 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -13,7 +13,6 @@ use Nette\Bridges\ApplicationLatte\Template; use Nette\Forms\Container; use Nette\Forms\Controls\Button; -use Nette\Forms\Controls\Checkbox; use Nette\Utils\Html; use Nette\Utils\Paginator; use Nette\Localization\ITranslator; From c0e35679c5c5ae1f673628b02afc6a0746e12158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Wed, 23 May 2018 17:48:24 +0200 Subject: [PATCH 02/17] Throw exception when data source callback is not set It suppress warning: call_user_func() expects parameter 1 to be a valid callback --- src/Datagrid.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Datagrid.php b/src/Datagrid.php index 4fb9f0c..906f084 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -336,6 +336,10 @@ protected function attached($presenter) protected function getData($key = null) { if (!$this->data) { + if ($this->dataSourceCallback === null) { + throw new \Exception('Data source callback is not set. Set it by ' . __CLASS__ . '::setDataSourceCallback().'); + } + $onlyRow = $key !== null && $this->presenter->isAjax(); if ($this->orderColumn !== NULL && !isset($this->columns[$this->orderColumn])) { From e1f5c8506b9dc95a51506e2db4c31dd29e386aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Wed, 23 May 2018 17:51:13 +0200 Subject: [PATCH 03/17] setTranslator() is nullable --- src/Datagrid.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index 906f084..d114e4c 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -71,7 +71,7 @@ class Datagrid extends UI\Control /** @var Paginator */ protected $paginator; - /** @var ITranslator */ + /** @var ITranslator|null */ protected $translator; /** @var callable|null */ @@ -241,7 +241,7 @@ public function getCellsTemplates() } - public function setTranslator(ITranslator $translator) + public function setTranslator(ITranslator $translator = null) { $this->translator = $translator; } From cc420ce944d9ac8df98b42f888fb57c5ea94e9cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Wed, 23 May 2018 17:54:08 +0200 Subject: [PATCH 04/17] fix: render action column when filter exists Filter buttons 'Filter' and 'Cancel' are renderen in an actions column. --- src/Datagrid.latte | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Datagrid.latte b/src/Datagrid.latte index 5dd6b3d..70a7842 100644 --- a/src/Datagrid.latte +++ b/src/Datagrid.latte @@ -21,7 +21,8 @@ {php $hasActionsColumn = (bool) $control->getEditFormFactory() /* we may render only one row so the form[filter] may not be created */ - || isset($this->blockQueue["row-actions"]); + || isset($this->blockQueue["row-actions"]) + || isset($form["filter"]); $hasGlobalActionsColumn = isset($form['actions']); foreach ($_templates as $_template): From 4980b48e0592d0924ff00368214ae28ef49afccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Wed, 23 May 2018 18:10:16 +0200 Subject: [PATCH 05/17] Added posibility to set own form factory Form can be already customized by renderer. Translator can be already set. --- src/Datagrid.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index d114e4c..d5b03ab 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -53,6 +53,9 @@ class Datagrid extends UI\Control /** @var callable */ protected $dataSourceCallback; + /** @var callable|null */ + protected $formFactory; + /** @var callable|null */ protected $editFormFactory; @@ -159,6 +162,18 @@ public function getDataSourceCallback() } + public function setFormFactory(callable $formFactory) + { + $this->formFactory = $formFactory; + } + + + public function getFormFactory() + { + return $this->formFactory; + } + + public function setEditFormFactory(callable $editFormFactory = null) { $this->editFormFactory = $editFormFactory; @@ -431,7 +446,14 @@ public function handleSort() public function createComponentForm() { - $form = new UI\Form; + $form = $this->formFactory === null + ? new UI\Form + : call_user_func($this->formFactory, $this); + + if (!$form instanceof UI\Form) { + $type = is_object($form) ? get_class($form) : gettype($form); + throw new \Exception('Form factory callback has to return ' . UI\Form::class . ", but $type returned."); + } if ($this->filterFormFactory) { $form['filter'] = call_user_func($this->filterFormFactory); From e042f45d1574fd4d28be7457c9b04856f17a9b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Wed, 23 May 2018 18:22:54 +0200 Subject: [PATCH 06/17] Added possibility to set form action anchor --- src/Datagrid.latte | 2 +- src/Datagrid.php | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Datagrid.latte b/src/Datagrid.latte index 70a7842..c578e13 100644 --- a/src/Datagrid.latte +++ b/src/Datagrid.latte @@ -4,7 +4,7 @@ * @license MIT * @link https://github.com/nextras *} -
+
{snippet rows} {var $_templates = []} diff --git a/src/Datagrid.php b/src/Datagrid.php index d5b03ab..3c35fce 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -41,6 +41,9 @@ class Datagrid extends UI\Control /** @persistent */ public $page = 1; + /** @var string|null */ + protected $anchor; + /** @var array */ protected $filterDataSource = []; @@ -138,6 +141,20 @@ public function getRowPrimaryKey() } + public function setAnchor($anchor = '') + { + $this->anchor = $anchor === '' + ? $this->getUniqueId() + : $anchor; + } + + + public function getAnchor() + { + return $this->anchor; + } + + public function setColumnGetterCallback(callable $getterCallback = null) { $this->columnGetterCallback = $getterCallback; @@ -455,6 +472,12 @@ public function createComponentForm() throw new \Exception('Form factory callback has to return ' . UI\Form::class . ", but $type returned."); } + if ($this->anchor !== null) { + $form->onAnchor[] = function (UI\Form $form) { + $form->setAction($form->getAction() . '#' . $this->getAnchor()); + }; + } + if ($this->filterFormFactory) { $form['filter'] = call_user_func($this->filterFormFactory); if (!isset($form['filter']['filter'])) { From 52b21a00e685f090a07cae8dd019f8f4054234f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Wed, 23 May 2018 18:27:30 +0200 Subject: [PATCH 07/17] Added possibility to prepend cell template Helps when adding templates on different places. By factory or by descendant __constructor(). --- src/Datagrid.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index 3c35fce..45b9fcb 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -252,8 +252,9 @@ public function setPagination($itemsPerPage, callable $itemsCountCallback = null /** * @param string|Template $path + * @param bool $append */ - public function addCellsTemplate($path) + public function addCellsTemplate($path, $append = true) { if ($path instanceof Template) { $path = $path->getFile(); @@ -261,7 +262,12 @@ public function addCellsTemplate($path) if (!file_exists($path)) { throw new \InvalidArgumentException("Template '{$path}' does not exist."); } - $this->cellsTemplates[] = $path; + + if ($append) { + $this->cellsTemplates[] = $path; + } else { + array_unshift($this->cellsTemplates, $path); + } } From d81e42f1c61e2048189e9164a0ea9eac84b6c3c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Thu, 24 May 2018 17:54:16 +0200 Subject: [PATCH 08/17] Row edit controls rendered via #row-edit-control block --- src/Datagrid.blocks.latte | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Datagrid.blocks.latte b/src/Datagrid.blocks.latte index c740e3b..105e1bf 100644 --- a/src/Datagrid.blocks.latte +++ b/src/Datagrid.blocks.latte @@ -91,6 +91,13 @@ {$control->translate(Edit)} {/define} +{define row-edit-control} + {input $column->name} + {if $form[edit][$column->name]->hasErrors()} +

{$error}

+ {/if} +{/define} + {define row} {include #row-inner row => $row} @@ -117,10 +124,7 @@ {include #"cell-edit-{$column->name}" form => $form, column => $column, row => $row} {else} {formContainer edit} - {input $column->name} - {if $form[edit][$column->name]->hasErrors()} -

{$error}

- {/if} + {include #row-edit-control form => $form, formContainer => $formContainer, column => $column, row => $row} {/formContainer} {/ifset} From 6ed4cf30d2f652df5e7dbd12f95abc458a674e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Thu, 24 May 2018 17:59:05 +0200 Subject: [PATCH 09/17] Simplified #row-edit-control block --- src/Datagrid.blocks.latte | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Datagrid.blocks.latte b/src/Datagrid.blocks.latte index 105e1bf..22c9399 100644 --- a/src/Datagrid.blocks.latte +++ b/src/Datagrid.blocks.latte @@ -93,9 +93,7 @@ {define row-edit-control} {input $column->name} - {if $form[edit][$column->name]->hasErrors()} -

{$error}

- {/if} +

{$error}

{/define} {define row} From 7a78cd2464c4b6fc8c7cc1942c93e468ab8ca79b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Thu, 24 May 2018 18:03:03 +0200 Subject: [PATCH 10/17] Added Column attributes --- src/Column.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Column.php b/src/Column.php index 845f2b6..096b39a 100644 --- a/src/Column.php +++ b/src/Column.php @@ -28,6 +28,9 @@ class Column /** @var Datagrid */ protected $grid; + /** @var array */ + protected $attributes = []; + public function __construct($name, $label, Datagrid $grid) { @@ -37,6 +40,39 @@ public function __construct($name, $label, Datagrid $grid) } + /** + * @param string $name + * @param mixed $value + * @return self + */ + public function setAttribute($name, $value = true) + { + $this->attributes[$name] = $value; + return $this; + } + + + /** + * @param string $name + * @return bool + */ + public function hasAttribute($name) + { + return isset($this->attributes[$name]); + } + + + /** + * @param string $name + * @param mixed $default + * @return self + */ + public function getAttribute($name, $default = null) + { + return $this->attributes[$name] ?? $default; + } + + public function enableSort($default = NULL) { $this->sort = TRUE; From 990ad5f3fd5e867709b4566baba8c1b7adcfe2dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Thu, 24 May 2018 18:06:52 +0200 Subject: [PATCH 11/17] Column titles rendered via #row-head-cell block --- src/Datagrid.blocks.latte | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Datagrid.blocks.latte b/src/Datagrid.blocks.latte index 22c9399..ab2b4ca 100644 --- a/src/Datagrid.blocks.latte +++ b/src/Datagrid.blocks.latte @@ -20,6 +20,10 @@ {/if} {/define} +{define row-head-cell} + {$column->label} +{/define} + {define row-head-columns} {ifset $form[actions]} @@ -28,7 +32,7 @@ {foreach $columns as $column} {if $column->canSort()} - {$column->label} + {include #row-head-cell column => $column} {if $column->isAsc()} {elseif $column->isDesc()} @@ -37,7 +41,7 @@ {/if} {else} - {$column->label} + {include #row-head-cell column => $column} {/if} {/foreach} From e38a400c6337db721519ef144dc814084389764b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Fri, 25 May 2018 12:14:26 +0200 Subject: [PATCH 12/17] fix: set validation scope for submit buttons --- src/Datagrid.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index 45b9fcb..a2584be 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -487,10 +487,10 @@ public function createComponentForm() if ($this->filterFormFactory) { $form['filter'] = call_user_func($this->filterFormFactory); if (!isset($form['filter']['filter'])) { - $form['filter']->addSubmit('filter', $this->translate('Filter')); + $form['filter']->addSubmit('filter', $this->translate('Filter'))->setValidationScope($form['filter']->getControls()); } if (!isset($form['filter']['cancel'])) { - $form['filter']->addSubmit('cancel', $this->translate('Cancel')); + $form['filter']->addSubmit('cancel', $this->translate('Cancel'))->setValidationScope(false); } $this->prepareFilterDefaults($form['filter']); @@ -503,12 +503,15 @@ public function createComponentForm() $data = $this->editRowKey !== null && empty($_POST) ? $this->getData($this->editRowKey) : null; $form['edit'] = call_user_func($this->editFormFactory, $data); - if (!isset($form['edit']['save'])) - $form['edit']->addSubmit('save', 'Save'); - if (!isset($form['edit']['cancel'])) - $form['edit']->addSubmit('cancel', 'Cancel'); - if (!isset($form['edit'][$this->rowPrimaryKey])) + if (!isset($form['edit']['save'])) { + $form['edit']->addSubmit('save', 'Save')->setValidationScope($form['edit']->getControls()); + } + if (!isset($form['edit']['cancel'])) { + $form['edit']->addSubmit('cancel', 'Cancel')->setValidationScope(false); + } + if (!isset($form['edit'][$this->rowPrimaryKey])) { $form['edit']->addHidden($this->rowPrimaryKey); + } $form['edit'][$this->rowPrimaryKey] ->setDefaultValue($this->editRowKey) @@ -521,7 +524,7 @@ public function createComponentForm() $form['actions']->addSelect('action', 'Action', $actions) ->setPrompt('- select action -'); $form['actions']->addCheckboxList('items', '', []); - $form['actions']->addSubmit('process', 'Do'); + $form['actions']->addSubmit('process', 'Do')->setValidationScope(false); } if ($this->translator) { From b03889a7069e85d618906c6faa040c4ca24a3aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Fri, 25 May 2018 12:39:34 +0200 Subject: [PATCH 13/17] fix: removed double translation of filter submit buttons --- src/Datagrid.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index a2584be..0c8f071 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -487,10 +487,10 @@ public function createComponentForm() if ($this->filterFormFactory) { $form['filter'] = call_user_func($this->filterFormFactory); if (!isset($form['filter']['filter'])) { - $form['filter']->addSubmit('filter', $this->translate('Filter'))->setValidationScope($form['filter']->getControls()); + $form['filter']->addSubmit('filter', 'Filter')->setValidationScope($form['filter']->getControls()); } if (!isset($form['filter']['cancel'])) { - $form['filter']->addSubmit('cancel', $this->translate('Cancel'))->setValidationScope(false); + $form['filter']->addSubmit('cancel', 'Cancel')->setValidationScope(false); } $this->prepareFilterDefaults($form['filter']); From 428f0b85c1d5ae944194d170e14de8bb1e9263b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Fri, 25 May 2018 13:44:37 +0200 Subject: [PATCH 14/17] Added insert cabability --- src/Datagrid.blocks.latte | 25 ++++++++++++++++++++++ src/Datagrid.latte | 6 ++++++ src/Datagrid.php | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/src/Datagrid.blocks.latte b/src/Datagrid.blocks.latte index ab2b4ca..5dd53e4 100644 --- a/src/Datagrid.blocks.latte +++ b/src/Datagrid.blocks.latte @@ -100,6 +100,31 @@

{$error}

{/define} +{define row-insert-control} + {input $formContainer[$column->name]} +{/define} + +{define row-insert-empty} +{/define} + +{define row-insert-button} + {input button} +{/define} + +{define row-insert} + {formContainer insert-data} + {foreach $columns as $column} + {ifset $formContainer[$column->name]} + {include #row-insert-control form => $form, formContainer => $formContainer, column => $column} + {else} + {include #row-insert-empty form => $form, formContainer => $formContainer, column => $column} + {/} + {/foreach} + {/formContainer} + {include #row-insert-button form => $form} +{/define} + + {define row} {include #row-inner row => $row} diff --git a/src/Datagrid.latte b/src/Datagrid.latte index c578e13..ed30e8e 100644 --- a/src/Datagrid.latte +++ b/src/Datagrid.latte @@ -22,6 +22,7 @@ $hasActionsColumn = (bool) $control->getEditFormFactory() /* we may render only one row so the form[filter] may not be created */ || isset($this->blockQueue["row-actions"]) + || isset($form["insert"]) || isset($form["filter"]); $hasGlobalActionsColumn = isset($form['actions']); @@ -51,6 +52,11 @@ {ifset #empty-result}{include #empty-result}{/ifset} {/if} + + + {include #row-insert} + + diff --git a/src/Datagrid.php b/src/Datagrid.php index 0c8f071..3c4e082 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -59,6 +59,12 @@ class Datagrid extends UI\Control /** @var callable|null */ protected $formFactory; + /** @var callable|null */ + protected $insertFormFactory; + + /** @var callable|null */ + protected $insertFormCallback; + /** @var callable|null */ protected $editFormFactory; @@ -215,6 +221,30 @@ public function getEditFormCallback() } + public function setInsertFormFactory(callable $insertFormFactory = null) + { + $this->insertFormFactory = $insertFormFactory; + } + + + public function getInsertFormFactory() + { + return $this->insertFormFactory; + } + + + public function setInsertFormCallback(callable $insertFormCallback = null) + { + $this->insertFormCallback = $insertFormCallback; + } + + + public function getInsertFormCallback() + { + return $this->insertFormCallback; + } + + public function setFilterFormFactory(callable $filterFormFactory = null) { $this->filterFormFactory = $filterFormFactory; @@ -499,6 +529,12 @@ public function createComponentForm() } } + if ($this->insertFormFactory) { + $form['insert'] = new Container; + $form['insert']['data'] = call_user_func($this->insertFormFactory); + $form['insert']->addSubmit('button', 'Insert')->setValidationScope($form['insert']['data']->getControls()); + } + if ($this->editFormFactory && ($this->editRowKey !== null || !empty($_POST['edit']))) { $data = $this->editRowKey !== null && empty($_POST) ? $this->getData($this->editRowKey) : null; $form['edit'] = call_user_func($this->editFormFactory, $data); @@ -540,6 +576,15 @@ public function createComponentForm() public function processForm(UI\Form $form) { $allowRedirect = true; + if (isset($form['insert']) && $form['insert']['button']->isSubmittedBy()) { + if ($form['insert']['data']->isValid()) { + call_user_func($this->insertFormCallback, $form['insert']['data']); + $this->redrawControl('rows'); + } else { + $allowRedirect = false; + } + } + if (isset($form['edit'])) { if ($form['edit']['save']->isSubmittedBy()) { if ($form['edit']->isValid()) { From 7844c57b22113123f164735bd9c3149e45de7f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Fri, 25 May 2018 13:55:28 +0200 Subject: [PATCH 15/17] Added delete capability --- src/Datagrid.blocks.latte | 13 +++++++++++-- src/Datagrid.latte | 3 ++- src/Datagrid.php | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Datagrid.blocks.latte b/src/Datagrid.blocks.latte index 5dd53e4..6081467 100644 --- a/src/Datagrid.blocks.latte +++ b/src/Datagrid.blocks.latte @@ -111,6 +111,10 @@ {input button} {/define} +{define row-actions-delete-link} + {$control->translate(Delete)} +{/define} + {define row-insert} {formContainer insert-data} {foreach $columns as $column} @@ -179,8 +183,13 @@ {else} {ifset #row-actions} {include #row-actions row => $row, primary => $primary} - {elseif $control->getEditFormFactory()} - {include #row-actions-edit-link row => $row, primary => $primary} + {else} + {if $control->getEditFormFactory()} + {include #row-actions-edit-link row => $row, primary => $primary} + {/if} + {if $control->getDeleteCallback()} + {include #row-actions-delete-link row => $row, primary => $primary} + {/if} {/ifset} {/if} diff --git a/src/Datagrid.latte b/src/Datagrid.latte index ed30e8e..04dd2de 100644 --- a/src/Datagrid.latte +++ b/src/Datagrid.latte @@ -23,7 +23,8 @@ (bool) $control->getEditFormFactory() /* we may render only one row so the form[filter] may not be created */ || isset($this->blockQueue["row-actions"]) || isset($form["insert"]) - || isset($form["filter"]); + || isset($form["filter"]) + || $control->getDeleteCallback(); $hasGlobalActionsColumn = isset($form['actions']); foreach ($_templates as $_template): diff --git a/src/Datagrid.php b/src/Datagrid.php index 3c4e082..a3b62d7 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -71,6 +71,9 @@ class Datagrid extends UI\Control /** @var callable|null */ protected $editFormCallback; + /** @var callable|null */ + protected $deleteCallback; + /** @var callable|null */ protected $filterFormFactory; @@ -257,6 +260,18 @@ public function getFilterFormFactory() } + public function setDeleteCallback(callable $callback = null) + { + $this->deleteCallback = $callback; + } + + + public function getDeleteCallback() + { + return $this->deleteCallback; + } + + public function addGlobalAction($name, $label, callable $action) { $this->globalActions[$name] = [$label, $action]; @@ -489,6 +504,15 @@ public function handleEdit($primaryValue, $cancelEditPrimaryValue = null) } + public function handleDelete($primaryValue) + { + call_user_func($this->deleteCallback, $primaryValue); + if ($this->presenter->isAjax()) { + $this->redrawControl('rows'); + } + } + + public function handleSort() { if ($this->presenter->isAjax()) { From f806dd8ffeeb30719df27c6535f9872c6494372d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Fri, 25 May 2018 14:00:10 +0200 Subject: [PATCH 16/17] Single global action render as hidden field --- src/Datagrid.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index a3b62d7..7deaa83 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -580,11 +580,18 @@ public function createComponentForm() if ($this->globalActions) { $actions = array_map(function($row) { return $row[0]; }, $this->globalActions); - $form['actions'] = new Container(); - $form['actions']->addSelect('action', 'Action', $actions) - ->setPrompt('- select action -'); + $form['actions'] = new Container; $form['actions']->addCheckboxList('items', '', []); - $form['actions']->addSubmit('process', 'Do')->setValidationScope(false); + + if (count($actions) === 1) { + $form['actions']->addHidden('action', key($actions)); + $form['actions']->addSubmit('process', current($actions))->setValidationScope(false); + + } else { + $form['actions']->addSelect('action', 'Action', $actions) + ->setPrompt('- select action -'); + $form['actions']->addSubmit('process', 'Do')->setValidationScope(false); + } } if ($this->translator) { From d4ea1f4d0ba327d416409fa080cd762b24ad6f77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Fri, 25 May 2018 14:07:51 +0200 Subject: [PATCH 17/17] fix: pass only valid filter inputs to data source callback [Closes #73] --- src/Datagrid.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index 7deaa83..a45dde5 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -429,10 +429,21 @@ protected function getData($key = null) $this->orderColumn = NULL; } + $validFilterData = []; + if ($this->filterFormFactory) { + $this['form']->isValid(); // triggers validation + foreach ($this['form']['filters']->getControls() as $name => $control) { + if ($control->getErrors() === []) { + $validFilterData[$name] = $control->getValue(); + } + } + $validFilterData = $this->filterFormFilter($validFilterData); + } + if (!$onlyRow && $this->paginator) { $itemsCount = call_user_func( $this->paginatorItemsCountCallback, - $this->filterDataSource, + $validFilterData, $this->orderColumn ? [$this->orderColumn, strtoupper($this->orderType)] : null ); @@ -444,7 +455,7 @@ protected function getData($key = null) $this->data = call_user_func( $this->dataSourceCallback, - $this->filterDataSource, + $validFilterData, $this->orderColumn ? [$this->orderColumn, strtoupper($this->orderType)] : null, $onlyRow ? null : $this->paginator );