Hey guys,
I have a table of users which are paginated and each user will have an ‘edit’ button which displays a modal. What are the best practices for including this? Here is a minimal example.
<table class="table">
<tr>
<td>Name</td>
<td>Actions</td>
</tr>
@foreach($users as $key => $user)
<tr id="{{ $user->id }}">
<td>
{{ $user->name }}
</td>
<td>
<a wire:click="$emit('showModal')">Edit</a>
</td>
</tr>
@endforeach
@livewire('user-modal', $user)
This kinda works and the modal opens however I get the following error in my console when I open the modal, I have tried specifying the wire:id as well as a key.
I tried adding the following on the table row as the troubleshooting docs but it seemed to break it.
wire:key="{{ $loop->index }}"
Code for the modal:
public $user;
public $isOpen = false;
protected $listeners = [
'closeModal',
'showModal' => 'open',
];
public function open()
{
$this->isOpen = true;
}
public function close()
{
$this->isOpen = false;
}
public function mount(User $user)
{
$this->user = $user;
}
public function render()
{
return view('livewire.user-modal', [
'user' => $this->user
]);
}
The blade file:
<div class="modal {{ $isOpen ? 'd-block' : '' }}">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ $user->name }}</h5>
<button class="close" wire:click="$emit('close')">
<span>×</span>
</button>
</div>
</div>
</div>
</div>
Any ideas?