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

Prawin's avatar
Level 12

Custom livewire datatable component is unable to send request when a row gets checked

I have tried to integrate custom livewire datatable into my project. When I check an item, it is not being marked with background color in real time. Moreover, I do not see the action button when an item is checked. It only works when I move to the next page with pagination link. I think my datatable livewire component is unable to send request when the item gets checked. (datatable component is inside another livewire component-ManageRoles.

Source code on github: (https://github.com/pmsj/play-school/blob/Role-and-Permissions/resources/views/livewire/authorization/role/manage-role.blade.php)

0 likes
2 replies
LaryAI's avatar
Level 58

To address the issue with your Livewire datatable component not updating in real-time when a row is checked, you need to ensure that the Livewire component is properly handling the state changes and that the frontend is correctly reflecting these changes. Here are some steps and code examples to help you troubleshoot and resolve the issue:

  1. Ensure Proper Event Binding: Make sure that the checkbox change event is properly bound to a Livewire method. This method should update the component's state.

  2. Livewire Method for Handling Checkbox: Create a method in your Livewire component to handle the checkbox state change. This method should update the state and trigger any necessary UI updates.

    // In your Livewire component
    public $selectedItems = [];
    
    public function toggleItem($itemId)
    {
        if (in_array($itemId, $this->selectedItems)) {
            $this->selectedItems = array_diff($this->selectedItems, [$itemId]);
        } else {
            $this->selectedItems[] = $itemId;
        }
    }
    
  3. Blade Template for Checkbox: Ensure that your Blade template is set up to call the Livewire method when a checkbox is toggled.

    <!-- In your Blade view -->
    <table>
        @foreach($items as $item)
            <tr class="{{ in_array($item->id, $selectedItems) ? 'bg-selected' : '' }}">
                <td>
                    <input type="checkbox" wire:click="toggleItem({{ $item->id }})" {{ in_array($item->id, $selectedItems) ? 'checked' : '' }}>
                </td>
                <td>{{ $item->name }}</td>
            </tr>
        @endforeach
    </table>
    
  4. CSS for Selected Rows: Add CSS to highlight the selected rows.

    <style>
        .bg-selected {
            background-color: #f0f0f0;
        }
    </style>
    
  5. Ensure Livewire is Loaded: Verify that Livewire scripts are correctly included in your layout. This is necessary for Livewire to function properly.

    @livewireStyles
    @livewireScripts
    
  6. Check for JavaScript Errors: Open your browser's developer console to check for any JavaScript errors that might be preventing Livewire from functioning correctly.

  7. Pagination Considerations: If pagination is involved, ensure that the state of selected items is maintained across pages. You might need to store the selected items in a session or a database if they need to persist beyond the current page.

By following these steps, you should be able to ensure that your Livewire datatable component updates in real-time when a row is checked. If the issue persists, consider checking the Livewire documentation for any additional configuration or troubleshooting tips.

Prawin's avatar
Level 12

app.js file:


import {Livewire, Alpine } from '../../vendor/livewire/livewire/dist/livewire.esm'
import humanDate from './directives/humanDate';


Alpine.directive('human-date', humanDate) //custom humanDate Directive // to update time on comment

Livewire.start()```



Initialization in main layout file (at the end of body tag):

  @livewireScriptConfig

Please or to participate in this conversation.