Livewire multiple wire:click, first one works, rest do not

若如初见. 提交于 2021-02-05 11:22:06

问题


I am putting together a CRUD app using livewire and my issue is with wire:click on the different buttons. I have a create modal come up and the wire:click in that modal works perfectly. However the edit and delete buttons on the main page do not fire off when clicked.

The only way I can get the edit button to fire is if it is the only button with wire:click on the page.

These two buttons are inside a @foreach loop for each organization. This loop builds out the table

<button class="btn btn-sm btn-success" wire:target="edit('{{$org->id}}')">Edit</button>
<button class="btn btn-sm btn-danger" wire:click="delete({{$org->id}})">Delete</button>

How can I get these wire:clicks to fire off for each row in the table.

I do have @livewireScripts on my app layout


回答1:


To not confuse Livewire, you should use wire:key on elements in a loop. This also means that you should wrap you HTML in one parent <div>.

@foreach ($items as $org)
    <div wire:key="{{ $loop->index }}">
        <button class="btn btn-sm btn-success" wire:target="edit('{{$org->id}}')">Edit</button>
        <button class="btn btn-sm btn-danger" wire:click="delete({{$org->id}})">Delete</button>
    </div>
@endforeach
  • https://laravel-livewire.com/docs/2.x/troubleshooting



回答2:


Livewire uses a complex DOM diffing algorithm to intelligently swap components. So When you have loops make sure to add keys and wrap them in a parent container.

<div wire:key="{{$org->id}}">
        <button class="btn btn-sm btn-success" wire:target="edit('{{$org->id}}')">Edit</button>
        <button class="btn btn-sm btn-danger" wire:click="delete({{$org->id}})">Delete</button>
</div>


来源:https://stackoverflow.com/questions/64817742/livewire-multiple-wireclick-first-one-works-rest-do-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!