Make table update when variable changes

Custom page with a livewire component that has a table needs to update the table when a variable changes.

There are more parts to the issue, but it boils down to the code below, when changeVar is called from livewire component the var changes (can be seen on the page), but the table header does not.
If I click the button once more, the table updates - so the table is always lagging 1 event.

<?php

namespace App\Livewire;

use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use Illuminate\Contracts\View\View;
use Livewire\Component;
use Filament\Tables\Columns\ViewColumn;

class TableTest extends Component implements HasForms, HasTable
{
    use InteractsWithTable;
    use InteractsWithForms;

    public $var = "test";

    function changeVar()
    {
        $this->var = "changed";
    }

    public function table(Table $table): Table
    {
        return $table
            ->query(\App\Models\Apartment::where('name', '!=', 'O')->where('name', '!=', 'P'))
            ->columns([
                TextColumn::make('name')->label($this->var),
            ])
            ->filters([
                // ...
            ])
            ->actions([
                // ...
            ])
            ->bulkActions([
                // ...
            ])->paginated(false);
    }

    public function render()
    {
        return view('livewire.table-test');
    }
}
`
Solution
So seems emit was renamed to dispatch in v3, I got the refresh working with the below code, thanks for the help.


class TableTest extends Component implements HasForms, HasTable
{
    use InteractsWithTable;
    use InteractsWithForms;

    public $var = "test";

    protected $listeners = ['refreshTableTest' => '$refresh'];

    public function table(Table $table): Table
    {
        return $table
            ->query(\App\Models\Apartment::where('name', '!=', 'O')->where('name', '!=', 'P'))
            ->columns([
                TextColumn::make('name')->label($this->var),
            ])
            ->filters([])
            ->actions([])
            ->bulkActions([])
            ->headerActions([
                Action::make('HeaderTest')
                ->action(function($livewire){ $this->var = "headerAction"; $livewire->dispatch('refreshTableTest'); })
            ])
            ->paginated(false);
    }

    public function render()
    {
        return view('livewire.table-test');
    }
}
Was this page helpful?