Home > Enterprise >  Laravel 8 Yajra DataTable refresh on the same page
Laravel 8 Yajra DataTable refresh on the same page

Time:10-09

How can I refresh a DataTable in the current page number without going back to the first page? This code refresh the table but keeps going to the first page or the first link of the pagination.

$('#table_users').DataTable().ajax.reload();

This is how the table is initialized.

        $(document).ready(function() {
            // init datatable.
            var dataTable = $('#table_users').DataTable({
                processing: true,
                serverSide: true,
                autoWidth: false,
                pageLength: 10,
                // scrollX: true,
                "order": [[ 0, "desc" ]],
                ajax: {
                    url: '{{ route('get_users') }}'
                },
                columns: [
                    {data: 'name', name: 'name'},
                    {data: 'email', name: 'email'},
                    {data: 'sex', name: 'sex'},
                    {data: 'bday', name: 'bday'},
                    {data: 'contact', name: 'contact'},
                    {data: 'scope', name: 'scope'},
                    {data: 'roles', name: 'roles'},
                    {data: 'permissions', name: 'permissions'},
                ]
            });
        });

The controller.

    $user = User::select('id', 'name', 'email', 'sex', 'bday', 'contact', 'scope', 'scope_id')->get();
    return \DataTables::of($user)->make()

CodePudding user response:

Replace datatable reload with this,

dataTable.ajax.reload(null, false)

Where the dataTable is an instance of created datatable and set first param as null which is callback and second param is for reset paging so set it as false.

Let me know its solve your issue or not.

Reference

  • Related