Skip to content

Commit a82cc95

Browse files
thyseusHerbert Maschke
andauthored
introduce label column (MedicOneSystems#288)
Co-authored-by: Herbert Maschke <thyseus@pm.me>
1 parent 0d66457 commit a82cc95

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,21 @@ somewhere in your CSS
9898
### Provide a datasource by declaring public property ```$model``` **OR** public method ```builder()``` that returns an instance of ```Illuminate\Database\Eloquent\Builder```
9999
> ```php artisan livewire:datatable users-table --model=user``` --> 'app/Http/Livewire/UsersTable.php' with ```public $model = User::class```
100100
101-
102101
### Declare a public method ```columns``` that returns an array containing one or more ```Mediconesystems\LivewireDatatables\Column```
103102

104103

105104
## Columns
106105
Columns can be built using any of the static methods below, and then their attributes assigned using fluent method chains.
107106
There are additional specific types of Column; ```NumberColumn```, ```DateColumn```, ```TimeColumn```, using the correct one for your datatype will enable type-specific formatting and filtering:
108107

109-
110108
| Class | Description |
111109
|---|---|
112110
|Column|Generic string-based column. Filter will be a text input|
113111
|NumberColumn| Number-based column. Filters will be a numeric range|
114112
|BooleanColumn| Values will be automatically formatted to a yes/no icon, filters will be yes/no|
115113
|DateColumn| Values will be automatically formatted to the default date format. Filters will be a date range|
116114
|TimeColumn| Values will be automatically formatted to the default time format. Filters will be a time range|
115+
|LabelColumn| Fixed header string ("label") with fixed content string in every row. No SQL is executed at all|
117116
___
118117

119118
```php
@@ -152,7 +151,11 @@ class ComplexDemoTable extends LivewireDatatable
152151
DateColumn::name('dob')
153152
->label('DOB')
154153
->filterable()
155-
->hide()
154+
->hide(),
155+
156+
(new LabelColumn())
157+
->label('My custom heading')
158+
->content('This fixed string appears in every row')
156159
];
157160
}
158161
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@
102102
<input type="checkbox" wire:click="toggleSelectAll" @if(count($selected) === $this->results->total()) checked @endif class="form-checkbox mt-1 h-4 w-4 text-blue-600 transition duration-150 ease-in-out" />
103103
</div>
104104
</div>
105+
@elseif($column['type'] === 'label')
106+
<div class="table-cell overflow-hidden align-top">
107+
{{ $column['label'] ?? '' }}
108+
</div>
105109
@else
106110
<div class="table-cell overflow-hidden align-top">
107111
@isset($column['filterable'])
@@ -129,6 +133,8 @@
129133
@endif
130134
@elseif($column['type'] === 'checkbox')
131135
@include('datatables::checkbox', ['value' => $row->checkbox_attribute])
136+
@elseif($column['type'] === 'label')
137+
@include('datatables::label')
132138
@else
133139
<div class="table-cell px-6 py-2 whitespace-no-wrap @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @else text-left @endif {{ $this->cellClasses($row, $column) }}">
134140
{!! $row->{$column['name']} !!}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="table-cell px-6 py-2 whitespace-no-wrap @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @else text-left @endif {{ $this->cellClasses($row, $column) }}">
2+
{!! $column['content'] ?? '' !!}
3+
</div>

src/Http/Livewire/LivewireDatatable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public function getViewColumns()
188188
return collect($column)->only([
189189
'hidden',
190190
'label',
191+
'content',
191192
'align',
192193
'type',
193194
'filterable',
@@ -290,7 +291,7 @@ public function getSelectStatements($withAlias = false, $export = false)
290291
{
291292
return $this->processedColumns->columns
292293
->reject(function ($column) use ($export) {
293-
return $column->scope || ($export && $column->preventExport);
294+
return $column->scope || $column->type === 'label' || ($export && $column->preventExport);
294295
})->map(function ($column) {
295296
if ($column->select) {
296297
return $column;

src/LabelColumn.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Mediconesystems\LivewireDatatables;
4+
5+
/**
6+
* Use this column to simply display a custom header ("label") and a fixed content ("content").
7+
*
8+
* @example (new LabelColumn())->label('foo')->content('bar'),
9+
*/
10+
class LabelColumn extends Column
11+
{
12+
public $type = 'label';
13+
14+
public $content = '';
15+
16+
/**
17+
* Which fixed string should always be displayed in every row of this column?
18+
*/
19+
public function content($content)
20+
{
21+
$this->content = $content;
22+
23+
return $this;
24+
}
25+
}

0 commit comments

Comments
 (0)