Skip to content

Commit b1b86e6

Browse files
authored
Merge pull request #397 from amvisor/pin-records
introduce ability to 'pin' selected records at the top to watch/compare them
2 parents 2cedad7 + 6453bf4 commit b1b86e6

File tree

7 files changed

+154
-255
lines changed

7 files changed

+154
-255
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,30 @@ public function getExportStylesProperty()
317317
}
318318
```
319319

320+
### Pin Records
321+
322+
If you want to give your users the ability to pin specific records to be able to, for example, compare
323+
them with each other, you can use the CanPinRecords trait. Ensure to have at least one Checkbox Column
324+
so the user can select records:
325+
326+
```php
327+
use Mediconesystems\LivewireDatatables\Traits\CanPinRecords;
328+
329+
class RecordTable extends LivewireDatatable
330+
{
331+
use CanPinRecords;
332+
333+
public $model = Record::class;
334+
335+
public function columns()
336+
{
337+
return [
338+
Column::checkbox(),
339+
340+
// ...
341+
342+
```
343+
320344
### Custom column names
321345
It is still possible to take full control over your table, you can define a ```builder``` method using whatever query you like, using your own joins, groups whatever, and then name your columns using your normal SQL syntax:
322346

@@ -346,7 +370,6 @@ public function columns()
346370

347371
```
348372

349-
350373
### Callbacks
351374
Callbacks give you the freedom to perform any mutations you like on the data before displaying in the table.
352375
- The callbacks are performed on the paginated results of the database query, so shouldn't use a ton of memory
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
<div class="flex justify-center">
2-
<input type="checkbox" wire:model="selected" value="{{ $value }}" class="form-checkbox mt-1 h-4 w-4 text-blue-600 transition duration-150 ease-in-out" />
2+
<input
3+
type="checkbox"
4+
wire:model="selected"
5+
value="{{ $value }}"
6+
@if (in_array($value, $this->pinnedRecords)) checked @endif
7+
class="w-4 h-4 mt-1 text-blue-600 form-checkbox transition duration-150 ease-in-out"
8+
/>
39
</div>

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,22 @@ class="px-3 py-2 text-xs font-medium tracking-wider text-green-500 uppercase bg-
121121
@endif
122122
@endforeach
123123
</div>
124-
125-
<div class="table-row bg-blue-100 divide-x divide-blue-200">
126-
@foreach($this->columns as $index => $column)
127-
@if($column['hidden'])
128-
@if($hideable === 'inline')
129-
<div class="table-cell w-5 overflow-hidden align-top bg-blue-100"></div>
130-
@endif
131-
@elseif($column['type'] === 'checkbox')
132-
<div
133-
@if (isset($column['tooltip']['text'])) title="{{ $column['tooltip']['text'] }}" @endif
134-
class="flex flex-col items-center h-full px-6 py-5 overflow-hidden text-xs font-medium tracking-wider text-left text-gray-500 uppercase align-top bg-blue-100 border-b border-gray-200 leading-4 space-y-2 focus:outline-none">
135-
<div>{{ __('SELECT ALL') }}</div>
136-
<div>
137-
<input type="checkbox" wire:click="toggleSelectAll" @if(count($selected) === $this->results->total()) checked @endif class="w-4 h-4 mt-1 text-blue-600 form-checkbox transition duration-150 ease-in-out" />
138-
</div>
139-
</div>
140-
@elseif($column['type'] === 'label')
141-
<div class="table-cell overflow-hidden align-top">
142-
{{ $column['label'] ?? '' }}
143-
</div>
144-
@else
145-
<div class="table-cell overflow-hidden align-top">
146-
@isset($column['filterable'])
124+
@endunless
125+
<div class="table-row bg-blue-100 divide-x divide-blue-200">
126+
@foreach($this->columns as $index => $column)
127+
@if($column['hidden'])
128+
@if($hideable === 'inline')
129+
<div class="table-cell w-5 overflow-hidden align-top bg-blue-100"></div>
130+
@endif
131+
@elseif($column['type'] === 'checkbox')
132+
@include('datatables::filters.checkbox')
133+
@elseif($column['type'] === 'label')
134+
<div class="table-cell overflow-hidden align-top">
135+
{{ $column['label'] ?? '' }}
136+
</div>
137+
@else
138+
<div class="table-cell overflow-hidden align-top">
139+
@isset($column['filterable'])
147140
@if( is_iterable($column['filterable']) )
148141
<div wire:key="{{ $index }}">
149142
@include('datatables::filters.select', ['index' => $index, 'name' => $column['label'], 'options' => $column['filterable']])
@@ -158,7 +151,6 @@ class="flex flex-col items-center h-full px-6 py-5 overflow-hidden text-xs font-
158151
@endif
159152
@endforeach
160153
</div>
161-
@endif
162154
@forelse($this->results as $row)
163155
<div class="table-row p-1 {{ $this->rowClasses($row, $loop) }}">
164156
@foreach($this->columns as $column)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div
2+
@if (isset($column['tooltip']['text'])) title="{{ $column['tooltip']['text'] }}" @endif
3+
class="flex flex-col items-center h-full px-6 py-5 overflow-hidden text-xs font-medium tracking-wider text-left text-gray-500 uppercase align-top bg-blue-100 border-b border-gray-200 leading-4 space-y-2 focus:outline-none">
4+
<div>{{ __('SELECT ALL') }}</div>
5+
<div>
6+
<input
7+
type="checkbox"
8+
wire:click="toggleSelectAll"
9+
class="w-4 h-4 mt-1 text-blue-600 form-checkbox transition duration-150 ease-in-out"
10+
@if(count($selected) === $this->results->total()) checked @endif
11+
/>
12+
</div>
13+
</div>

src/ColumnSet.php.old.ctp

Lines changed: 0 additions & 228 deletions
This file was deleted.

src/Http/Livewire/LivewireDatatable.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public function applyToTable($options)
151151
'activeNumberFilters',
152152
'hide',
153153
'selected',
154+
'pinnedRecords',
154155
] as $property) {
155156
if (isset($options[$property])) {
156157
$this->$property = $options[$property];
@@ -245,6 +246,10 @@ public function mount(
245246
$this->initialisePerPage();
246247
$this->initialiseColumnGroups();
247248
$this->model = $this->model ?: get_class($this->builder()->getModel());
249+
250+
if (isset($this->pinnedRecords)) {
251+
$this->initialisePinnedRecords();
252+
}
248253
}
249254

250255
// save settings
@@ -1180,6 +1185,10 @@ public function buildDatabaseQuery($export = false)
11801185
->addTimeRangeFilter()
11811186
->addComplexQuery()
11821187
->addSort();
1188+
1189+
if (isset($this->pinnedRecors)) {
1190+
$this->applyPinnedRecords();
1191+
}
11831192
}
11841193

11851194
public function complexQuery($rules)
@@ -1502,6 +1511,9 @@ public function addTimeRangeFilter()
15021511
public function addSort()
15031512
{
15041513
if (isset($this->sort) && isset($this->freshColumns[$this->sort]) && $this->freshColumns[$this->sort]['name']) {
1514+
if (isset($this->pinnedRecords) && $this->pinnedRecords) {
1515+
$this->query->orderBy(DB::raw('FIELD(id,' . implode(',', $this->pinnedRecords) . ')'), 'DESC');
1516+
}
15051517
$this->query->orderBy(DB::raw($this->getSortString()), $this->direction ? 'asc' : 'desc');
15061518
}
15071519

@@ -1793,6 +1805,5 @@ public function massActionOptionHandler()
17931805
}
17941806

17951807
$this->massActionOption = null;
1796-
$this->selected = [];
17971808
}
17981809
}

0 commit comments

Comments
 (0)