F
Filament11mo ago
TiBiBa

Possible to get record from the table row above current row?

For a custom table action I'm attempting to implement an action to copy the values from the selectColumn in the table row above the current one into the selectColumn of the current row. But I can't figure out how to get the current row index and/or the row above. Is this possible? Thanks in advance!
Solution:
Found a way to implement this: ``` private function copyAttributeAbove(string $attribute, Table $table, Task $record): void { $records = $table->getRecords();...
Jump to solution
1 Reply
Solution
TiBiBa
TiBiBa11mo ago
Found a way to implement this:
private function copyAttributeAbove(string $attribute, Table $table, Task $record): void
{
$records = $table->getRecords();
$currentRecordIndex = $records->search(function($tableRecord) use ($record) {
return $record->id === $tableRecord->id;
});

if ($currentRecordIndex == 0) {
return;
}

$previousRecord = $records->slice($currentRecordIndex-1, 1)->first();
$record->update([
$attribute => $previousRecord[$attribute],
]);
}
private function copyAttributeAbove(string $attribute, Table $table, Task $record): void
{
$records = $table->getRecords();
$currentRecordIndex = $records->search(function($tableRecord) use ($record) {
return $record->id === $tableRecord->id;
});

if ($currentRecordIndex == 0) {
return;
}

$previousRecord = $records->slice($currentRecordIndex-1, 1)->first();
$record->update([
$attribute => $previousRecord[$attribute],
]);
}
Want results from more Discord servers?
Add your server