Skip to content

Commit 0d66457

Browse files
thyseusHerbert Maschke
andauthored
introduce the ability to define which columns the user should be able to hide (MedicOneSystems#286)
Co-authored-by: Herbert Maschke <thyseus@pm.me>
1 parent d5b6f38 commit 0d66457

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ class ComplexDemoTable extends LivewireDatatable
140140
Column::name('name')
141141
->defaultSort('asc')
142142
->searchable()
143+
->hideable()
143144
->filterable(),
144145

145146
Column::name('planet.name')
146147
->label('Planet')
147148
->searchable()
149+
->hideable()
148150
->filterable($this->planets),
149151

150152
DateColumn::name('dob')
@@ -175,6 +177,7 @@ class ComplexDemoTable extends LivewireDatatable
175177
|**round**|[*Integer* $precision (default: 0)]|Rounds value to given precision|```Column::name('age')->round()```|
176178
|**defaultSort**|[*String* $direction (default: 'desc')]|Marks the column as the default search column|```Column::name('name')->defaultSort('asc')```|
177179
|**searchable**| |Includes the column in the global search|```Column::name('name')->searchable()```|
180+
|**hideable**| |The user is able to toggle the visibility of this column|```Column::name('name')->hideable()```|
178181
|**filterable**|[*Array* $options], [*String* $filterScope]|Adds a filter to the column, according to Column type. If an array of options is passed it wil be used to populate a select input. If the column is a scope column then the name of the filter scope must also be passed|```Column::name('allegiance')->filterable(['Rebellion', 'Empire'])```|
179182
|**filterOn**|*String/Array* $statement|Allows you to specify a column name or sql statement upon which to perform the filter (must use SQL syntax, not Eloquent eg. ```'users.name'``` instead of ```'user.name'```). Useful if using a callback to modify the displayed values. Can pass a single string or array of strings which will be combined with ```OR```|```Column::callback(['name', 'allegiance'], function ($name, $allegiance) { return "$name is allied to $allegiance"; })->filterable(['Rebellion', 'Empire'])->filterOn('users.allegiance')```|
180183
|**view**|*String* $viewName| Passes the column value, whole row of values, and any additional parameters to a view template | _(see below)_|

resources/views/livewire/datatables/datatable.blade.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@
5757
@if($hideable === 'buttons')
5858
<div class="p-2 grid grid-cols-8 gap-2">
5959
@foreach($this->columns as $index => $column)
60-
<button wire:click.prefetch="toggle('{{ $index }}')" class="px-3 py-2 rounded text-white text-xs focus:outline-none
61-
{{ $column['hidden'] ? 'bg-blue-100 hover:bg-blue-300 text-blue-600' : 'bg-blue-500 hover:bg-blue-800' }}">
62-
{{ $column['label'] }}
63-
</button>
60+
@if ($column['hideable'])
61+
<button wire:click.prefetch="toggle('{{ $index }}')" class="px-3 py-2 rounded text-white text-xs focus:outline-none
62+
{{ $column['hidden'] ? 'bg-blue-100 hover:bg-blue-300 text-blue-600' : 'bg-blue-500 hover:bg-blue-800' }}">
63+
{{ $column['label'] }}
64+
</button>
65+
@endif
6466
@endforeach
6567
</div>
6668
@endif

resources/views/livewire/datatables/header-inline-hide.blade.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-xs leadi
3434
</span>
3535
</button>
3636
@endif
37-
<button wire:click.prefetch="toggle('{{ $index }}')"
38-
class="absolute bottom-1 right-1 focus:outline-none">
39-
<x-icons.arrow-circle-left class="h-3 w-3 text-gray-300 hover:text-blue-400" />
40-
</button>
37+
38+
@if ($column['hideable'])
39+
<button wire:click.prefetch="toggle('{{ $index }}')"
40+
class="absolute bottom-1 right-1 focus:outline-none">
41+
<x-icons.arrow-circle-left class="h-3 w-3 text-gray-300 hover:text-blue-400" />
42+
</button>
43+
@endif
4144
</div>

src/Column.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Column
1717
public $searchable;
1818
public $filterOn;
1919
public $filterable;
20+
public $hideable;
2021
public $sort;
2122
public $unsortable;
2223
public $defaultSort;
@@ -118,6 +119,13 @@ public function defaultSort($direction = true)
118119
return $this;
119120
}
120121

122+
public function hideable()
123+
{
124+
$this->hideable = true;
125+
126+
return $this;
127+
}
128+
121129
public function unsortable()
122130
{
123131
$this->unsortable = true;

src/Http/Livewire/LivewireDatatable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public function getViewColumns()
191191
'align',
192192
'type',
193193
'filterable',
194+
'hideable',
194195
'complex',
195196
'filterView',
196197
'name',
@@ -1347,7 +1348,7 @@ public function getDisplayValue($index, $value)
13471348
: $value;
13481349
}
13491350

1350-
/* This can be called to apply highlting of the search term to some string.
1351+
/* This can be called to apply highlighting of the search term to some string.
13511352
* Motivation: Call this from your Column::Callback to apply highlight to a chosen section of the result.
13521353
*/
13531354
public function highlightStringWithCurrentSearchTerm(string $originalString)

0 commit comments

Comments
 (0)