diff --git a/resources/views/livewire/datatables/datatable.blade.php b/resources/views/livewire/datatables/datatable.blade.php
index be0b94ea..a7c2e628 100644
--- a/resources/views/livewire/datatables/datatable.blade.php
+++ b/resources/views/livewire/datatables/datatable.blade.php
@@ -109,7 +109,7 @@ class="px-3 py-2 text-xs font-medium tracking-wider text-green-500 uppercase bg-
@unless($column['hidden'])
- {{ count($selected) }}
+ {{ count($visibleSelected) }}
@endunless
diff --git a/resources/views/livewire/datatables/filters/checkbox.blade.php b/resources/views/livewire/datatables/filters/checkbox.blade.php
index 15dcd6ab..87f5a871 100644
--- a/resources/views/livewire/datatables/filters/checkbox.blade.php
+++ b/resources/views/livewire/datatables/filters/checkbox.blade.php
@@ -7,7 +7,7 @@ class="flex flex-col items-center h-full px-6 py-5 overflow-hidden text-xs font-
type="checkbox"
wire:click="toggleSelectAll"
class="w-4 h-4 mt-1 text-blue-600 form-checkbox transition duration-150 ease-in-out"
- @if(count($selected) === $this->results->total()) checked @endif
+ @if($this->results->total() === count($visibleSelected)) checked @endif
/>
diff --git a/src/Http/Livewire/LivewireDatatable.php b/src/Http/Livewire/LivewireDatatable.php
index 49b13989..38289dc4 100644
--- a/src/Http/Livewire/LivewireDatatable.php
+++ b/src/Http/Livewire/LivewireDatatable.php
@@ -65,6 +65,7 @@ class LivewireDatatable extends Component
public $persistSort = true;
public $persistPerPage = true;
public $persistFilters = true;
+ public $visibleSelected = [];
public $row = 1;
public $tablePrefix = '';
@@ -217,6 +218,7 @@ public function resetHiddenColumns()
public function updatedSearch()
{
+ $this->visibleSelected = ($this->search) ? array_intersect($this->getQuery()->get()->pluck('checkbox_attribute')->toArray(), $this->selected) : $this->selected;
$this->setPage(1);
}
@@ -704,21 +706,21 @@ public function getSortString($dbtable)
return Str::before($column['select'], ' AS ');
break;
- default:
-
- switch ($dbtable) {
- case 'mysql':
- return new Expression('`' . $column['name'] . '`');
- break;
- case 'pgsql':
- return new Expression('"' . $column['name'] . '"');
- break;
- case 'sqlsrv':
- return new Expression("'" . $column['name'] . "'");
- break;
- default:
- return new Expression("'" . $column['name'] . "'");
- }
+ default:
+
+ switch ($dbtable) {
+ case 'mysql':
+ return new Expression('`' . $column['name'] . '`');
+ break;
+ case 'pgsql':
+ return new Expression('"' . $column['name'] . '"');
+ break;
+ case 'sqlsrv':
+ return new Expression("'" . $column['name'] . "'");
+ break;
+ default:
+ return new Expression("'" . $column['name'] . "'");
+ }
}
}
@@ -870,6 +872,7 @@ public function isGroupHidden($group)
public function doBooleanFilter($index, $value)
{
$this->activeBooleanFilters[$index] = $value;
+ $this->setVisibleSelected();
$this->setPage(1);
$this->setSessionStoredFilters();
}
@@ -877,6 +880,7 @@ public function doBooleanFilter($index, $value)
public function doSelectFilter($index, $value)
{
$this->activeSelectFilters[$index][] = $value;
+ $this->setVisibleSelected();
$this->setPage(1);
$this->setSessionStoredFilters();
}
@@ -886,7 +890,7 @@ public function doTextFilter($index, $value)
foreach (explode(' ', $value) as $val) {
$this->activeTextFilters[$index][] = $val;
}
-
+ $this->setVisibleSelected();
$this->setPage(1);
$this->setSessionStoredFilters();
}
@@ -894,6 +898,7 @@ public function doTextFilter($index, $value)
public function doDateFilterStart($index, $start)
{
$this->activeDateFilters[$index]['start'] = $start;
+ $this->setVisibleSelected();
$this->setPage(1);
$this->setSessionStoredFilters();
}
@@ -901,6 +906,7 @@ public function doDateFilterStart($index, $start)
public function doDateFilterEnd($index, $end)
{
$this->activeDateFilters[$index]['end'] = $end;
+ $this->setVisibleSelected();
$this->setPage(1);
$this->setSessionStoredFilters();
}
@@ -908,6 +914,7 @@ public function doDateFilterEnd($index, $end)
public function doTimeFilterStart($index, $start)
{
$this->activeTimeFilters[$index]['start'] = $start;
+ $this->setVisibleSelected();
$this->setPage(1);
$this->setSessionStoredFilters();
}
@@ -915,6 +922,7 @@ public function doTimeFilterStart($index, $start)
public function doTimeFilterEnd($index, $end)
{
$this->activeTimeFilters[$index]['end'] = $end;
+ $this->setVisibleSelected();
$this->setPage(1);
$this->setSessionStoredFilters();
}
@@ -923,6 +931,7 @@ public function doNumberFilterStart($index, $start)
{
$this->activeNumberFilters[$index]['start'] = ($start != '') ? (int) $start : null;
$this->clearEmptyNumberFilter($index);
+ $this->setVisibleSelected();
$this->setPage(1);
$this->setSessionStoredFilters();
}
@@ -931,6 +940,7 @@ public function doNumberFilterEnd($index, $end)
{
$this->activeNumberFilters[$index]['end'] = ($end != '') ? (int) $end : null;
$this->clearEmptyNumberFilter($index);
+ $this->setVisibleSelected();
$this->setPage(1);
$this->setSessionStoredFilters();
}
@@ -947,6 +957,7 @@ public function clearEmptyNumberFilter($index)
public function removeSelectFilter($column, $key = null)
{
unset($this->activeSelectFilters[$column][$key]);
+ $this->visibleSelected = $this->selected;
if (count($this->activeSelectFilters[$column]) < 1) {
unset($this->activeSelectFilters[$column]);
}
@@ -964,6 +975,7 @@ public function clearAllFilters()
$this->activeNumberFilters = [];
$this->complexQuery = null;
$this->userFilter = null;
+ $this->visibleSelected = $this->selected;
$this->setPage(1);
$this->setSessionStoredFilters();
@@ -973,6 +985,7 @@ public function clearAllFilters()
public function removeBooleanFilter($column)
{
unset($this->activeBooleanFilters[$column]);
+ $this->visibleSelected = $this->selected;
$this->setSessionStoredFilters();
}
@@ -986,12 +999,14 @@ public function removeTextFilter($column, $key = null)
} else {
unset($this->activeTextFilters[$column]);
}
+ $this->visibleSelected = $this->selected;
$this->setSessionStoredFilters();
}
public function removeNumberFilter($column)
{
unset($this->activeNumberFilters[$column]);
+ $this->visibleSelected = $this->selected;
$this->setSessionStoredFilters();
}
@@ -1684,14 +1699,38 @@ public function checkboxQuery()
public function toggleSelectAll()
{
- if (count($this->selected) === $this->getQuery()->getCountForPagination()) {
- $this->selected = [];
+ $visible_checkboxes = $this->getQuery()->get()->pluck('checkbox_attribute')->toArray();
+ $visible_checkboxes = array_map('strval', $visible_checkboxes);
+ if ($this->searchOrFilterActive()) {
+ if (count($this->visibleSelected) === count($visible_checkboxes)) {
+ $this->selected = array_values(array_diff($this->selected, $visible_checkboxes));
+ $this->visibleSelected = [];
+ } else {
+ $this->selected = array_unique(array_merge($this->selected, $visible_checkboxes));
+ sort($this->selected);
+ $this->visibleSelected = $visible_checkboxes;
+ }
} else {
- $this->selected = $this->checkboxQuery()->values()->toArray();
+ if (count($this->selected) === $this->getQuery()->getCountForPagination()) {
+ $this->selected = [];
+ } else {
+ $this->selected = $this->checkboxQuery()->values()->toArray();
+ }
+ $this->visibleSelected = $this->selected;
}
+
$this->forgetComputed();
}
+ public function updatedSelected()
+ {
+ if ($this->searchOrFilterActive()) {
+ $this->setVisibleSelected();
+ } else {
+ $this->visibleSelected = $this->selected;
+ }
+ }
+
public function rowIsSelected($row)
{
return isset($row->checkbox_attribute) && in_array($row->checkbox_attribute, $this->selected);
@@ -1802,4 +1841,15 @@ public function massActionOptionHandler()
$this->massActionOption = null;
}
+
+ private function searchOrFilterActive()
+ {
+ return ! empty($this->search) || $this->getActiveFiltersProperty();
+ }
+
+ private function setVisibleSelected()
+ {
+ $this->visibleSelected = array_intersect($this->getQuery()->get()->pluck('checkbox_attribute')->toArray(), $this->selected);
+ $this->visibleSelected = array_map('strval', $this->visibleSelected);
+ }
}