When selecting filters in a list of records, the widget does not update
I have an invoice resource and I have added a widget to it to calculate the total of invoices. The widget needs to update according to the filters applied. I have tried this solution: https://filamentphp.com/community/how-to-refresh-widgets-when-table-actions-are-fired, but it still doesn't work. Widget totals not updating. Filament 2 version.
1 Reply
class InvoicesTotalization extends BaseWidget
{
protected static string $view = 'filament.resources.invoice-resource.widgets.invoices-totalization';
protected static ?string $pollingInterval = null;
protected $listeners = ['updateInvoicesTotalization' => '$refresh'];
public $tableFilters;
protected $queryString = ['tableFilters'];
protected function getCards(): array
{
$data = [];
$query = Invoice::query();
if ($this->tableFilters && isset($this->tableFilters['status'])) {
$query->where('status', $this->tableFilters['status']['value']);
}
if ($this->tableFilters && isset($this->tableFilters['street'])) {
$query->whereHas('Client', function($q){
$q->where('street', 'LIKE', '%'.$this->tableFilters['street']['street'].'%');
});
}
if ($this->tableFilters && isset($this->tableFilters['number'])) {
$query->whereHas('Client', function($q){
$q->where('address_number', $this->tableFilters['number']['number']);
});
}
if ($this->tableFilters && isset($this->tableFilters['invoice_date'])) {
$query->whereBetween('invoice_date', [$this->tableFilters['invoice_date']['start_date'], $this->tableFilters['invoice_date']['end_date']]);
}
$data['subtotal'] = $query->sum('subtotal');
$data['iva'] = $query->sum('iva');
$data['total'] = $query->sum('total');
return [
Card::make('SUBTOTAL: ', number_format($data['subtotal'], 2, ',', '.')),
Card::make('IVA: ', number_format($data['iva'], 2, ',', '.')),
Card::make('TOTAL: ', number_format($data['total'], 2, ',', '.')),
];
}
}
In ListInvoice, i have this: public function updated($name)
{
if (Str::of($name)->contains(['tableFilters'])) {
$this->emit('updateInvoicesTotalization');
}
}
anyone?