+
@if($this->hasActions && !$this->showActionsInToolbar)
diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php
index d065b25ed..fc1b20153 100644
--- a/src/Traits/ComponentUtilities.php
+++ b/src/Traits/ComponentUtilities.php
@@ -4,6 +4,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
+use Livewire\Attributes\Locked;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Traits\Configuration\ComponentConfiguration;
use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers;
@@ -27,7 +28,8 @@ trait ComponentUtilities
protected string $tableName = 'table';
- protected ?string $dataTableFingerprint;
+ #[Locked]
+ public ?string $dataTableFingerprint;
protected bool $offlineIndicatorStatus = true;
@@ -61,6 +63,8 @@ public function mountComponentUtilities(): void
if (is_null($this->theme)) {
$this->setTheme();
}
+ $this->generateDataTableFingerprint();
+
}
/**
@@ -81,7 +85,7 @@ public function bootedComponentUtilities(): void
// Make sure a primary key is set
if (! $this->hasPrimaryKey()) {
- throw new DataTableConfigurationException('You must set a primary key using setPrimaryKey in the configure method.');
+ throw new DataTableConfigurationException('You must set a primary key using setPrimaryKey in the configure method, or configuring/configured lifecycle hooks');
}
}
diff --git a/src/Traits/Configuration/TableAttributeConfiguration.php b/src/Traits/Configuration/TableAttributeConfiguration.php
index 01cf7376d..c1bb681e9 100644
--- a/src/Traits/Configuration/TableAttributeConfiguration.php
+++ b/src/Traits/Configuration/TableAttributeConfiguration.php
@@ -118,4 +118,19 @@ public function setTableRowUrlTarget(\Closure $callback): self
return $this;
}
+
+ public function setShouldBeDisplayedStatus(bool $status): void
+ {
+ $this->shouldBeDisplayed = $status;
+ }
+
+ public function setShouldBeDisplayed(): void
+ {
+ $this->setShouldBeDisplayedStatus(true);
+ }
+
+ public function setShouldBeHidden(): void
+ {
+ $this->setShouldBeDisplayedStatus(false);
+ }
}
diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php
index 828df37a5..0480f39e4 100644
--- a/src/Traits/Helpers/ComponentHelpers.php
+++ b/src/Traits/Helpers/ComponentHelpers.php
@@ -9,7 +9,7 @@ trait ComponentHelpers
{
public function getDataTableFingerprint(): string
{
- return $this->dataTableFingerprint ?? $this->generateDataTableFingerprint();
+ return $this->dataTableFingerprint ?? ($this->dataTableFingerprint = $this->generateDataTableFingerprint());
}
public function setBuilder(Builder $builder): void
diff --git a/src/Traits/Helpers/TableAttributeHelpers.php b/src/Traits/Helpers/TableAttributeHelpers.php
index 842c20886..d5f57f03e 100644
--- a/src/Traits/Helpers/TableAttributeHelpers.php
+++ b/src/Traits/Helpers/TableAttributeHelpers.php
@@ -76,4 +76,10 @@ public function getTableRowUrlTarget(int|Model $row): ?string
{
return isset($this->trUrlTargetCallback) ? call_user_func($this->trUrlTargetCallback, $row) : null;
}
+
+ #[Computed]
+ public function getShouldBeDisplayed(): bool
+ {
+ return $this->shouldBeDisplayed;
+ }
}
diff --git a/src/Traits/WithTableAttributes.php b/src/Traits/WithTableAttributes.php
index 0ec1884c1..f87edea61 100644
--- a/src/Traits/WithTableAttributes.php
+++ b/src/Traits/WithTableAttributes.php
@@ -3,6 +3,7 @@
namespace Rappasoft\LaravelLivewireTables\Traits;
use Closure;
+use Livewire\Attributes\On;
use Rappasoft\LaravelLivewireTables\Traits\Configuration\TableAttributeConfiguration;
use Rappasoft\LaravelLivewireTables\Traits\Helpers\TableAttributeHelpers;
@@ -32,4 +33,6 @@ trait WithTableAttributes
protected ?\Closure $trUrlCallback;
protected ?\Closure $trUrlTargetCallback;
+
+ public bool $shouldBeDisplayed = true;
}
diff --git a/tests/Http/Livewire/PetsTable.php b/tests/Http/Livewire/PetsTable.php
index 28cc1782c..59b52049d 100644
--- a/tests/Http/Livewire/PetsTable.php
+++ b/tests/Http/Livewire/PetsTable.php
@@ -3,6 +3,7 @@
namespace Rappasoft\LaravelLivewireTables\Tests\Http\Livewire;
use Illuminate\Database\Eloquent\Builder;
+use Livewire\Attributes\On;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
diff --git a/tests/Http/Livewire/PetsTableAttributes.php b/tests/Http/Livewire/PetsTableAttributes.php
index 53f79646f..690faf813 100644
--- a/tests/Http/Livewire/PetsTableAttributes.php
+++ b/tests/Http/Livewire/PetsTableAttributes.php
@@ -2,26 +2,8 @@
namespace Rappasoft\LaravelLivewireTables\Tests\Http\Livewire;
-use Illuminate\Database\Eloquent\Builder;
-use Rappasoft\LaravelLivewireTables\DataTableComponent;
-use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
-use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
-use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
-use Rappasoft\LaravelLivewireTables\Views\Column;
-use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;
-use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
-use Rappasoft\LaravelLivewireTables\Views\Filters\DateFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\DateTimeFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectDropdownFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\NumberFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\TextFilter;
-
-class PetsTableAttributes extends DataTableComponent
+class PetsTableAttributes extends PetsTable
{
- public $model = Pet::class;
-
public function configure(): void
{
$this->setPrimaryKey('id')
@@ -49,123 +31,4 @@ public function configure(): void
});
}
-
- public function columns(): array
- {
- return [
- Column::make('ID', 'id')
- ->sortable()
- ->setSortingPillTitle('Key')
- ->setSortingPillDirections('0-9', '9-0'),
- Column::make('Sort')
- ->sortable()
- ->excludeFromColumnSelect(),
- Column::make('Name')
- ->sortable()
- ->secondaryHeader($this->getFilterByKey('pet_name_filter'))
- ->footerFilter('pet_name_filter')
- ->searchable(),
-
- Column::make('Age'),
-
- Column::make('Breed', 'breed.name')
- ->secondaryHeaderFilter('breed')
- ->footer($this->getFilterByKey('breed'))
- ->sortable(
- fn (Builder $query, string $direction) => $query->orderBy('pets.id', $direction)
- )
- ->searchable(
- fn (Builder $query, $searchTerm) => $query->orWhere('breed.name', $searchTerm)
- ),
-
- Column::make('Other')
- ->label(function ($row, Column $column) {
- return 'Other';
- })
- ->footer(function ($rows) {
- return 'Count: '.$rows->count();
- }),
-
- LinkColumn::make('Link')
- ->title(fn ($row) => 'Edit')
- ->location(fn ($row) => 'http://www.google.com')
- ->attributes(fn ($row) => [
- 'class' => 'rounded-full',
- 'alt' => $row->name.' Avatar',
- ]),
- ImageColumn::make('RowImg')
- ->location(fn ($row) => 'test'.$row->id)
- ->attributes(fn ($row) => [
- 'class' => 'rounded-full',
- 'alt' => $row->name.' Avatar',
- ]),
- Column::make('Last Visit', 'last_visit')
- ->sortable()
- ->deselected(),
- ];
- }
-
- public function filters(): array
- {
- return [
- MultiSelectFilter::make('Breed')
- ->options(
- Breed::query()
- ->orderBy('name')
- ->get()
- ->keyBy('id')
- ->map(fn ($breed) => $breed->name)
- ->toArray()
- )
- ->filter(function (Builder $builder, array $values) {
- return $builder->whereIn('breed_id', $values);
- }),
- MultiSelectDropdownFilter::make('Species')
- ->options(
- Species::query()
- ->orderBy('name')
- ->get()
- ->keyBy('id')
- ->map(fn ($species) => $species->name)
- ->toArray()
- )
- ->filter(function (Builder $builder, array $values) {
- return $builder->whereIn('species_id', $values);
- }),
- NumberFilter::make('Breed ID', 'breed_id_filter')
- ->filter(function (Builder $builder, string $value) {
- return $builder->where('breed_id', '=', $value);
- }),
-
- TextFilter::make('Pet Name', 'pet_name_filter')
- ->filter(function (Builder $builder, string $value) {
- return $builder->where('pets.name', '=', $value);
- }),
-
- DateFilter::make('Last Visit After Date', 'last_visit_date_filter')
- ->filter(function (Builder $builder, string $value) {
- return $builder->whereDate('pets.last_visit', '=>', $value);
- }),
-
- DateTimeFilter::make('Last Visit Before DateTime', 'last_visit_datetime_filter')
- ->filter(function (Builder $builder, string $value) {
- return $builder->whereDate('pets.last_visit', '<=', $value);
- }),
-
- SelectFilter::make('Breed SelectFilter', 'breed_select_filter')
- ->options(
- Breed::query()
- ->orderBy('name')
- ->get()
- ->keyBy('id')
- ->map(fn ($breed) => $breed->name)
- ->toArray()
- )
- ->filter(function (Builder $builder, string $value) {
- return $builder->where('breed_id', $value);
- })
- ->setCustomFilterLabel('livewire-tables::tests.testFilterLabel')
- ->setFilterPillBlade('livewire-tables::tests.testFilterPills'),
- ];
- }
}
diff --git a/tests/Http/Livewire/PetsTableEvents.php b/tests/Http/Livewire/PetsTableEvents.php
new file mode 100644
index 000000000..24c7163cf
--- /dev/null
+++ b/tests/Http/Livewire/PetsTableEvents.php
@@ -0,0 +1,25 @@
+setShouldBeHidden();
+ }
+
+ #[On('showTable')]
+ public function showTable(): void
+ {
+ $this->setShouldBeDisplayed();
+ }
+
+ #[On('hideTable')]
+ public function hideTable()
+ {
+ $this->setShouldBeHidden();
+ }
+}
diff --git a/tests/Http/Livewire/PetsTableLoadingPlaceholder.php b/tests/Http/Livewire/PetsTableLoadingPlaceholder.php
index c5096c496..26fd7c367 100644
--- a/tests/Http/Livewire/PetsTableLoadingPlaceholder.php
+++ b/tests/Http/Livewire/PetsTableLoadingPlaceholder.php
@@ -2,149 +2,12 @@
namespace Rappasoft\LaravelLivewireTables\Tests\Http\Livewire;
-use Illuminate\Database\Eloquent\Builder;
-use Rappasoft\LaravelLivewireTables\DataTableComponent;
-use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
-use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
-use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
-use Rappasoft\LaravelLivewireTables\Views\Column;
-use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;
-use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
-use Rappasoft\LaravelLivewireTables\Views\Filters\DateFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\DateTimeFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectDropdownFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\NumberFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
-use Rappasoft\LaravelLivewireTables\Views\Filters\TextFilter;
-
-class PetsTableLoadingPlaceholder extends DataTableComponent
+class PetsTableLoadingPlaceholder extends PetsTable
{
- public $model = Pet::class;
-
public function configure(): void
{
$this->setPrimaryKey('id')
->setLoadingPlaceholderEnabled()
->setLoadingPlaceholderContent('TestLoadingPlaceholderContentTestTest');
}
-
- public function columns(): array
- {
- return [
- Column::make('ID', 'id')
- ->sortable()
- ->setSortingPillTitle('Key')
- ->setSortingPillDirections('0-9', '9-0'),
- Column::make('Sort')
- ->sortable()
- ->excludeFromColumnSelect(),
- Column::make('Name')
- ->sortable()
- ->secondaryHeader($this->getFilterByKey('pet_name_filter'))
- ->footerFilter('pet_name_filter')
- ->searchable(),
-
- Column::make('Age'),
-
- Column::make('Breed', 'breed.name')
- ->secondaryHeaderFilter('breed')
- ->footer($this->getFilterByKey('breed'))
- ->sortable(
- fn (Builder $query, string $direction) => $query->orderBy('pets.id', $direction)
- )
- ->searchable(
- fn (Builder $query, $searchTerm) => $query->orWhere('breed.name', $searchTerm)
- ),
-
- Column::make('Other')
- ->label(function ($row, Column $column) {
- return 'Other';
- })
- ->footer(function ($rows) {
- return 'Count: '.$rows->count();
- }),
-
- LinkColumn::make('Link')
- ->title(fn ($row) => 'Edit')
- ->location(fn ($row) => 'http://www.google.com')
- ->attributes(fn ($row) => [
- 'class' => 'rounded-full',
- 'alt' => $row->name.' Avatar',
- ]),
- ImageColumn::make('RowImg')
- ->location(fn ($row) => 'test'.$row->id)
- ->attributes(fn ($row) => [
- 'class' => 'rounded-full',
- 'alt' => $row->name.' Avatar',
- ]),
- Column::make('Last Visit', 'last_visit')
- ->sortable()
- ->deselected(),
- ];
- }
-
- public function filters(): array
- {
- return [
- MultiSelectFilter::make('Breed')
- ->options(
- Breed::query()
- ->orderBy('name')
- ->get()
- ->keyBy('id')
- ->map(fn ($breed) => $breed->name)
- ->toArray()
- )
- ->filter(function (Builder $builder, array $values) {
- return $builder->whereIn('breed_id', $values);
- }),
- MultiSelectDropdownFilter::make('Species')
- ->options(
- Species::query()
- ->orderBy('name')
- ->get()
- ->keyBy('id')
- ->map(fn ($species) => $species->name)
- ->toArray()
- )
- ->filter(function (Builder $builder, array $values) {
- return $builder->whereIn('species_id', $values);
- }),
- NumberFilter::make('Breed ID', 'breed_id_filter')
- ->filter(function (Builder $builder, string $value) {
- return $builder->where('breed_id', '=', $value);
- }),
-
- TextFilter::make('Pet Name', 'pet_name_filter')
- ->filter(function (Builder $builder, string $value) {
- return $builder->where('pets.name', '=', $value);
- }),
-
- DateFilter::make('Last Visit After Date', 'last_visit_date_filter')
- ->filter(function (Builder $builder, string $value) {
- return $builder->whereDate('pets.last_visit', '=>', $value);
- }),
-
- DateTimeFilter::make('Last Visit Before DateTime', 'last_visit_datetime_filter')
- ->filter(function (Builder $builder, string $value) {
- return $builder->whereDate('pets.last_visit', '<=', $value);
- }),
-
- SelectFilter::make('Breed SelectFilter', 'breed_select_filter')
- ->options(
- Breed::query()
- ->orderBy('name')
- ->get()
- ->keyBy('id')
- ->map(fn ($breed) => $breed->name)
- ->toArray()
- )
- ->filter(function (Builder $builder, string $value) {
- return $builder->where('breed_id', $value);
- })
- ->setCustomFilterLabel('livewire-tables::tests.testFilterLabel')
- ->setFilterPillBlade('livewire-tables::tests.testFilterPills'),
- ];
- }
}
diff --git a/tests/Http/Livewire/PetsTableNoFilters.php b/tests/Http/Livewire/PetsTableNoFilters.php
index 20bc7a7b9..d209a6602 100644
--- a/tests/Http/Livewire/PetsTableNoFilters.php
+++ b/tests/Http/Livewire/PetsTableNoFilters.php
@@ -6,15 +6,8 @@
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Views\Column;
-class PetsTableNoFilters extends DataTableComponent
+class PetsTableNoFilters extends PetsTable
{
- public $model = Pet::class;
-
- public function configure(): void
- {
- $this->setPrimaryKey('id');
- }
-
public function columns(): array
{
return [
@@ -36,4 +29,10 @@ public function columns(): array
}),
];
}
+
+ public function filters(): array
+ {
+ return [
+ ];
+ }
}
diff --git a/tests/TestCase.php b/tests/TestCase.php
index 0bf3aa3e2..92cec4db2 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -9,7 +9,7 @@
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
use Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider;
-use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\{BreedsTable,PetsTable,PetsTableUnpaginated,PetsTableWithOwner,SpeciesTable};
+use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\{BreedsTable,PetsTable,PetsTableEvents,PetsTableUnpaginated,PetsTableWithOwner,SpeciesTable};
use Rappasoft\LaravelLivewireTables\Tests\Http\TestComponent;
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
use Rappasoft\LaravelLivewireTables\Tests\Models\Owner;
@@ -107,6 +107,21 @@ protected function setupBasicTable()
$this->basicTable->render();
}
+ protected function setupEventsTable()
+ {
+ $view = view('livewire-tables::datatable');
+ $this->eventsTable = new PetsTableEvents;
+ $this->eventsTable->boot();
+ $this->eventsTable->bootedComponentUtilities();
+ $this->eventsTable->bootedWithData();
+ $this->eventsTable->bootedWithColumns();
+ $this->eventsTable->bootedWithColumnSelect();
+ $this->eventsTable->bootedWithSecondaryHeader();
+ $this->eventsTable->booted();
+ $this->eventsTable->renderingWithPagination($view, []);
+ $this->eventsTable->render();
+ }
+
protected function setupBreedsTable()
{
$view = view('livewire-tables::datatable');
diff --git a/tests/Traits/Visuals/ComponentVisualsTest.php b/tests/Traits/Visuals/ComponentVisualsTest.php
index 62cd5cf6b..1b2df3adf 100644
--- a/tests/Traits/Visuals/ComponentVisualsTest.php
+++ b/tests/Traits/Visuals/ComponentVisualsTest.php
@@ -48,15 +48,15 @@ public function test_fails_when_table_has_no_pk(): void
Livewire::test(NoPrimaryKeyTable::class);
} catch (DataTableConfigurationException $DataTableConfigurationException) {
$this->testErrors = true;
- $this->assertSame('You must set a primary key using setPrimaryKey in the configure method.', substr($DataTableConfigurationException->getMessage(), 0, 71));
+ $this->assertSame('You must set a primary key using setPrimaryKey in the configure method, or configuring/configured lifecycle hooks', substr($DataTableConfigurationException->getMessage(), 0, 113));
} catch (ViewException $ViewException) {
$this->testErrors = true;
- $this->assertSame('You must set a primary key using setPrimaryKey in the configure method.', substr($ViewException->getMessage(), 0, 71));
+ $this->assertSame('You must set a primary key using setPrimaryKey in the configure method, or configuring/configured lifecycle hooks', substr($ViewException->getMessage(), 0, 113));
} catch (Exception $standardException) {
$this->testErrors = true;
- $this->assertSame('You must set a primary key using setPrimaryKey in the configure method.', substr($standardException->getMessage(), 0, 71));
+ $this->assertSame('You must set a primary key using setPrimaryKey in the configure method, or configuring/configured lifecycle hooks', substr($standardException->getMessage(), 0, 113));
}
if (! $this->testErrors) {
$this->fail('Did Not Throw Error - Missing Primary Key');
diff --git a/tests/Traits/Visuals/HideableTableTest.php b/tests/Traits/Visuals/HideableTableTest.php
new file mode 100644
index 000000000..6af61ed75
--- /dev/null
+++ b/tests/Traits/Visuals/HideableTableTest.php
@@ -0,0 +1,69 @@
+setPrimaryKey('id');
+ }
+
+ public function bulkActions(): array
+ {
+ return ['exportBulk' => 'exportBulk'];
+ }
+
+ public function exportBulk($items)
+ {
+ return $items;
+ }
+ });
+ Livewire::test($petsTable)
+ ->assertSet('shouldBeDisplayed', true)
+ ->assertSee('Cartman');
+ }
+
+ public function test_can_see_table_if_shown(): void
+ {
+ $this->setupEventsTable();
+
+ Livewire::test($this->eventsTable)
+ ->assertSet('shouldBeDisplayed', false)
+ ->assertSee('Cartman')
+ ->call('setShouldBeDisplayed')
+ ->assertSet('shouldBeDisplayed', true)
+ ->assertSee('Cartman');
+
+ }
+
+ public function test_can_not_see_hidden_table(): void
+ {
+ $this->setupEventsTable();
+
+ Livewire::test($this->eventsTable)
+ ->assertSee('Cartman')
+ ->assertSet('shouldBeDisplayed', false)
+ ->assertSee('Cartman');
+ }
+
+ public function test_can_show_hidden_table(): void
+ {
+ $this->setupEventsTable();
+
+ Livewire::test($this->eventsTable)
+ ->assertSet('shouldBeDisplayed', false)
+ ->dispatch('showTable')
+ ->assertSet('shouldBeDisplayed', true)
+ ->assertSee('Cartman');
+
+ }
+}