From 23cff7b9e9a947d930dc2971ce606a3b9d91677d Mon Sep 17 00:00:00 2001 From: Anthony Rappa Date: Thu, 28 Apr 2022 14:20:12 -0400 Subject: [PATCH 1/5] Ability to add additional selects to query that are not a column --- CHANGELOG.md | 4 ++++ src/Traits/ComponentUtilities.php | 1 + .../Configuration/ComponentConfiguration.php | 16 ++++++++++++++++ src/Traits/Helpers/ComponentHelpers.php | 8 ++++++++ src/Traits/WithData.php | 5 +++++ tests/Traits/Helpers/ComponentHelpersTest.php | 10 ++++++++++ 6 files changed, 44 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cff4e31d3..d112b2f4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to `laravel-livewire-tables` will be documented in this file ## [Unreleased] +### Added + +- Added ability to define additional select statements outside the scope of a column using the `setAdditionalSelects(array $selects)` configuration method. + ## [2.2.1] - 2022-04-27 ### Changed diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 5411e2db3..ec0b4e15e 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -31,6 +31,7 @@ trait ComponentUtilities protected $tdAttributesCallback; protected $collapsingColumnsStatus = true; protected string $emptyMessage = 'No items found. Try to broaden your search.'; + protected array $additionalSelects = []; /** * Set the custom query string array for this specific table diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index 02bfae066..e72e8624d 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -281,4 +281,20 @@ public function setTableRowUrlTarget(callable $callback): self return $this; } + + /** + * @param $selects + * + * @return $this + */ + public function setAdditionalSelects($selects): self + { + if (! is_array($selects)) { + $selects = [$selects]; + } + + $this->additionalSelects = $selects; + + return $this; + } } diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index fbc7ea217..79ddc1ad3 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -303,4 +303,12 @@ public function getTableRowUrlTarget($row): ?string { return $this->trUrlTargetCallback ? call_user_func($this->trUrlTargetCallback, $row) : null; } + + /** + * @return array + */ + public function getAdditionalSelects(): array + { + return $this->additionalSelects; + } } diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index 188f4b447..818088b3c 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -107,6 +107,11 @@ protected function performJoin(Builder $builder, $table, $foreign, $other, $type protected function selectFields(Builder $builder): Builder { + // Load any additional selects that were not already columns + foreach ($this->getAdditionalSelects() as $select) { + $builder->addSelect($select); + } + foreach ($this->getSelectableColumns() as $column) { $builder->addSelect($column->getColumn() . ' as ' .$column->getColumnSelectName()); } diff --git a/tests/Traits/Helpers/ComponentHelpersTest.php b/tests/Traits/Helpers/ComponentHelpersTest.php index f3edb5ab9..ab71fc3d5 100644 --- a/tests/Traits/Helpers/ComponentHelpersTest.php +++ b/tests/Traits/Helpers/ComponentHelpersTest.php @@ -152,4 +152,14 @@ public function can_check_for_tr_url(): void $this->assertTrue($this->basicTable->hasTableRowUrl()); } + + /** @test */ + public function can_get_additional_selects(): void + { + $this->assertEquals([], $this->basicTable->getAdditionalSelects()); + + $this->basicTable->setAdditionalSelects(['id', 'name']); + + $this->assertEquals(['id', 'name'], $this->basicTable->getAdditionalSelects()); + } } From c6692d434271b5f27f89b3505d2dfa0c7e05041e Mon Sep 17 00:00:00 2001 From: rappasoft Date: Thu, 28 Apr 2022 18:20:47 +0000 Subject: [PATCH 2/5] Fix styling --- src/Traits/Configuration/ComponentConfiguration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index e72e8624d..96d4860f8 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -284,7 +284,7 @@ public function setTableRowUrlTarget(callable $callback): self /** * @param $selects - * + * * @return $this */ public function setAdditionalSelects($selects): self From 2a832821310d11ad63c4aab39bb9dc9c8965de6a Mon Sep 17 00:00:00 2001 From: Anthony Rappa Date: Thu, 28 Apr 2022 15:50:04 -0400 Subject: [PATCH 3/5] Added configurable areas --- CHANGELOG.md | 1 + docs/datatable/available-methods.md | 15 ++ docs/datatable/configurable-areas.md | 131 ++++++++++++++++++ .../views/components/pagination.blade.php | 8 ++ .../views/components/tools/toolbar.blade.php | 56 ++++++++ src/Traits/ComponentUtilities.php | 10 ++ .../Configuration/ComponentConfiguration.php | 12 ++ src/Traits/Helpers/ComponentHelpers.php | 28 ++++ tests/Traits/Helpers/ComponentHelpersTest.php | 21 +++ 9 files changed, 282 insertions(+) create mode 100644 docs/datatable/configurable-areas.md diff --git a/CHANGELOG.md b/CHANGELOG.md index d112b2f4b..cc5e71c43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file ### Added - Added ability to define additional select statements outside the scope of a column using the `setAdditionalSelects(array $selects)` configuration method. +- Added 8 configurable areas, see `Configurable Areas` of the `Datatable` section of the documentation. ## [2.2.1] - 2022-04-27 diff --git a/docs/datatable/available-methods.md b/docs/datatable/available-methods.md index ea173cbe7..d14a4f91a 100644 --- a/docs/datatable/available-methods.md +++ b/docs/datatable/available-methods.md @@ -373,6 +373,21 @@ public function configure(): void } ``` +## Builder + +### setAdditionalSelects + +By default the only columns defined in the select statement are the ones defined via columns. If you need to define additional selects that you don't have a column for you may: + +```php +public function configure(): void +{ + $this->setAdditionalSelects(['users.id as id']); +} +``` + +Since you probably won't have an `ID` column defined, the ID will not be available on the model to use. In the case of an actions column where you have buttons specific to the row, you probably need that, so you can add the select statement to make it available on the model. + ## Misc. ### setEmptyMessage diff --git a/docs/datatable/configurable-areas.md b/docs/datatable/configurable-areas.md new file mode 100644 index 000000000..9b9ec1fbe --- /dev/null +++ b/docs/datatable/configurable-areas.md @@ -0,0 +1,131 @@ +--- +title: Configurable Areas +weight: 3 +--- + +There are certain areas of the datatable as of `v2.2` where you can include your own view files. This is good for adding additional functionality to the toolbar, for example. + +## Available Methods + +### setConfigurableAreas + +You can use the `setConfigurableAreas` method to set the areas that you want to be configurable. + +```php +public function configure(): void +{ + $this->setConfigurableAreas([ + 'toolbar-left-start' => 'path.to.my.view', + 'toolbar-left-end' => 'path.to.my.view', + 'toolbar-right-start' => 'path.to.my.view', + 'toolbar-right-end' => 'path.to.my.view', + 'before-toolbar' => 'path.to.my.view', + 'after-toolbar' => 'path.to.my.view', + 'before-pagination' => 'path.to.my.view', + 'after-pagination' => 'path.to.my.view', + ]); +} +``` + +**Note:** Only specify the keys you are actually implementing, otherwise you may get uneven spacing. + +## Example View + +Example dropdown for the toolbar: + +```php +@aware(['component']) + +@php + $theme = $component->getTheme(); +@endphp + +@if ($theme === 'tailwind') +
+
+
+ + + +
+ +
+
+ +
+
+
+
+@elseif ($theme === 'bootstrap-4' || $theme === 'bootstrap-5') +
+@endif +``` + +**Note:** If you don't specify the theme conditional, it will show up on every + +## Areas + +Here are the placements for the available areas: + +Toolbar Left Start +![Toolbar Left Start](https://imgur.com/eDQx67u.png) + +Toolbar Left End +![Toolbar Left End](https://imgur.com/hmkfoyH.png) + +Toolbar Right Start +![Toolbar Right Start](https://imgur.com/V99PQUv.png) + +Toolbar Right End +![Toolbar Right End](https://imgur.com/rZgbeYO.png) + +Before Toolbar +![Before Toolbar](https://imgur.com/KK9EiSM.png) + +After Toolbar +![After Toolbar](https://imgur.com/VL0OGia.png) + +Before Pagination +![Before Pagination](https://imgur.com/lVIGpDW.png) + +After Pagination +![After Pagination](https://imgur.com/wJR2LEJ.png) \ No newline at end of file diff --git a/resources/views/components/pagination.blade.php b/resources/views/components/pagination.blade.php index 20daddfa8..82454e485 100644 --- a/resources/views/components/pagination.blade.php +++ b/resources/views/components/pagination.blade.php @@ -5,6 +5,10 @@ $theme = $component->getTheme(); @endphp +@if ($component->hasConfigurableAreaFor('before-pagination')) + @include($component->getConfigurableAreaFor('before-pagination')) +@endif + @if ($theme === 'tailwind')
@if ($component->paginationVisibilityIsEnabled()) @@ -96,3 +100,7 @@ @endif
@endif + +@if ($component->hasConfigurableAreaFor('after-pagination')) + @include($component->getConfigurableAreaFor('after-pagination')) +@endif \ No newline at end of file diff --git a/resources/views/components/tools/toolbar.blade.php b/resources/views/components/tools/toolbar.blade.php index 957fad3e0..589284d57 100644 --- a/resources/views/components/tools/toolbar.blade.php +++ b/resources/views/components/tools/toolbar.blade.php @@ -4,9 +4,17 @@ $theme = $component->getTheme(); @endphp +@if ($component->hasConfigurableAreaFor('before-toolbar')) + @include($component->getConfigurableAreaFor('before-toolbar')) +@endif + @if ($theme === 'tailwind')
+ @if ($component->hasConfigurableAreaFor('toolbar-left-start')) + @include($component->getConfigurableAreaFor('toolbar-left-start')) + @endif + @if ($component->reorderIsEnabled())
@endif + + @if ($component->hasConfigurableAreaFor('toolbar-left-end')) + @include($component->getConfigurableAreaFor('toolbar-left-end')) + @endif
+ @if ($component->hasConfigurableAreaFor('toolbar-right-start')) + @include($component->getConfigurableAreaFor('toolbar-right-start')) + @endif + @if ($component->showBulkActionsDropdown())
@endif + + @if ($component->hasConfigurableAreaFor('toolbar-right-end')) + @include($component->getConfigurableAreaFor('toolbar-right-end')) + @endif
@@ -526,6 +562,10 @@ class="d-block"> @elseif ($theme === 'bootstrap-5')
+ @if ($component->hasConfigurableAreaFor('toolbar-left-start')) + @include($component->getConfigurableAreaFor('toolbar-left-start')) + @endif + @if ($component->reorderIsEnabled())
@endif + + @if ($component->hasConfigurableAreaFor('toolbar-left-end')) + @include($component->getConfigurableAreaFor('toolbar-left-end')) + @endif
+ @if ($component->hasConfigurableAreaFor('toolbar-right-start')) + @include($component->getConfigurableAreaFor('toolbar-right-start')) + @endif + @if ($component->showBulkActionsDropdown())
@endif + + @if ($component->hasConfigurableAreaFor('toolbar-right-end')) + @include($component->getConfigurableAreaFor('toolbar-right-end')) + @endif
@@ -747,3 +799,7 @@ class="d-block">
@endif @endif + +@if ($component->hasConfigurableAreaFor('after-toolbar')) + @include($component->getConfigurableAreaFor('after-toolbar')) +@endif \ No newline at end of file diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index ec0b4e15e..07c678340 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -32,6 +32,16 @@ trait ComponentUtilities protected $collapsingColumnsStatus = true; protected string $emptyMessage = 'No items found. Try to broaden your search.'; protected array $additionalSelects = []; + protected array $configurableAreas = [ + 'toolbar-left-start' => null, + 'toolbar-left-end' => null, + 'toolbar-right-start' => null, + 'toolbar-right-end' => null, + 'before-toolbar' => null, + 'after-toolbar' => null, + 'before-pagination' => null, + 'after-pagination' => null, + ]; /** * Set the custom query string array for this specific table diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index e72e8624d..838a8fd58 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -297,4 +297,16 @@ public function setAdditionalSelects($selects): self return $this; } + + /** + * @param $areas + * + * @return $this + */ + public function setConfigurableAreas(array $areas): self + { + $this->configurableAreas = $areas; + + return $this; + } } diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index 79ddc1ad3..873316f60 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -311,4 +311,32 @@ public function getAdditionalSelects(): array { return $this->additionalSelects; } + + /** + * @return array + */ + public function getConfigurableAreas(): array + { + return $this->configurableAreas; + } + + /** + * @param string $area + * + * @return bool + */ + public function hasConfigurableAreaFor(string $area): bool + { + return isset($this->configurableAreas[$area]) && $this->getConfigurableAreaFor($area) !== null; + } + + /** + * @param string $area + * + * @return string|null + */ + public function getConfigurableAreaFor(string $area): ?string + { + return $this->configurableAreas[$area] ?? null; + } } diff --git a/tests/Traits/Helpers/ComponentHelpersTest.php b/tests/Traits/Helpers/ComponentHelpersTest.php index ab71fc3d5..876192bc3 100644 --- a/tests/Traits/Helpers/ComponentHelpersTest.php +++ b/tests/Traits/Helpers/ComponentHelpersTest.php @@ -162,4 +162,25 @@ public function can_get_additional_selects(): void $this->assertEquals(['id', 'name'], $this->basicTable->getAdditionalSelects()); } + + /** @test */ + public function can_get_configurable_areas(): void + { + $this->assertEquals([ + 'toolbar-left-start' => null, + 'toolbar-left-end' => null, + 'toolbar-right-start' => null, + 'toolbar-right-end' => null, + 'before-toolbar' => null, + 'after-toolbar' => null, + 'before-pagination' => null, + 'after-pagination' => null, + ], $this->basicTable->getConfigurableAreas()); + + $this->basicTable->setConfigurableAreas([ + 'toolbar-left-start' => 'includes.areas.toolbar-left-start' + ]); + + $this->assertEquals('includes.areas.toolbar-left-start', $this->basicTable->getConfigurableAreaFor('toolbar-left-start')); + } } From d7e71878322194d08b05e01b1b7c7c11693791f4 Mon Sep 17 00:00:00 2001 From: rappasoft Date: Thu, 28 Apr 2022 19:50:32 +0000 Subject: [PATCH 4/5] Fix styling --- src/Traits/Configuration/ComponentConfiguration.php | 2 +- src/Traits/Helpers/ComponentHelpers.php | 4 ++-- tests/Traits/Helpers/ComponentHelpersTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index b34432368..40d723933 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -300,7 +300,7 @@ public function setAdditionalSelects($selects): self /** * @param $areas - * + * * @return $this */ public function setConfigurableAreas(array $areas): self diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index 873316f60..e30e209a7 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -322,7 +322,7 @@ public function getConfigurableAreas(): array /** * @param string $area - * + * * @return bool */ public function hasConfigurableAreaFor(string $area): bool @@ -332,7 +332,7 @@ public function hasConfigurableAreaFor(string $area): bool /** * @param string $area - * + * * @return string|null */ public function getConfigurableAreaFor(string $area): ?string diff --git a/tests/Traits/Helpers/ComponentHelpersTest.php b/tests/Traits/Helpers/ComponentHelpersTest.php index 876192bc3..454ec61ed 100644 --- a/tests/Traits/Helpers/ComponentHelpersTest.php +++ b/tests/Traits/Helpers/ComponentHelpersTest.php @@ -178,7 +178,7 @@ public function can_get_configurable_areas(): void ], $this->basicTable->getConfigurableAreas()); $this->basicTable->setConfigurableAreas([ - 'toolbar-left-start' => 'includes.areas.toolbar-left-start' + 'toolbar-left-start' => 'includes.areas.toolbar-left-start', ]); $this->assertEquals('includes.areas.toolbar-left-start', $this->basicTable->getConfigurableAreaFor('toolbar-left-start')); From 797463b1c5670d174892660f4d932363c0ec2ce8 Mon Sep 17 00:00:00 2001 From: Anthony Rappa Date: Thu, 28 Apr 2022 15:52:40 -0400 Subject: [PATCH 5/5] Changelog --- CHANGELOG.md | 5 ++++- docs/datatable/configurable-areas.md | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc5e71c43..964aeb5ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to `laravel-livewire-tables` will be documented in this file ## [Unreleased] +## [2.3.0] - 2022-04-28 + ### Added - Added ability to define additional select statements outside the scope of a column using the `setAdditionalSelects(array $selects)` configuration method. @@ -616,7 +618,8 @@ Ground Up Rebuild - Initial release -[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.2.1...development +[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.3.0...development +[2.3.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.2.1...v2.3.0 [2.2.1]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.2.0...v2.2.1 [2.2.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.1.0...v2.2.0 [2.1.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.0.0...v2.1.0 diff --git a/docs/datatable/configurable-areas.md b/docs/datatable/configurable-areas.md index 9b9ec1fbe..27d650b1e 100644 --- a/docs/datatable/configurable-areas.md +++ b/docs/datatable/configurable-areas.md @@ -3,7 +3,7 @@ title: Configurable Areas weight: 3 --- -There are certain areas of the datatable as of `v2.2` where you can include your own view files. This is good for adding additional functionality to the toolbar, for example. +There are certain areas of the datatable as of `v2.3` where you can include your own view files. This is good for adding additional functionality to the toolbar, for example. ## Available Methods