Skip to content

Commit 062dee9

Browse files
committed
Button group columns
1 parent bd6606c commit 062dee9

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
77
### Added
88

99
- Added functionality to display BooleanColumn as Yes/No instead of icons.
10+
- Added ButtonGroupColumn for multiple LinkColumns in one group. Pretty much built in action buttons support.
1011

1112
## [2.5.0] - 2022-05-03
1213

docs/columns/other-column-types.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,36 @@ LinkColumn::make('Action')
126126
'class' => 'rounded-full',
127127
'alt' => $row->name . ' Avatar',
128128
]),
129+
```
130+
131+
## Button Group Columns
132+
133+
Button group columns let you provide an array of LinkColumns to display in a single cell.
134+
135+
```php
136+
ButtonGroupColumn::make('Actions')
137+
->attributes(function($row) {
138+
return [
139+
'class' => 'space-x-2',
140+
];
141+
})
142+
->buttons([
143+
LinkColumn::make('View')
144+
->title(fn($row) => 'View ' . $row->name)
145+
->location(fn($row) => route('user.show', $row)),
146+
->attributes(function($row) {
147+
return [
148+
'class' => 'underline text-blue-500 hover:no-underline',
149+
];
150+
}),
151+
LinkColumn::make('Edit ' . $row->name)
152+
->title(fn($row) => 'Edit')
153+
->location(fn($row) => route('user.edit', $row)),
154+
->attributes(function($row) {
155+
return [
156+
'target' => '_blank',
157+
'class' => 'underline text-blue-500 hover:no-underline',
158+
];
159+
}),
160+
]),
129161
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div {!! count($attributes) ? $column->arrayToAttributes($attributes) : '' !!}>
2+
@foreach($buttons as $button)
3+
{!! $button->getContents($row) !!}
4+
@endforeach
5+
</div>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Views\Columns;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Rappasoft\LaravelLivewireTables\Views\Column;
7+
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ButtonGroupColumnConfiguration;
8+
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ButtonGroupColumnHelpers;
9+
10+
class ButtonGroupColumn extends Column
11+
{
12+
use ButtonGroupColumnConfiguration,
13+
ButtonGroupColumnHelpers;
14+
15+
protected array $buttons = [];
16+
protected string $view = 'livewire-tables::includes.columns.button-group';
17+
protected $attributesCallback;
18+
19+
public function __construct(string $title, string $from = null)
20+
{
21+
parent::__construct($title, $from);
22+
23+
$this->label(fn () => null);
24+
}
25+
26+
public function getContents(Model $row)
27+
{
28+
return view($this->getView())
29+
->withColumn($this)
30+
->withRow($row)
31+
->withButtons($this->getButtons())
32+
->withAttributes($this->hasAttributesCallback() ? app()->call($this->getAttributesCallback(), ['row' => $row]) : []);
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Configuration;
4+
5+
trait ButtonGroupColumnConfiguration
6+
{
7+
public function buttons(array $buttons): self
8+
{
9+
$this->buttons = $buttons;
10+
11+
return $this;
12+
}
13+
14+
public function attributes(callable $callback): self
15+
{
16+
$this->attributesCallback = $callback;
17+
18+
return $this;
19+
}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Helpers;
4+
5+
use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
6+
7+
trait ButtonGroupColumnHelpers
8+
{
9+
public function getView(): string
10+
{
11+
return $this->view;
12+
}
13+
14+
public function getButtons(): array
15+
{
16+
return collect($this->buttons)
17+
->reject(fn($button) => ! $button instanceof LinkColumn)
18+
->toArray();
19+
}
20+
21+
public function getAttributesCallback()
22+
{
23+
return $this->attributesCallback;
24+
}
25+
26+
public function hasAttributesCallback()
27+
{
28+
return $this->attributesCallback !== null;
29+
}
30+
}

0 commit comments

Comments
 (0)