laravel 如何在从另一个组件发出事件后使用Livewire重新呈现表

juzqafwq  于 2023-01-27  发布在  其他
关注(0)|答案(2)|浏览(117)

我是Livewire的新手,我遇到了这个问题。
我用livewire创建了一个 table.blade.php 组件,还有另一个searchbar. blade. php组件,它不是table组件的子组件,每次搜索一个词,table都应该用seached参数重新呈现。
一切正常,搜索查询给出了正确的结果(客户端具有分页功能),但不知何故,表没有重新呈现html。
知道我哪里做错了吗?谢谢

<div>
 <input type="text" wire:model="query" autofocus>
</div>


class SearchBar extends Component
{
    public $query;

    public function updatedQuery()
    {
        $this->emit('searchForQuotes', $this->query);
    }

    public function render()
    {
        return view('livewire.clients.searchbar');
    }
}


<div>
  <table>
    <tbody>
    @foreach($clients as $client)
       @livewire('clients.row', ['client' => $client], key($client->id))
    @endforeach
   </tody>
 </table>
</div>

class Table extends Component
{
    use WithPagination;

    public $query;

    protected $listeners = [
        'searchForQuotes' => 'render'
    ];

    public function mount()
    {
        $this->resetData();
    }

    public function resetData()
    {
        $this->query = null;
    }

    public function render($query = null)
    {
        $q = Client::query();

        if ($query) {
          $q->whereRaw("CONCAT(surname, ' ', name) LIKE '%" . $query . "%'");
        }

        $clients = $q->latest()->paginate(20);

        return view('livewire.clients.inc.table', [
            'clients' => $clients, 'query' => $query
        ]);
    }
}
uubf1zoe

uubf1zoe1#

试试这样的方法:

class Table extends Component
{
use WithPagination;

public $query;

protected $listeners = ['searchForQuotes'];

public function mount()
{
    $this->resetData();
}

public function searchForQuotes($query)
{
    $this->query = $query; 
    // Do something
    $this->render();
}

public function resetData()
{
    $this->query = null;
}

public function render()
{
    $q = Client::query();

    if ($this->query) {
      $q->whereRaw("CONCAT(surname, ' ', name) LIKE '%" . $query . "%'");
    }

    $clients = $q->latest()->paginate(20);

    return view('livewire.clients.inc.table', [
        'clients' => $clients, 'query' => $this->query
    ]);
}
}
nwo49xxi

nwo49xxi2#

我想我找到问题了,但不知道如何解决它.我的table.blade.php组件我得到了以下代码.

@foreach($clients as $client)
   @livewire('clients.row', ['client' => $client], key($client->id))
@endforeach

似乎嵌套组件在激发事件后未呈现。

相关问题