Skip to content

Commit 9808eaa

Browse files
committed
Ability to hide a column based on a condition or permanently
1 parent 26a8315 commit 9808eaa

File tree

5 files changed

+51
-33
lines changed

5 files changed

+51
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
1111
- Arabic translations
1212
- Ability to add a link to make table rows clickable
1313
- Added the ability to change the sort icons
14+
- Ability to hide a column based on a condition or permanently
1415

1516
### Updated
1617

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class UsersTable extends TableComponent
4848
return [
4949
Column::make('ID')
5050
->searchable()
51-
->sortable(),
51+
->sortable()
52+
->hideIf(!auth()->user()->hasRole('admin')),
5253
Column::make('Name')
5354
->searchable()
5455
->sortable(),
@@ -138,6 +139,16 @@ public function customAttribute() : self;
138139
* This view will be used for the column, can still be used with sortable and searchable.
139140
*/
140141
public function view($view, $viewModelName = 'model') : self;
142+
143+
/**
144+
* Hide this column permanently
145+
*/
146+
public function hide() : self;
147+
148+
/**
149+
* Hide this column based on a condition. i.e.: user has or doesn't have a role or permission
150+
*/
151+
public function hideIf($condition) : self;
141152
```
142153

143154
### Properties

resources/views/includes/_body.blade.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ class="{{ $this->setTableRowClass($model) }}"
2727
@endif
2828

2929
@foreach($columns as $column)
30-
<td
31-
class="{{ $this->setTableDataClass($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
32-
id="{{ $this->setTableDataId($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
33-
@foreach ($this->setTableDataAttributes($column->attribute, Arr::get($model->toArray(), $column->attribute)) as $key => $value)
34-
{{ $key }}="{{ $value }}"
35-
@endforeach
36-
>
37-
@include('laravel-livewire-tables::includes._column-data')
38-
</td>
30+
@if (!$column->isHidden())
31+
<td
32+
class="{{ $this->setTableDataClass($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
33+
id="{{ $this->setTableDataId($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
34+
@foreach ($this->setTableDataAttributes($column->attribute, Arr::get($model->toArray(), $column->attribute)) as $key => $value)
35+
{{ $key }}="{{ $value }}"
36+
@endforeach
37+
>
38+
@include('laravel-livewire-tables::includes._column-data')
39+
</td>
40+
@endif
3941
@endforeach
4042

4143
@if($checkbox && $checkboxLocation === 'right')

resources/views/includes/_columns.blade.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@
33
@endif
44

55
@foreach($columns as $column)
6-
<th
7-
class="{{ $this->setTableHeadClass($column->attribute) }}"
8-
id="{{ $this->setTableHeadId($column->attribute) }}"
9-
@foreach ($this->setTableHeadAttributes($column->attribute) as $key => $value)
10-
{{ $key }}="{{ $value }}"
11-
@endforeach
12-
>
13-
@if($column->sortable)
14-
<span style="cursor: pointer;" wire:click="sort('{{ $column->attribute }}')">
15-
{{ $column->text }}
6+
@if (!$column->isHidden())
7+
<th
8+
class="{{ $this->setTableHeadClass($column->attribute) }}"
9+
id="{{ $this->setTableHeadId($column->attribute) }}"
10+
@foreach ($this->setTableHeadAttributes($column->attribute) as $key => $value)
11+
{{ $key }}="{{ $value }}"
12+
@endforeach
13+
>
14+
@if($column->sortable)
15+
<span style="cursor: pointer;" wire:click="sort('{{ $column->attribute }}')">
16+
{{ $column->text }}
1617

17-
@if ($sortField !== $column->attribute)
18-
<i class="{{ $sortDefaultClass }}"></i>
19-
@elseif ($sortDirection === 'asc')
20-
<i class="{{ $ascSortClass }}"></i>
21-
@else
22-
<i class="{{ $descSortClass }}"></i>
23-
@endif
24-
</span>
25-
@else
26-
{{ $column->text }}
27-
@endif
28-
</th>
18+
@if ($sortField !== $column->attribute)
19+
<i class="{{ $sortDefaultClass }}"></i>
20+
@elseif ($sortDirection === 'asc')
21+
<i class="{{ $ascSortClass }}"></i>
22+
@else
23+
<i class="{{ $descSortClass }}"></i>
24+
@endif
25+
</span>
26+
@else
27+
{{ $column->text }}
28+
@endif
29+
</th>
30+
@endif
2931
@endforeach
3032

3133
@if($checkbox && $checkboxLocation === 'right')

src/Views/Column.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
namespace Rappasoft\LaravelLivewireTables\Views;
44

55
use Illuminate\Support\Str;
6+
use Rappasoft\LaravelLivewireTables\Traits\CanBeHidden;
67
use Rappasoft\LaravelLivewireTables\Traits\HasComponents;
78

89
/**
910
* Class Column.
1011
*/
1112
class Column
1213
{
13-
use HasComponents;
14+
use CanBeHidden,
15+
HasComponents;
1416

1517
/**
1618
* @var

0 commit comments

Comments
 (0)