Skip to content

Commit 77993df

Browse files
committed
Add ability to hide columns with a conditional
1 parent b0e8c6f commit 77993df

File tree

8 files changed

+92
-51
lines changed

8 files changed

+92
-51
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- Added hideIf for columns to hide a column with a conditional, works out of the box for cells not using rowView, if using rowView you must wrap the cells you want to hide in the same conditional. [See documentation](https://github.com/rappasoft/laravel-livewire-tables/wiki/Conditionally-hiding-columns).
10+
711
## [1.4.0] - 2021-04-29
812

913
### Added
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
@foreach($columns as $column)
2-
<td>
3-
@if ($column->asHtml)
4-
{{ new \Illuminate\Support\HtmlString($column->formatted($row)) }}
5-
@else
6-
{{ $column->formatted($row) }}
7-
@endif
8-
</td>
2+
@if ($column->isVisible())
3+
<td>
4+
@if ($column->asHtml)
5+
{{ new \Illuminate\Support\HtmlString($column->formatted($row)) }}
6+
@else
7+
{{ $column->formatted($row) }}
8+
@endif
9+
</td>
10+
@endif
911
@endforeach

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
@endif
1111

1212
@foreach($columns as $column)
13-
@if ($column->isBlank())
14-
<x-livewire-tables::bs4.table.heading />
15-
@else
16-
<x-livewire-tables::bs4.table.heading
17-
:sortable="$column->isSortable()"
18-
:column="$column->column()"
19-
:direction="$column->column() ? $sorts[$column->column()] ?? null : null"
20-
:text="$column->text() ?? ''"
21-
:class="$column->class() ?? ''"
22-
/>
13+
@if ($column->isVisible())
14+
@if ($column->isBlank())
15+
<x-livewire-tables::bs4.table.heading />
16+
@else
17+
<x-livewire-tables::bs4.table.heading
18+
:sortable="$column->isSortable()"
19+
:column="$column->column()"
20+
:direction="$column->column() ? $sorts[$column->column()] ?? null : null"
21+
:text="$column->text() ?? ''"
22+
:class="$column->class() ?? ''"
23+
/>
24+
@endif
2325
@endif
2426
@endforeach
2527
</x-slot>
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
@foreach($columns as $column)
2-
<td>
3-
@if ($column->asHtml)
4-
{{ new \Illuminate\Support\HtmlString($column->formatted($row)) }}
5-
@else
6-
{{ $column->formatted($row) }}
7-
@endif
8-
</td>
2+
@if ($column->isVisible())
3+
<td>
4+
@if ($column->asHtml)
5+
{{ new \Illuminate\Support\HtmlString($column->formatted($row)) }}
6+
@else
7+
{{ $column->formatted($row) }}
8+
@endif
9+
</td>
10+
@endif
911
@endforeach

resources/views/bootstrap-5/includes/table.blade.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ class="form-check-input"
1111
@endif
1212

1313
@foreach($columns as $column)
14-
@if ($column->isBlank())
15-
<x-livewire-tables::bs5.table.heading />
16-
@else
17-
<x-livewire-tables::bs5.table.heading
18-
:sortable="$column->isSortable()"
19-
:column="$column->column()"
20-
:direction="$column->column() ? $sorts[$column->column()] ?? null : null"
21-
:text="$column->text() ?? ''"
22-
:class="$column->class() ?? ''"
23-
/>
14+
@if ($column->isVisible())
15+
@if ($column->isBlank())
16+
<x-livewire-tables::bs5.table.heading />
17+
@else
18+
<x-livewire-tables::bs5.table.heading
19+
:sortable="$column->isSortable()"
20+
:column="$column->column()"
21+
:direction="$column->column() ? $sorts[$column->column()] ?? null : null"
22+
:text="$column->text() ?? ''"
23+
:class="$column->class() ?? ''"
24+
/>
25+
@endif
2426
@endif
2527
@endforeach
2628
</x-slot>
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
@foreach($columns as $column)
2-
<x-livewire-tables::table.cell>
3-
@if ($column->asHtml)
4-
{{ new \Illuminate\Support\HtmlString($column->formatted($row)) }}
5-
@else
6-
{{ $column->formatted($row) }}
7-
@endif
8-
</x-livewire-tables::table.cell>
2+
@if ($column->isVisible())
3+
<x-livewire-tables::table.cell>
4+
@if ($column->asHtml)
5+
{{ new \Illuminate\Support\HtmlString($column->formatted($row)) }}
6+
@else
7+
{{ $column->formatted($row) }}
8+
@endif
9+
</x-livewire-tables::table.cell>
10+
@endif
911
@endforeach

resources/views/tailwind/includes/table.blade.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ class="rounded-md shadow-sm border-gray-300 block transition duration-150 ease-i
1313
@endif
1414

1515
@foreach($columns as $column)
16-
@if ($column->isBlank())
17-
<x-livewire-tables::table.heading />
18-
@else
19-
<x-livewire-tables::table.heading
20-
:sortable="$column->isSortable()"
21-
:column="$column->column()"
22-
:direction="$column->column() ? $sorts[$column->column()] ?? null : null"
23-
:text="$column->text() ?? ''"
24-
:class="$column->class() ?? ''"
25-
/>
16+
@if ($column->isVisible())
17+
@if ($column->isBlank())
18+
<x-livewire-tables::table.heading />
19+
@else
20+
<x-livewire-tables::table.heading
21+
:sortable="$column->isSortable()"
22+
:column="$column->column()"
23+
:direction="$column->column() ? $sorts[$column->column()] ?? null : null"
24+
:text="$column->text() ?? ''"
25+
:class="$column->class() ?? ''"
26+
/>
27+
@endif
2628
@endif
2729
@endforeach
2830
</x-slot>

src/Views/Column.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class Column
5959
*/
6060
public bool $asHtml = false;
6161

62+
/**
63+
* @var bool
64+
*/
65+
public bool $hidden = false;
66+
6267
/**
6368
* Column constructor.
6469
*
@@ -262,4 +267,24 @@ public function getSearchCallback(): ?callable
262267
{
263268
return $this->searchCallback;
264269
}
270+
271+
/**
272+
* @param $condition
273+
*
274+
* @return $this
275+
*/
276+
public function hideIf($condition): self
277+
{
278+
$this->hidden = $condition;
279+
280+
return $this;
281+
}
282+
283+
/**
284+
* @return bool
285+
*/
286+
public function isVisible(): bool
287+
{
288+
return $this->hidden !== true;
289+
}
265290
}

0 commit comments

Comments
 (0)