Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,21 @@ somewhere in your CSS
### Provide a datasource by declaring public property ```$model``` **OR** public method ```builder()``` that returns an instance of ```Illuminate\Database\Eloquent\Builder```
> ```php artisan livewire:datatable users-table --model=user``` --> 'app/Http/Livewire/UsersTable.php' with ```public $model = User::class```


### Declare a public method ```columns``` that returns an array containing one or more ```Mediconesystems\LivewireDatatables\Column```


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


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

```php
Expand Down Expand Up @@ -150,7 +149,11 @@ class ComplexDemoTable extends LivewireDatatable
DateColumn::name('dob')
->label('DOB')
->filterable()
->hide()
->hide(),

(new LabelColumn())
->label('My custom heading')
->content('This fixed string appears in every row')
];
}
}
Expand Down
6 changes: 6 additions & 0 deletions resources/views/livewire/datatables/datatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
<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" />
</div>
</div>
@elseif($column['type'] === 'label')
<div class="table-cell overflow-hidden align-top">
{{ $column['label'] ?? '' }}
</div>
@else
<div class="table-cell overflow-hidden align-top">
@isset($column['filterable'])
Expand Down Expand Up @@ -127,6 +131,8 @@
@endif
@elseif($column['type'] === 'checkbox')
@include('datatables::checkbox', ['value' => $row->checkbox_attribute])
@elseif($column['type'] === 'label')
@include('datatables::label')
@else
<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) }}">
{!! $row->{$column['name']} !!}
Expand Down
3 changes: 3 additions & 0 deletions resources/views/livewire/datatables/label.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<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) }}">
{!! $column['content'] ?? '' !!}
</div>
3 changes: 2 additions & 1 deletion src/Http/Livewire/LivewireDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public function getViewColumns()
return collect($column)->only([
'hidden',
'label',
'content',
'align',
'type',
'filterable',
Expand Down Expand Up @@ -289,7 +290,7 @@ public function getSelectStatements($withAlias = false, $export = false)
{
return $this->processedColumns->columns
->reject(function ($column) use ($export) {
return $column->scope || ($export && $column->preventExport);
return $column->scope || $column->type === 'label' || ($export && $column->preventExport);
})->map(function ($column) {
if ($column->select) {
return $column;
Expand Down
25 changes: 25 additions & 0 deletions src/LabelColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Mediconesystems\LivewireDatatables;

/**
* Use this column to simply display a custom header ("label") and a fixed content ("content").
*
* @example (new LabelColumn())->label('foo')->content('bar'),
*/
class LabelColumn extends Column
{
public $type = 'label';

public $content = '';

/**
* Which fixed string should always be displayed in every row of this column?
*/
public function content($content)
{
$this->content = $content;

return $this;
}
}