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

Expose Laravel Loop Object To Column #4857

Merged
merged 25 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/tables/docs/03-columns/02-text.md
Expand Up @@ -133,6 +133,36 @@ TextColumn::make('status')->enum([
])
```

## Displaying the row index

You may want a column to contain the number of the current row in the table:

```php
use Filament\Tables\Columns\TextColumn;

TextColumn::make('index')->getStateUsing(static function (stdClass $rowLoop): string {
return (string) $rowLoop->iteration;
});
```

As `$rowLoop` is Laravel's Blade `$loop` object, you can reference all other `$loop` properties.

As a shortcut, you may use the `rowIndex()` method:

```php
use Filament\Tables\Columns\TextColumn;

TextColumn::make('index')->rowIndex()
```

To start counting from 0 instead of 1, use `isFromZero: true`:

```php
use Filament\Tables\Columns\TextColumn;

TextColumn::make('index')->rowIndex(isFromZero: true)
```

## Custom formatting

You may instead pass a custom formatting callback to `formatStateUsing()`, which accepts the `$state` of the cell, and optionally its `$record`:
Expand Down
Expand Up @@ -19,5 +19,6 @@
:components="$getComponents()"
:record="$getRecord()"
:record-key="$recordKey"
:row-loop="$getRowLoop()"
/>
</div>
Expand Up @@ -25,5 +25,6 @@
:components="$getComponents()"
:record="$getRecord()"
:record-key="$recordKey"
:row-loop="$getRowLoop()"
/>
</div>
Expand Up @@ -2,6 +2,7 @@
'components',
'record',
'recordKey' => null,
'rowLoop' => null,
])

@php
Expand Down Expand Up @@ -33,6 +34,7 @@
@foreach ($components as $layoutComponent)
@php
$layoutComponent->record($record);
$layoutComponent->rowLoop($rowLoop);

$isColumn = $layoutComponent instanceof \Filament\Tables\Columns\Column;
@endphp
Expand Down
4 changes: 4 additions & 0 deletions packages/tables/resources/views/index.blade.php
Expand Up @@ -507,6 +507,7 @@ class="flex-1 block py-3"
:components="$getColumnsLayout()"
:record="$record"
:record-key="$recordKey"
:row-loop="$loop"
/>
</a>
@elseif ($recordAction)
Expand All @@ -530,6 +531,7 @@ class="flex-1 block py-3"
:components="$getColumnsLayout()"
:record="$record"
:record-key="$recordKey"
:row-loop="$loop"
/>
</button>
@else
Expand All @@ -538,6 +540,7 @@ class="flex-1 block py-3"
:components="$getColumnsLayout()"
:record="$record"
:record-key="$recordKey"
:row-loop="$loop"
/>
</div>
@endif
Expand Down Expand Up @@ -774,6 +777,7 @@ class="filament-tables-record-checkbox"
@foreach ($columns as $column)
@php
$column->record($record);
$column->rowLoop($loop->parent);
@endphp

<x-tables::cell
Expand Down
2 changes: 2 additions & 0 deletions packages/tables/src/Columns/Column.php
Expand Up @@ -25,6 +25,7 @@ class Column extends ViewComponent
use Concerns\HasAlignment;
use Concerns\HasExtraHeaderAttributes;
use Concerns\HasLabel;
use Concerns\HasRowLoopObject;
use Concerns\HasName;
use Concerns\HasRecord;
use Concerns\HasState;
Expand Down Expand Up @@ -55,6 +56,7 @@ protected function getDefaultEvaluationParameters(): array
return array_merge(parent::getDefaultEvaluationParameters(), [
'livewire' => $this->getLivewire(),
'record' => $this->getRecord(),
'rowLoop' => $this->getRowLoop(),
]);
}
}
22 changes: 22 additions & 0 deletions packages/tables/src/Columns/Concerns/HasRowLoopObject.php
@@ -0,0 +1,22 @@
<?php

namespace Filament\Tables\Columns\Concerns;

use stdClass;

trait HasRowLoopObject
{
protected ?stdClass $loop = null;

public function rowLoop(?stdClass $loop): static
{
$this->loop = $loop;

return $this;
}

public function getRowLoop(): ?stdClass
{
return $this->loop;
}
}
3 changes: 3 additions & 0 deletions packages/tables/src/Columns/Layout/Component.php
Expand Up @@ -12,6 +12,7 @@
use Filament\Tables\Columns\Concerns\CanGrow;
use Filament\Tables\Columns\Concerns\CanSpanColumns;
use Filament\Tables\Columns\Concerns\HasRecord;
use Filament\Tables\Columns\Concerns\HasRowLoopObject;
use Illuminate\Support\Traits\Conditionable;

class Component extends ViewComponent
Expand All @@ -22,6 +23,7 @@ class Component extends ViewComponent
use CanSpanColumns;
use CanGrow;
use HasRecord;
use HasRowLoopObject;
use Conditionable;
use HasExtraAttributes;

Expand Down Expand Up @@ -88,6 +90,7 @@ protected function getDefaultEvaluationParameters(): array
return array_merge(parent::getDefaultEvaluationParameters(), [
'livewire' => $this->getLivewire(),
'record' => $this->getRecord(),
'rowLoop' => $this->getRowLoop(),
]);
}
}
10 changes: 10 additions & 0 deletions packages/tables/src/Columns/TextColumn.php
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Tables\Columns;

use Closure;
use stdClass;

class TextColumn extends Column
{
Expand All @@ -18,6 +19,15 @@ class TextColumn extends Column

protected bool | Closure $canWrap = false;

public function rowIndex(bool $isFromZero = false): static
{
$this->getStateUsing(static function (stdClass $rowLoop) use ($isFromZero): string {
return (string) $rowLoop->{$isFromZero ? 'index' : 'iteration'};
});

return $this;
}

public function wrap(bool | Closure $condition = true): static
{
$this->canWrap = $condition;
Expand Down