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

anonymouse703's avatar

how to re-initialize DataTable inside the Javascript hooks?

How to re-initialize the DataTable using Javascript hooks?

I tried this but have an error DataTables warning: table id=rolesTable - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

This is my script

document.addEventListener("DOMContentLoaded", () => {
        Livewire.hook('component.initialized', (component) => {
            $('#rolesTable').DataTable({
                "pagingType": "full_numbers",
            });
        })
        Livewire.hook('element.initialized', (el, component) => {
            $('#rolesTable').DataTable({
                "pagingType": "full_numbers",
            });
        })
        Livewire.hook('element.updating', (fromEl, toEl, component) => {
            $('#rolesTable').DataTable({
                "pagingType": "full_numbers",
            });
        })
        Livewire.hook('element.updated', (el, component) => {
            $('#rolesTable').DataTable({
                "pagingType": "full_numbers",
            });
        })
});
0 likes
1 reply
frankielee's avatar

http://datatables.net/tn/3 Check the link?

DataTables provides a destroy() method to destroy an old table, so you would be able to initialise a new one in its place.

Make sure the datatable is destroyed or haven't initialized yet.

Please or to participate in this conversation.