Skip to content

Commit 955fff4

Browse files
author
Herbert Maschke
committed
introduce column groups
1 parent fed8ad6 commit 955fff4

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,21 @@ class ComplexDemoTable extends LivewireDatatable
138138

139139
Column::name('name')
140140
->defaultSort('asc')
141+
->group('group1')
141142
->searchable()
142143
->hideable()
143144
->filterable(),
144145

145146
Column::name('planet.name')
146147
->label('Planet')
148+
->group('group1')
147149
->searchable()
148150
->hideable()
149151
->filterable($this->planets),
150152

151153
DateColumn::name('dob')
152154
->label('DOB')
155+
->group('group2')
153156
->filterable()
154157
->hide(),
155158

@@ -171,6 +174,7 @@ class ComplexDemoTable extends LivewireDatatable
171174
|_static_ **delete**|[*String* $primaryKey default: 'id']|Adds a column with a delete button, which will call ```$this->model::destroy($primaryKey)```|```Column::delete()```|
172175
|_static_ **checkbox**|[*String* $column default: 'id']|Adds a column with a checkbox. The component public property ```$selected``` will contain an array of the named column from checked rows, |```Column::checkbox()```|
173176
|**label**|*String* $name|Changes the display name of a column|```Column::name('id')->label('ID)```|
177+
|**group**|*String* $group|Assign the column to a group. Allows to toggle the visibility of all columns of a group at once|```Column::name('id')->group('my-group')```|
174178
|**format**|[*String* $format]|Formats the column value according to type. Dates/times will use the default format or the argument |```Column::name('email_verified_at')->filterable(),```|
175179
|**hide**| |Marks column to start as hidden|```Column::name('id')->hidden()```|
176180
|**sortBy**|*String\|Expression* $column|Changes the query by which the column is sorted|```Column::name('dob')->sortBy('DATE_FORMAT(users.dob, "%m%d%Y")'),```|
@@ -209,6 +213,10 @@ NumberColumn::name('students.age:min')->label('Student Min'),
209213
NumberColumn::name('students.age:max')->label('Student Max'),
210214
```
211215

216+
### Column Groups
217+
218+
When you have a very big table with a lot of columns, it is possible to create 'column groups' that allows the user to toggle the visibility of a whole group at once. Use `->group('NAME')` at any column to achieve this.
219+
212220
### Custom column names
213221
It is still possible to take full control over your table, you can define a ```builder``` method using whatever query you like, using your own joins, groups whatever, and then name your columns using your normal SQL syntax:
214222

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
@if($hideable === 'select')
5252
@include('datatables::hide-column-multiselect')
5353
@endif
54+
55+
@foreach ($columnGroups as $name => $group)
56+
<button wire:click="toggleGroup('{{ $name }}')" class="flex items-center space-x-2 px-3 border border-green-400 rounded-md bg-white text-green-500 text-xs leading-4 font-medium uppercase tracking-wider hover:bg-green-200 focus:outline-none"><span>{{ __('Toggle :group', ['group' => $name]) }}</span>
57+
</button>
58+
@endforeach
5459
</div>
5560
</div>
5661

src/Column.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Column
99
{
1010
public $type = 'string';
1111
public $label;
12+
public $group;
1213
public $name;
1314
public $select;
1415
public $joins;
@@ -333,6 +334,16 @@ public function field()
333334
return Str::afterLast($this->name, '.');
334335
}
335336

337+
/**
338+
* You can use group(null) to revoke a column from a group, if necessary.
339+
*/
340+
public function group($group)
341+
{
342+
$this->group = $group;
343+
344+
return $this;
345+
}
346+
336347
public function relations()
337348
{
338349
return $this->isBaseColumn() ? null : collect(explode('.', Str::beforeLast($this->name, '.')));

src/Http/Livewire/LivewireDatatable.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class LivewireDatatable extends Component
5959
public $complexQuery;
6060
public $title;
6161
public $name;
62+
public $columnGroups = [];
6263
public $userFilter;
6364
public $persistComplexQuery;
6465
public $persistHiddenColumns = true;
@@ -176,6 +177,7 @@ public function mount(
176177
$this->initialiseHiddenColumns();
177178
$this->initialiseFilters();
178179
$this->initialisePerPage();
180+
$this->initialiseColumnGroups();
179181
}
180182

181183
public function columns()
@@ -189,6 +191,7 @@ public function getViewColumns()
189191
return collect($column)->only([
190192
'hidden',
191193
'label',
194+
'group',
192195
'content',
193196
'align',
194197
'type',
@@ -537,6 +540,15 @@ public function initialisePerPage()
537540
}
538541
}
539542

543+
public function initialiseColumnGroups()
544+
{
545+
array_map(function ($column) {
546+
if ($column['group'] ?? false) {
547+
$this->columnGroups[$column['group']][] = $column['name'] ?? $column['label'];
548+
}
549+
}, $this->columns);
550+
}
551+
540552
public function initialiseFilters()
541553
{
542554
if (! $this->persistFilters) {
@@ -652,6 +664,15 @@ public function toggle($index)
652664
}
653665
}
654666

667+
public function toggleGroup($group)
668+
{
669+
foreach ($this->columns as $key => $column) {
670+
if ($column['group'] === $group) {
671+
$this->toggle($key);
672+
}
673+
}
674+
}
675+
655676
public function doBooleanFilter($index, $value)
656677
{
657678
$this->activeBooleanFilters[$index] = $value;

0 commit comments

Comments
 (0)