Show and hide Blade table rows with Alpine JS #1978
-
I am using Blade with Laravel 8.x and would like to show or hide table rows using <table>
@foreach($items as $item)
<div x-data="{show: false}">
<tr>
<td>
<button type="button" x-on:click="show = !show">
Toggle Show
</button>
</td>
</tr>
<tr x-show="show">
<td>{{ $item->name }}</td>
</tr>
</div>
@endforeach
</table> When I do this however, I get a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It's possible but <table>
@foreach($items as $item)
<tbody x-data="{show: false}">
<tr>
<td>
<button type="button" x-on:click="show = !show">
Toggle Show
</button>
</td>
</tr>
<tr x-show="show">
<td>{{ $item->name }}</td>
</tr>
</tbody>
@endforeach
</table> |
Beta Was this translation helpful? Give feedback.
It's possible but
div
is not a syntactically valid child of thetable
tag and a lot of browsers will move it out oftable
trying to "fix" your HTML. At that point, Alpine will start behaving in a weird way because the DOM is not the one it expects.Try using
tbody
instead (you can have as manytbody
you want in a table, the purpose of that tag is to group table rows).