Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

valentin_vranic's avatar

Livewire 3 render conditionally

Hi folks. I have a component where I list some data into table based on some filter elements (initially there is empty table). For each row there are multiple "details" option, where on click event, below another table is rendered. Now, my issue is, that on each click one of those "details" button, I have to re-render the first table based on filtered data to be able to show the table, and after to populate the other table below.

Either my other interruption is page with multiple (simple )forms not related to each other.

What I want to achieve is to somehow stop rendering the first already filtered table and only show the table below on "details" click.

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve the behavior you're describing in Livewire, where you want to conditionally render parts of your component without re-rendering the entire view, you can utilize Livewire's ability to manage component rendering selectively. Here's a step-by-step solution to manage the rendering of only the details table when a "details" button is clicked, without re-rendering the main table.

Step 1: Setup Your Component

First, ensure your Livewire component has the necessary state to track which details should be shown. You might have an array or a specific property to track the visible state of each details table.

class SomeComponent extends Component
{
    public $items;
    public $showDetailsIndex = null;

    public function mount()
    {
        $this->items = Item::all(); // Assuming you are fetching data from a model called Item
    }

    public function showDetails($index)
    {
        $this->showDetailsIndex = $index;
    }

    public function render()
    {
        return view('livewire.some-component');
    }
}

Step 2: Modify Your Blade Template

In your Blade template, you can conditionally render the details table based on the showDetailsIndex property. Use Livewire's wire:key to ensure proper DOM diffing by Livewire.

<table>
    @foreach ($items as $index => $item)
        <tr>
            <td>{{ $item->name }}</td>
            <td>
                <button wire:click="showDetails({{ $index }})">Details</button>
            </td>
        </tr>
        @if ($showDetailsIndex === $index)
            <tr wire:key="details-{{ $index }}">
                <td colspan="2">
                    <!-- Render your details table here -->
                    <table>
                        <!-- Details data -->
                    </table>
                </td>
            </tr>
        @endif
    @endforeach
</table>

Step 3: Optimize Rendering

To prevent the main table from re-rendering when details are shown, ensure that your Livewire component's render function does not depend on the showDetailsIndex. This setup, as shown, should work as the showDetails method only changes showDetailsIndex, which only affects the rendering of the conditional part of the Blade template.

Conclusion

This setup allows you to maintain the state of the main table while independently controlling the rendering of the details table. Each click on the "Details" button updates only the part of the component that needs to change, which is the visibility of the details table. This approach leverages Livewire's reactive properties efficiently and should meet your requirements as described.

Please or to participate in this conversation.