Skip to content

Commit 7275492

Browse files
author
Herbert Maschke
committed
introduce label column
1 parent ecd2d29 commit 7275492

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
@@ -150,7 +149,11 @@ class ComplexDemoTable extends LivewireDatatable
150149
DateColumn::name('dob')
151150
->label('DOB')
152151
->filterable()
153-
->hide()
152+
->hide(),
153+
154+
(new LabelColumn())
155+
->label('My custom heading')
156+
->content('This fixed string appears in every row')
154157
];
155158
}
156159
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@
100100
<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" />
101101
</div>
102102
</div>
103+
@elseif($column['type'] === 'label')
104+
<div class="table-cell overflow-hidden align-top">
105+
{{ $column['label'] ?? '' }}
106+
</div>
103107
@else
104108
<div class="table-cell overflow-hidden align-top">
105109
@isset($column['filterable'])
@@ -127,6 +131,8 @@
127131
@endif
128132
@elseif($column['type'] === 'checkbox')
129133
@include('datatables::checkbox', ['value' => $row->checkbox_attribute])
134+
@elseif($column['type'] === 'label')
135+
@include('datatables::label')
130136
@else
131137
<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) }}">
132138
{!! $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',
@@ -289,7 +290,7 @@ public function getSelectStatements($withAlias = false, $export = false)
289290
{
290291
return $this->processedColumns->columns
291292
->reject(function ($column) use ($export) {
292-
return $column->scope || ($export && $column->preventExport);
293+
return $column->scope || $column->type === 'label' || ($export && $column->preventExport);
293294
})->map(function ($column) {
294295
if ($column->select) {
295296
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)