Skip to content

Commit

Permalink
Merge pull request #4857 from Nuhel/Expose-Laravel-Loop-Object-To-Column
Browse files Browse the repository at this point in the history
Expose Laravel Loop Object To Column
  • Loading branch information
danharrin committed Nov 9, 2022
2 parents e68ed05 + dc7cc2b commit 7f42837
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/tables/docs/03-columns/02-text.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
:components="$getComponents()"
:record="$getRecord()"
:record-key="$recordKey"
:row-loop="$getRowLoop()"
/>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
:components="$getComponents()"
:record="$getRecord()"
:record-key="$recordKey"
:row-loop="$getRowLoop()"
/>
</div>
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 7f42837

Please sign in to comment.