F
Filament15mo 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
HasithOP15mo ago
i can use extraAttributes() and add class => 'hidden'. But how can i add that using a condition ?
Hugo
Hugo15mo 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
HasithOP15mo ago
Thanks will try this
Hugo
Hugo15mo ago
Work ?
Hasith
HasithOP15mo ago
I created a brand new widget. Did not tried this one. creating new widget was easy. anyway thank you ❤️ @hugomyb
Want results from more Discord servers?
Add your server