Skip to content

Commit a08fac1

Browse files
author
Herbert Maschke
committed
introduce ability to make i18nable custom column category labels
1 parent b5b567d commit a08fac1

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
@endif
5454

5555
@foreach ($columnGroups as $name => $group)
56-
<button wire:click="toggleGroup('{{ $name }}')" class="px-3 py-2 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 class="flex items-center h-5">{{ __('Toggle :group', ['group' => $name]) }}</span>
56+
<button wire:click="toggleGroup('{{ $name }}')" class="px-3 py-2 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 class="flex items-center h-5">{{ $group['label'] ? __($group['label']) : __('Toggle :group', ['group' => $name]) }}</span>
5757
</button>
5858
@endforeach
5959
</div>

src/Column.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Column
1010
public $type = 'string';
1111
public $label;
1212
public $group;
13+
public $groupLabel;
1314
public $name;
1415
public $select;
1516
public $joins;
@@ -336,10 +337,13 @@ public function field()
336337

337338
/**
338339
* You can use group(null) to revoke a column from a group, if necessary.
340+
* The label can be a i18n placeholder like 'app.my_string' and it will be
341+
* automatically translated via __().
339342
*/
340-
public function group($group)
343+
public function group($group, $label = null)
341344
{
342345
$this->group = $group;
346+
$this->groupLabel = $label;
343347

344348
return $this;
345349
}

src/Http/Livewire/LivewireDatatable.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public function getViewColumns()
192192
'hidden',
193193
'label',
194194
'group',
195+
'groupLabel',
195196
'content',
196197
'align',
197198
'type',
@@ -278,7 +279,7 @@ public function resolveAdditionalSelects($column)
278279
return $selects->count() > 1
279280
? new Expression("CONCAT_WS('" . static::SEPARATOR . "' ," .
280281
collect($selects)->map(function ($select) {
281-
return "IF($select, $select, '')";
282+
return "COALESCE($select, NULL)";
282283
})->join(', ') . ')')
283284
: $selects->first();
284285
}
@@ -543,8 +544,12 @@ public function initialisePerPage()
543544
public function initialiseColumnGroups()
544545
{
545546
array_map(function ($column) {
546-
if ($column['group'] ?? false) {
547-
$this->columnGroups[$column['group']][] = $column['name'] ?? $column['label'];
547+
if (isset ($column['group']) && isset($column['groupLabel'])) {
548+
$this->columnGroups[$column['group']]['label'] = $column['groupLabel'];
549+
}
550+
551+
if (isset ($column['group'])) {
552+
$this->columnGroups[$column['group']]['columns'][] = $column['label'] ?? $column['name'];
548553
}
549554
}, $this->columns);
550555
}

0 commit comments

Comments
 (0)