F
Filament10mo ago
Hasith

conditionally render widget stat?

How can I show hide stat using a condition? I want to show the first stat only when $this->productCount > 0.
protected function getStats(): array
{
return [

Stat::make('Processed', '192.1k')
->color('success')
->extraAttributes([
'class' => 'cursor-pointer',
'wire:click' => "\$dispatch('setStatusFilter', { filter: 'processed' })",
]),

Stat::make('Product Count', $this->productCount)
->chart([7, 2, 10, 3, 15, 4, 17])
->description('Current product count')
->icon('heroicon-m-banknotes')
->color('success'),
];
}
protected function getStats(): array
{
return [

Stat::make('Processed', '192.1k')
->color('success')
->extraAttributes([
'class' => 'cursor-pointer',
'wire:click' => "\$dispatch('setStatusFilter', { filter: 'processed' })",
]),

Stat::make('Product Count', $this->productCount)
->chart([7, 2, 10, 3, 15, 4, 17])
->description('Current product count')
->icon('heroicon-m-banknotes')
->color('success'),
];
}
5 Replies
Hasith
Hasith10mo ago
i can use extraAttributes() and add class => 'hidden'. But how can i add that using a condition ?
Hugo
Hugo10mo ago
you can do something like that :
$statsCards = [];

if ($this->productCount > 0) {
$statsCards[] = Stat::make('Processed', '192.1k')
->color('success')
->extraAttributes([
'class' => 'cursor-pointer',
'wire:click' => "\$dispatch('setStatusFilter', { filter: 'processed' })",
]);
}

$statsCards[] = Stat::make('Product Count', $this->productCount)
->chart([7, 2, 10, 3, 15, 4, 17])
->description('Current product count')
->icon('heroicon-m-banknotes')
->color('success'),

return $statsCards;
$statsCards = [];

if ($this->productCount > 0) {
$statsCards[] = Stat::make('Processed', '192.1k')
->color('success')
->extraAttributes([
'class' => 'cursor-pointer',
'wire:click' => "\$dispatch('setStatusFilter', { filter: 'processed' })",
]);
}

$statsCards[] = Stat::make('Product Count', $this->productCount)
->chart([7, 2, 10, 3, 15, 4, 17])
->description('Current product count')
->icon('heroicon-m-banknotes')
->color('success'),

return $statsCards;
Hasith
Hasith10mo ago
Thanks will try this
Hugo
Hugo10mo ago
Work ?
Hasith
Hasith10mo ago
I created a brand new widget. Did not tried this one. creating new widget was easy. anyway thank you ❤️ @hugomyb