Skip to content

Commit 3d39eb8

Browse files
committed
Ability to set per component settings from the mount method
1 parent f46664f commit 3d39eb8

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
1414
- Render method to columns which returns whatever you put into it, you can return a view, html, an attribute, etc.
1515
- Pulled in and modified the HTML component library from laravelcollective so you an return html components from the render method. i.e.: $this->image(...);
1616
- Added new loading config on whether to keep displaying the current data while loading or collapse it
17+
- Added ability to set frontend framework specific options via the mount method on a per component basis.
1718

1819
### Changed
1920

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,32 @@ public function email($email): string
284284
public function html($html): HtmlString
285285
```
286286

287+
### Setting Options
288+
289+
There are some frontend framework specific options that can be set.
290+
291+
These have to be set from the `mount()` method of your component.
292+
293+
They are done this way instead of the config file that way you can have per-component control over these settings.
294+
295+
```php
296+
public function mount()
297+
{
298+
$this->setOptions([
299+
// The class set on the table when using bootstrap
300+
'bootstrap.classes.table' => 'table table-striped table-bordered',
301+
302+
// Whether or not the table is wrapped in a `.container-fluid` or not
303+
'bootstrap.container' => true,
304+
305+
// Whether or not the table is wrapped in a `.table-responsive` or not
306+
'bootstrap.responsive' => true,
307+
]);
308+
}
309+
```
310+
311+
For this to work you have to pass an associative array of overrides to the `setOptions()` method. The above are the defaults, if you're not changing them then you can leave them out.
312+
287313
### Passing Properties
288314

289315
To pass properties from your blade view to your table, you can use the normal Livewire mount method:

resources/views/bootstrap-4/table-component.blade.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
<div class="container-fluid" @if (is_numeric($refresh)) wire:poll.{{ $refresh }}.ms @elseif(is_string($refresh)) wire:poll="{{ $refresh }}" @endif>
1+
<div
2+
class="{{ $this->getOption('bootstrap.container') ? 'container-fluid' : '' }}"
3+
@if (is_numeric($refresh)) wire:poll.{{ $refresh }}.ms @elseif(is_string($refresh)) wire:poll="{{ $refresh }}" @endif
4+
>
25
@include('laravel-livewire-tables::'.config('laravel-livewire-tables.theme').'.includes.offline')
36
@include('laravel-livewire-tables::'.config('laravel-livewire-tables.theme').'.includes.options')
47

5-
<div class="table-responsive">
6-
<table class="table table-bordered table-striped">
8+
@if ($this->getOption('bootstrap.responsive'))
9+
<div class="table-responsive">
10+
@endif
11+
<table class="{{ $this->getOption('bootstrap.classes.table') }}">
712
@include('laravel-livewire-tables::'.config('laravel-livewire-tables.theme').'.includes.thead')
813

914
@include('laravel-livewire-tables::'.config('laravel-livewire-tables.theme').'.includes.loading')
@@ -16,7 +21,9 @@
1621

1722
@include('laravel-livewire-tables::'.config('laravel-livewire-tables.theme').'.includes.tfoot')
1823
</table>
19-
</div><!--table-responsive-->
24+
@if ($this->getOption('bootstrap.responsive'))
25+
</div><!--table-responsive-->
26+
@endif
2027

2128
@include('laravel-livewire-tables::'.config('laravel-livewire-tables.theme').'.includes.pagination')
2229
</div>

src/TableComponent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Livewire\Component;
99
use Livewire\WithPagination;
1010
use Rappasoft\LaravelLivewireTables\Traits\Loading;
11+
use Rappasoft\LaravelLivewireTables\Traits\Options;
1112
use Rappasoft\LaravelLivewireTables\Traits\Pagination;
1213
use Rappasoft\LaravelLivewireTables\Traits\Search;
1314
use Rappasoft\LaravelLivewireTables\Traits\Sorting;
@@ -20,6 +21,7 @@
2021
abstract class TableComponent extends Component
2122
{
2223
use Loading,
24+
Options,
2325
Pagination,
2426
Search,
2527
Sorting,
@@ -62,6 +64,8 @@ public function __construct($id = null)
6264
$this->paginationTheme = 'bootstrap';
6365
}
6466

67+
$this->setOptions();
68+
6569
parent::__construct($id);
6670
}
6771

src/Traits/Options.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits;
4+
5+
use Illuminate\Support\Arr;
6+
7+
/**
8+
* Trait Options.
9+
*/
10+
trait Options
11+
{
12+
/**
13+
* @var array
14+
*/
15+
protected $options = [
16+
'bootstrap' => [
17+
'classes' => [
18+
'table' => 'table table-bordered table-striped',
19+
],
20+
'container' => true,
21+
'responsive' => true,
22+
],
23+
];
24+
25+
/**
26+
* @param $option
27+
*
28+
* @return mixed
29+
*/
30+
public function getOption($option)
31+
{
32+
return Arr::dot($this->options)[$option] ?? null;
33+
}
34+
35+
/**
36+
* @param array $overrides
37+
*/
38+
protected function setOptions(array $overrides = []): void
39+
{
40+
foreach ($overrides as $key => $value) {
41+
data_set($this->options, $key, $value);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)