This commit is contained in:
Dan Harrin
2024-07-09 09:13:26 +01:00
parent 8e781a79b1
commit b5fcfb3e6a
3 changed files with 26 additions and 6 deletions
+17 -5
View File
@@ -332,18 +332,30 @@ TextColumn::make('name')
The callback has access to the database `$query` builder instance to perform calculations with. It should return the value to display in the table.
## Hiding the summary
## Conditionally hiding the summary
To hide a summary conditionally, you may use the `hidden()` method:
To hide a summary, you may pass a boolean, or a function that returns a boolean, to the `hidden()` method. If you need it, you can access the Eloquent query builder instance for that summarizer via the `$query` argument of the function:
```php
use Filament\Tables\Columns\Summarizers\Summarizer;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Builder;
TextColumn::make('name')
TextColumn::make('sku')
->summarize(Summarizer::make()
->hidden(fn(Builder $query): bool => $query->count() == 0))
->hidden(fn (Builder $query): bool => ! $query->exists()))
```
Alternatively, you can use the `visible()` method to achieve the opposite effect:
```php
use Filament\Tables\Columns\Summarizers\Summarizer;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Builder;
TextColumn::make('sku')
->summarize(Summarizer::make()
->visible(fn (Builder $query): bool => $query->exists()))
```
## Summarising groups of rows
@@ -352,7 +364,7 @@ You can use summaries with [groups](grouping) to display a summary of the record
### Hiding the grouped rows and showing the summary only
You may hide the rows inside groups and just show the summary of each group using the `groupsOnly()` method. This is very useful in many reporting scenarios.
You may hide the rows inside groups and just show the summary of each group using the `groupsOnly()` method. This is beneficial in many reporting scenarios.
```php
use Filament\Tables\Columns\Summarizers\Sum;
@@ -17,9 +17,16 @@ trait CanBeHidden
return $this;
}
public function visible(bool | Closure $condition = true): static
{
$this->isVisible = $condition;
return $this;
}
public function isHidden(): bool
{
if ($this->evaluate($this->isHidden, ['query' => $this->query])) {
if ($this->evaluate($this->isHidden)) {
return true;
}
@@ -170,6 +170,7 @@ class Summarizer extends ViewComponent
return match ($parameterName) {
'livewire' => [$this->getLivewire()],
'table' => [$this->getTable()],
'query' => [$this->getQuery()],
default => parent::resolveDefaultClosureDependencyForEvaluationByName($parameterName),
};
}