Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter overrides to column searchable and sortable #234

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 20 additions & 4 deletions packages/tables/src/Columns/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ class Column

protected $isSearchable = false;

protected $searchUsing = false;

protected $isSortable = false;

protected $sortUsing = false;

protected $label;

protected $name;
Expand Down Expand Up @@ -179,11 +183,21 @@ public function isSearchable()
return $this->isSearchable && $this->getValueUsing === null;
}

public function getSearchUsing()
{
return $this->searchUsing;
}

public function isSortable()
{
return $this->isSortable && $this->getValueUsing === null;
}

public function getSortUsing()
{
return $this->sortUsing;
}

public function label($label)
{
$this->configure(function () use ($label) {
Expand Down Expand Up @@ -253,19 +267,21 @@ public function renderCell($record)
]));
}

public function searchable()
public function searchable($searchUsing = false)
{
$this->configure(function () {
$this->configure(function () use ($searchUsing) {
$this->isSearchable = true;
$this->searchUsing = $searchUsing;
});

return $this;
}

public function sortable()
public function sortable($sortUsing = false)
{
$this->configure(function () {
$this->configure(function () use ($sortUsing) {
$this->isSortable = true;
$this->sortUsing = $sortUsing;
});

return $this;
Expand Down
60 changes: 42 additions & 18 deletions packages/tables/src/HasTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,34 @@ public function getRecords()
][$query->getConnection()->getDriverName()] ?? 'like';

$first = $index === 0;

if (Str::of($column->getName())->contains('.')) {
$relationship = (string) Str::of($column->getName())->beforeLast('.');

$query = $query->{$first ? 'whereHas' : 'orWhereHas'}(
$relationship,
function ($query) use ($column, $search, $searchOperator) {
$columnName = (string) Str::of($column->getName())->afterLast('.');

return $query->where($columnName, $searchOperator, "%{$search}%");
},
if ($searchUsing = $column->getSearchUsing()) {
collect($searchUsing)
->each(function ($fieldName, &$index) use (&$query, &$first, $search, $searchOperator) {
$first = $index === 0;
$query = $query->{$first ? 'where' : 'orWhere'}(
fn ($query) => $query->where($fieldName, $searchOperator, "%{$search}%"),
);
});
} else {
if (Str::of($column->getName())->contains('.')) {
$relationship = (string) Str::of($column->getName())->beforeLast('.');

$query = $query->{$first ? 'whereHas' : 'orWhereHas'}(
$relationship,
function ($query) use ($column, $search, $searchOperator) {
$columnName = (string) Str::of($column->getName())->afterLast('.');

return $query->where($columnName, $searchOperator, "%{$search}%");
},
);

return;
}

$query = $query->{$first ? 'where' : 'orWhere'}(
fn ($query) => $query->where($column->getName(), $searchOperator, "%{$search}%"),
);

return;
}

$query = $query->{$first ? 'where' : 'orWhere'}(
fn ($query) => $query->where($column->getName(), $searchOperator, "%{$search}%"),
);
});
}

Expand All @@ -85,7 +94,14 @@ function ($query) use ($column, $search, $searchOperator) {
},
]);
} else {
$query = $query->orderBy($this->sortColumn, $this->sortDirection);
if ($sortUsingColumn = $this->sortByColumnSortUsing()) {
collect($sortUsingColumn->getSortUsing())
->each(function ($fieldName) use (&$query) {
return $query = $query->orderBy($fieldName, $this->sortDirection);
});
} else {
$query = $query->orderBy($this->sortColumn, $this->sortDirection);
}
}
}

Expand All @@ -103,6 +119,14 @@ public function setPage($page)
$this->selected = [];
}

protected function sortByColumnSortUsing()
{
return collect($this->getTable()->getColumns())
->first(function ($column) {
return $this->sortColumn == $column->getName() && $column->getSortUsing();
});
}

public function sortBy($column)
{
if ($this->sortColumn === $column) {
Expand Down