Skip to content

Show and hide Blade table rows with Alpine JS #1978

Answered by SimoTod
howellmartinez asked this question in 1. Help
Discussion options

You must be logged in to vote

It's possible but div is not a syntactically valid child of thetable tag and a lot of browsers will move it out of table 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 many tbody you want in a table, the purpose of that tag is to group table rows).

<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>

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@shailesh1984
Comment options

@ekwoka
Comment options

Answer selected by howellmartinez
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants