Warrior
Custom summaries with value calculated in column
This is ListTenantRates.php
<?php
namespace App\Filament\Resources\TenantRateResource\Pages;
use App\Filament\Resources\TenantRateResource;
use App\Filament\Widgets\TenantRateTotal;
use Filament\Resources\Pages\ListRecords;
class ListTenantRates extends ListRecords
{
protected static string $resource = TenantRateResource::class;
public $totalCalculatedPrice = 0;
protected function getHeaderActions(): array
{
return [];
}
public function updateTotalCalculatedPrice($amount)
{
$this->totalCalculatedPrice += $amount;
}
public function getTotalCalculatedPrice()
{
/* To temporarily resolve this, I divided the total by 3 in the getTotalCalculatedPrice method. This workaround is necessary because the state is being duplicated three times, likely due to how the table rendering process manages state: */
return $this->totalCalculatedPrice / 3;
}
public function updatedTableFilters(): void
{
parent::updatedTableFilters();
$this->totalCalculatedPrice = 0;
}
}
<?php
namespace App\Filament\Resources\TenantRateResource\Pages;
use App\Filament\Resources\TenantRateResource;
use App\Filament\Widgets\TenantRateTotal;
use Filament\Resources\Pages\ListRecords;
class ListTenantRates extends ListRecords
{
protected static string $resource = TenantRateResource::class;
public $totalCalculatedPrice = 0;
protected function getHeaderActions(): array
{
return [];
}
public function updateTotalCalculatedPrice($amount)
{
$this->totalCalculatedPrice += $amount;
}
public function getTotalCalculatedPrice()
{
/* To temporarily resolve this, I divided the total by 3 in the getTotalCalculatedPrice method. This workaround is necessary because the state is being duplicated three times, likely due to how the table rendering process manages state: */
return $this->totalCalculatedPrice / 3;
}
public function updatedTableFilters(): void
{
parent::updatedTableFilters();
$this->totalCalculatedPrice = 0;
}
}
6 replies
Custom summaries with value calculated in column
This is TenantRateResource extends Resource, in the method
table(Table $table): Table
TextColumn::make('calculated_price')
->label('Subtotal')
->getStateUsing(function (TenantRate $record, HasTable $livewire) {
$filterFrom = $livewire->getTableFilterState('tenant')['from'] ?? Carbon::now()->startOfMonth();
$filterTo = $livewire->getTableFilterState('tenant')['to'] ?? Carbon::now()->endOfMonth();
$from = Carbon::parse($filterFrom);
$to = Carbon::parse($filterTo);
$tenantRateCalculator = new TenantRateCalculator($record->tenant);
$price = $tenantRateCalculator->calculate($from, $to);
$livewire->updateTotalCalculatedPrice($price);
return $price; // I need access this price in summarize
})
->summarize(
Summarizer::make()
->label('Total')
->using(function (TenantRate $record, HasTable $livewire) {
return $livewire->getTotalCalculatedPrice(); // This solution is temporary, here you must add the price
})
),
TextColumn::make('calculated_price')
->label('Subtotal')
->getStateUsing(function (TenantRate $record, HasTable $livewire) {
$filterFrom = $livewire->getTableFilterState('tenant')['from'] ?? Carbon::now()->startOfMonth();
$filterTo = $livewire->getTableFilterState('tenant')['to'] ?? Carbon::now()->endOfMonth();
$from = Carbon::parse($filterFrom);
$to = Carbon::parse($filterTo);
$tenantRateCalculator = new TenantRateCalculator($record->tenant);
$price = $tenantRateCalculator->calculate($from, $to);
$livewire->updateTotalCalculatedPrice($price);
return $price; // I need access this price in summarize
})
->summarize(
Summarizer::make()
->label('Total')
->using(function (TenantRate $record, HasTable $livewire) {
return $livewire->getTotalCalculatedPrice(); // This solution is temporary, here you must add the price
})
),
6 replies