Skip to content

Commit

Permalink
search for something that is not on the current page
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Nov 4, 2021
1 parent e2d550b commit 84397c5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
25 changes: 12 additions & 13 deletions src/Helpers/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public function filter(): Builder
private function validateInputTextOptions(string $field): bool
{
return isset($this->filters['input_text_options'][$field]) && in_array(
strtolower($this->filters['input_text_options'][$field]),
['is', 'is_not', 'contains', 'contains_not', 'starts_with', 'ends_with']
);
strtolower($this->filters['input_text_options'][$field]),
['is', 'is_not', 'contains', 'contains_not', 'starts_with', 'ends_with']
);
}

/**
Expand Down Expand Up @@ -169,7 +169,7 @@ public function filterInputText(Builder $query, string $field, $value)
*/
public function filterBoolean($query, string $field, $value)
{
/** @var \Illuminate\Database\Eloquent\Builder $query */
/** @var Builder $query */
if ($value != 'all') {
$value = ($value == 'true');
$query->where($field, '=', $value);
Expand All @@ -183,7 +183,7 @@ public function filterBoolean($query, string $field, $value)
*/
public function filterSelect($query, string $field, $value)
{
/** @var \Illuminate\Database\Eloquent\Builder $query */
/** @var Builder $query */
if (filled($value)) {
$query->where($field, $value);
}
Expand All @@ -196,7 +196,7 @@ public function filterSelect($query, string $field, $value)
*/
public function filterMultiSelect($query, string $field, $value)
{
$empty = false;
$empty = false;
$values = collect($value)->get('values');
if (count($values) === 0) {
return;
Expand All @@ -220,13 +220,13 @@ public function filterNumber($query, string $field, $value)
{
if (isset($value['start']) && !isset($value['end'])) {
$start = str_replace($value['thousands'], '', $value['start']);
$start = (float) str_replace($value['decimal'], '.', $start);
$start = (float)str_replace($value['decimal'], '.', $start);

$query->where($field, '>=', $start);
}
if (!isset($value['start']) && isset($value['end'])) {
$end = str_replace($value['thousands'], '', $value['end']);
$end = (float) str_replace($value['decimal'], '.', $end);
$end = (float)str_replace($value['decimal'], '.', $end);

$query->where($field, '<=', $end);
}
Expand All @@ -247,11 +247,10 @@ public function filterContains(): Model
$this->query = $this->query->where(function (Builder $query) {
/** @var Column $column */
foreach ($this->columns as $column) {
if ($column->searchable) {
$hasColumn = Schema::hasColumn($query->getModel()->getTable(), $column->field);
if ($hasColumn) {
$query->orWhere($column->field, 'like', '%' . $this->search . '%');
}
$hasColumn = Schema::hasColumn($query->getModel()->getTable(), $column->field);

if ($column->searchable && $hasColumn) {
$query->orWhere($column->field, 'like', '%' . $this->search . '%');
}
}

Expand Down
37 changes: 21 additions & 16 deletions src/PowerGridComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PowerComponents\LivewirePowerGrid;

use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection as BaseCollection;
use Livewire\{Component, WithPagination};
use PowerComponents\LivewirePowerGrid\Helpers\{Collection, Model};
Expand Down Expand Up @@ -48,11 +49,11 @@ class PowerGridComponent extends Component

protected $listeners = [
'eventChangeDatePiker' => 'eventChangeDatePiker',
'eventInputChanged' => 'eventInputChanged',
'eventToggleChanged' => 'eventInputChanged',
'eventMultiSelect' => 'eventMultiSelect',
'eventRefresh' => '$refresh',
'eventToggleColumn' => 'toggleColumn',
'eventInputChanged' => 'eventInputChanged',
'eventToggleChanged' => 'eventInputChanged',
'eventMultiSelect' => 'eventMultiSelect',
'eventRefresh' => '$refresh',
'eventToggleColumn' => 'toggleColumn',
];

public bool $toggleColumns = false;
Expand Down Expand Up @@ -132,7 +133,7 @@ public function showToggleColumns(): PowerGridComponent
*/
public function showCheckBox(string $attribute = 'id'): PowerGridComponent
{
$this->checkbox = true;
$this->checkbox = true;
$this->checkboxAttribute = $attribute;

return $this;
Expand All @@ -146,7 +147,7 @@ public function showPerPage(int $perPage = 10): PowerGridComponent
{
if (\Str::contains($perPage, $this->perPageValues)) {
$this->perPageInput = true;
$this->perPage = $perPage;
$this->perPage = $perPage;
}

return $this;
Expand All @@ -170,7 +171,7 @@ public function render()
$this->powerGridTheme = PowerGrid::theme($this->template() ?? powerGridTheme())->apply();

$this->columns = collect($this->columns)->map(function ($column) {
return (object) $column;
return (object)$column;
})->toArray();

$this->relationSearch = $this->relationSearch();
Expand All @@ -188,7 +189,7 @@ public function render()
private function renderView($data)
{
return view($this->powerGridTheme->layout->table, [
'data' => $data,
'data' => $data,
'theme' => $this->powerGridTheme,
'table' => 'livewire-powergrid::components.table',
]);
Expand All @@ -200,8 +201,6 @@ private function resolveModel($datasource = null)
return $this->datasource();
}

$this->total = $datasource->count();

return $datasource;
}

Expand Down Expand Up @@ -272,7 +271,7 @@ public function updateMessages(string $status, string $field = '_default_message
$updateMessages = [
'success' => [
'_default_message' => __('Data has been updated successfully!'),
'status' => __('Custom Field updated successfully!'),
'status' => __('Custom Field updated successfully!'),
],
'error' => [
'_default_message' => __('Error updating the data.'),
Expand Down Expand Up @@ -312,14 +311,18 @@ public function fillData()
$this->filtered = $results->pluck('id')->toArray();

$paginated = Collection::paginate($results, $this->perPage);
$results = $paginated->setCollection($this->transform($paginated->getCollection()));
$results = $paginated->setCollection($this->transform($paginated->getCollection()));
}

return $results;
}

if (filled($this->search)) {
$this->gotoPage(1);
}

$results = $this->resolveModel($datasource)
->where(function (\Illuminate\Database\Eloquent\Builder $query) {
->where(function (Builder $query) {
Model::query($query)
->setColumns($this->columns)
->setSearch($this->search)
Expand All @@ -336,6 +339,8 @@ public function fillData()
$results = $results->paginate($results->count());
}

$this->total = $results->total();

return $results->setCollection($this->transform($results->getCollection()));
}

Expand All @@ -357,7 +362,7 @@ private function transform($results)
|| is_a($this->addColumns(), PowerGridEloquent::class)
) {
return $results->transform(function ($row) {
$row = (object) $row;
$row = (object)$row;
$columns = $this->addColumns()->columns;
foreach ($columns as $key => $column) {
$row->{$key} = $column($row);
Expand All @@ -382,7 +387,7 @@ public function toggleColumn($field): void
$column['hidden'] = !data_get($column, 'hidden');
}

return (object) $column;
return (object)$column;
})->toArray();
}
}
7 changes: 7 additions & 0 deletions tests/Feature/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
->call('gotoPage', '11')
->assertSeeHtml('wire:click="previousPage"');

it('search for something that is not on the current page')
->livewire(DishesTable::class)
->assertSeeHtml('Francesinha vegana')
->call('gotoPage', 2)
->set('search', 'Francesinha vegana')
->assertSeeHtml('Francesinha vegana');

it('properly paginates', function () {
$component = powergrid();
$component->perPage = 5;
Expand Down

0 comments on commit 84397c5

Please sign in to comment.