Skip to content

Commit 85304e2

Browse files
authored
Added a Datetime Column (#543)
* Create DatetimeColumn.php * Create datetime.blade.php * Added DatetimeFilters to LivewireDatatable * Changed doDateFilterStart and doDateFilterEnd to doDatetimeFilterStart and doDatetimeFilterEnd on filter * Fix the issue detected by ci, and created a new config named "default_datetime_format"
1 parent be96bed commit 85304e2

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

config/livewire-datatables.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
'default_time_format' => 'H:i',
1515
'default_date_format' => 'd/m/Y',
16+
'default_datetime_format' => 'd/m/Y H:i',
1617

1718
/*
1819
|--------------------------------------------------------------------------
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<div x-data class="flex flex-col">
2+
<div class="w-full relative flex">
3+
<input x-ref="start" class="w-full pr-8 m-1 text-sm leading-4 block rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50" type="datetime-local"
4+
wire:change="doDatetimeFilterStart('{{ $index }}', $event.target.value)" style="padding-bottom: 5px"
5+
value="{{ $this->activeDatetimeFilters[$index]['start'] ?? ''}}"
6+
/>
7+
<div class="absolute inset-y-0 right-0 pr-2 flex items-center">
8+
<button x-on:click="$refs.start.value=''" wire:click="doDatetimeFilterStart('{{ $index }}', '')" class="-mb-0.5 pr-1 flex text-gray-400 hover:text-red-600 focus:outline-none" tabindex="-1">
9+
<x-datatables.icons.x-circle class="h-5 w-5 stroke-current" />
10+
</button>
11+
</div>
12+
</div>
13+
<div class="w-full relative flex items-center">
14+
<input x-ref="end" class="w-full pr-8 m-1 text-sm leading-4 block rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50" type="datetime-local"
15+
wire:change="doDatetimeFilterEnd('{{ $index }}', $event.target.value)" style="padding-bottom: 5px"
16+
value="{{ $this->activeDatetimeFilters[$index]['end'] ?? ''}}"
17+
/>
18+
<div class="absolute inset-y-0 right-0 pr-2 flex items-center">
19+
<button x-on:click="$refs.end.value=''" wire:click="doDatetimeFilterEnd('{{ $index }}', '')" class="-mb-0.5 pr-1 flex text-gray-400 hover:text-red-600 focus:outline-none" tabindex="-1">
20+
<x-datatables.icons.x-circle class="h-5 w-5 stroke-current" />
21+
</button>
22+
</div>
23+
</div>
24+
</div>

src/DatetimeColumn.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+
use Illuminate\Support\Carbon;
6+
7+
class DatetimeColumn extends Column
8+
{
9+
public $type = 'datetime';
10+
public $callback;
11+
12+
public function __construct()
13+
{
14+
$this->format();
15+
}
16+
17+
public function format($format = null)
18+
{
19+
$this->callback = function ($value) use ($format) {
20+
return $value ? Carbon::parse($value)->format($format ?? config('livewire-datatables.default_datetime_format')) : null;
21+
};
22+
23+
return $this;
24+
}
25+
}

src/Http/Livewire/LivewireDatatable.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class LivewireDatatable extends Component
3131
public $sort;
3232
public $direction;
3333
public $activeDateFilters = [];
34+
public $activeDatetimeFilters = [];
3435
public $activeTimeFilters = [];
3536
public $activeSelectFilters = [];
3637
public $activeBooleanFilters = [];
@@ -170,6 +171,7 @@ public function applyToTable($options)
170171
'search',
171172
'activeSelectFilters',
172173
'activeDateFilters',
174+
'activeDatetimeFilters',
173175
'activeTimeFilters',
174176
'activeBooleanFilters',
175177
'activeTextFilters',
@@ -197,6 +199,7 @@ public function resetTable()
197199
$this->setPage(1);
198200
$this->activeSelectFilters = [];
199201
$this->activeDateFilters = [];
202+
$this->activeDatetimeFilters = [];
200203
$this->activeTimeFilters = [];
201204
$this->activeTextFilters = [];
202205
$this->activeBooleanFilters = [];
@@ -531,6 +534,7 @@ public function setSessionStoredFilters()
531534
'boolean' => $this->activeBooleanFilters,
532535
'select' => $this->activeSelectFilters,
533536
'date' => $this->activeDateFilters,
537+
'datetime' => $this->activeDatetimeFilters,
534538
'time' => $this->activeTimeFilters,
535539
'number' => $this->activeNumberFilters,
536540
'search' => $this->search,
@@ -638,6 +642,10 @@ public function initialiseDefaultFilters()
638642
$this->activeDateFilters[$columnIndex] = $value;
639643
}
640644

645+
if ($column['type'] === 'datetime') {
646+
$this->activeDatetimeFilters[$columnIndex] = $value;
647+
}
648+
641649
if ($column['type'] === 'time') {
642650
$this->activeTimeFilters[$columnIndex] = $value;
643651
}
@@ -672,6 +680,10 @@ public function initialiseFilters()
672680
$this->activeDateFilters = $filters['date'];
673681
}
674682

683+
if (! empty($filters['datetime'])) {
684+
$this->activeDatetimeFilters = $filters['datetime'];
685+
}
686+
675687
if (! empty($filters['time'])) {
676688
$this->activeTimeFilters = $filters['time'];
677689
}
@@ -923,6 +935,22 @@ public function doDateFilterEnd($index, $end)
923935
$this->setSessionStoredFilters();
924936
}
925937

938+
public function doDatetimeFilterStart($index, $start)
939+
{
940+
$this->activeDatetimeFilters[$index]['start'] = $start;
941+
$this->setVisibleSelected();
942+
$this->setPage(1);
943+
$this->setSessionStoredFilters();
944+
}
945+
946+
public function doDatetimeFilterEnd($index, $end)
947+
{
948+
$this->activeDatetimeFilters[$index]['end'] = $end;
949+
$this->setVisibleSelected();
950+
$this->setPage(1);
951+
$this->setSessionStoredFilters();
952+
}
953+
926954
public function doTimeFilterStart($index, $start)
927955
{
928956
$this->activeTimeFilters[$index]['start'] = $start;
@@ -980,6 +1008,7 @@ public function removeSelectFilter($column, $key = null)
9801008
public function clearAllFilters()
9811009
{
9821010
$this->activeDateFilters = [];
1011+
$this->activeDatetimeFilters = [];
9831012
$this->activeTimeFilters = [];
9841013
$this->activeSelectFilters = [];
9851014
$this->activeBooleanFilters = [];
@@ -1154,6 +1183,7 @@ public function getNumberFiltersProperty()
11541183
public function getActiveFiltersProperty()
11551184
{
11561185
return count($this->activeDateFilters)
1186+
|| count($this->activeDatetimeFilters)
11571187
|| count($this->activeTimeFilters)
11581188
|| count($this->activeSelectFilters)
11591189
|| count($this->activeBooleanFilters)
@@ -1209,6 +1239,7 @@ public function buildDatabaseQuery($export = false)
12091239
->addTextFilters()
12101240
->addNumberFilters()
12111241
->addDateRangeFilter()
1242+
->addDatetimeRangeFilter()
12121243
->addTimeRangeFilter()
12131244
->addComplexQuery()
12141245
->addSort();
@@ -1504,6 +1535,27 @@ public function addDateRangeFilter()
15041535
return $this;
15051536
}
15061537

1538+
public function addDatetimeRangeFilter()
1539+
{
1540+
if (! count($this->activeDatetimeFilters)) {
1541+
return $this;
1542+
}
1543+
1544+
$this->query->where(function ($query) {
1545+
foreach ($this->activeDatetimeFilters as $index => $filter) {
1546+
if (! ((isset($filter['start']) && $filter['start'] != '') || (isset($filter['end']) && $filter['end'] != ''))) {
1547+
break;
1548+
}
1549+
$query->whereBetween($this->getColumnFilterStatement($index)[0], [
1550+
isset($filter['start']) && $filter['start'] != '' ? $filter['start'] : config('livewire-datatables.default_time_start', '0000-00-00 00:00'),
1551+
isset($filter['end']) && $filter['end'] != '' ? $filter['end'] : config('livewire-datatables.default_time_end', '9999-12-31 23:59'),
1552+
]);
1553+
}
1554+
});
1555+
1556+
return $this;
1557+
}
1558+
15071559
public function addTimeRangeFilter()
15081560
{
15091561
if (! count($this->activeTimeFilters)) {

0 commit comments

Comments
 (0)