DEV Community

Agik Setiawan
Agik Setiawan

Posted on

3 2

Use Pure Datatable in Laravel Livewire with AlpineJS

Datatable is most popular Jquery plugin for make table easy with simple code.

<div x-data="app()" x-init="init()">
    <table class="table table-bordered" id="tableExample">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

<script>
    function app() {
        return {
            dataTable(newData = "") {
                const data = newData !== '' ? newData : {!! json_encode($users) !!};
                $('#tableExample').DataTable({
                    data: data,
                    columns: [
                        {
                            data: 'name'
                        },
                        {
                            data: 'email'
                        },
                    ],
                    destroy: true,
                    language: {
                        searchPlaceholder: 'Search...',
                        scrollX: "100%",
                        sSearch: '',
                    }
                });
            },
            init() {
                const _this = this;
                _this.dataTable();
                Livewire.on("dataChanged", function(data) {
                    _this.dataTable(data);
                });
            }
        }
    }
</script>
Enter fullscreen mode Exit fullscreen mode

you can changed new data from livewire function event with dataChanged function for retreive new data.

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs