如何在 Livewire 中发出事件时触发加载效果?
How to trigger a loading effect when an event is emitted in Livewire?
我有一个向数据库添加新条目的模式。将其添加到数据库后,我使用发出的事件更新 table (HTML),该事件刷新包含 table.
的组件
不过,我想给 table 添加一个加载状态。我试过了,但是在请求过程中没有触发。
我怎样才能做到这一点?
如果需要,我可以使用 AlpineJS。
$this->emit('newTeamInvite');
<?php
namespace App\Http\Livewire\Team;
use Illuminate\Support\Collection;
use Livewire\Component;
class TeamInvites extends Component
{
public Collection $invites;
protected $listeners = ['newTeamInvite' => '$refresh'];
public function render()
{
$this->invites = auth()->user()->company->team_invites->reverse();
return view('livewire.team.team-invites');
}
}
<div>
<div class="flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
<div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
{{ __('Name')}}
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
{{ __('Number of branches')}}
</th>
<th scope="col" class="relative px-6 py-3">
<span class="sr-only">{{ __('Cancel')}}</span>
</th>
</tr>
</thead>
<tbody>
@foreach($invites as $invite)
<tr class="@if($loop->even) bg-gray-50 @else bg-white @endif">
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
{{ $invite->email }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{ $invite->number_of_branches }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
<a href="#" class="link">{{ __('Cancel')}}</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div wire:loading>
Loading data...
</div>
</div>
我不确定此时加载状态是否可以针对已触发的事件...我的意思是,如果可能的话。文档指的是整个加载组件行为或目标模型、属性、操作……但不是事件。
当它通过 livewire 钩子触发事件并做一些事情时,你可以管理什么。例如:
<div class="hidden" id="loading-container">
Loading data...
</div>
<script>
Livewire.hook('message.sent', (message,component) => {
if (message.updateQueue[0].payload.event === 'newTeamInvite') {
$('#loading-container').removeClass('hidden');
}
});
Livewire.hook('message.processed', (message,component) => {
if (message.updateQueue[0].payload.event === 'newTeamInvite') {
$('#loading-container').addClass('hidden');
}
});
</script>
我有一个向数据库添加新条目的模式。将其添加到数据库后,我使用发出的事件更新 table (HTML),该事件刷新包含 table.
的组件不过,我想给 table 添加一个加载状态。我试过了,但是在请求过程中没有触发。
我怎样才能做到这一点?
如果需要,我可以使用 AlpineJS。
$this->emit('newTeamInvite');
<?php
namespace App\Http\Livewire\Team;
use Illuminate\Support\Collection;
use Livewire\Component;
class TeamInvites extends Component
{
public Collection $invites;
protected $listeners = ['newTeamInvite' => '$refresh'];
public function render()
{
$this->invites = auth()->user()->company->team_invites->reverse();
return view('livewire.team.team-invites');
}
}
<div>
<div class="flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
<div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
{{ __('Name')}}
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
{{ __('Number of branches')}}
</th>
<th scope="col" class="relative px-6 py-3">
<span class="sr-only">{{ __('Cancel')}}</span>
</th>
</tr>
</thead>
<tbody>
@foreach($invites as $invite)
<tr class="@if($loop->even) bg-gray-50 @else bg-white @endif">
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
{{ $invite->email }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{ $invite->number_of_branches }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
<a href="#" class="link">{{ __('Cancel')}}</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div wire:loading>
Loading data...
</div>
</div>
我不确定此时加载状态是否可以针对已触发的事件...我的意思是,如果可能的话。文档指的是整个加载组件行为或目标模型、属性、操作……但不是事件。 当它通过 livewire 钩子触发事件并做一些事情时,你可以管理什么。例如:
<div class="hidden" id="loading-container">
Loading data...
</div>
<script>
Livewire.hook('message.sent', (message,component) => {
if (message.updateQueue[0].payload.event === 'newTeamInvite') {
$('#loading-container').removeClass('hidden');
}
});
Livewire.hook('message.processed', (message,component) => {
if (message.updateQueue[0].payload.event === 'newTeamInvite') {
$('#loading-container').addClass('hidden');
}
});
</script>