yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.75k stars 861 forks source link

Laravel paginate() with DataTables #3134

Closed Onepamopa closed 4 months ago

Onepamopa commented 5 months ago

Hello, so... Since executing the query "normally" is too expensive in terms of resources, I'm trying to limit using ->paginate(xx):

    public function getManufacturers(Request $request)
    {
        $manufacturers = Manufacturer::active()->paginate(10);

        return DataTables::of($manufacturers)
            ->editColumn('checkbox', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.checkbox', compact('manufacturer'));
            })
            ->editColumn('id', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.id', compact('manufacturer'));
            })
            ->editColumn('custom_id', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.custom_id', compact('manufacturer'));
            })
            ->editColumn('image', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.image', compact('manufacturer'));
            })
            ->editColumn('name', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.name', compact('manufacturer'));
            })
            ->editColumn('phone', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.phone', compact('manufacturer'));
            })
            ->editColumn('email', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.email', compact('manufacturer'));
            })
            ->editColumn('country', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.country', compact('manufacturer'));
            })
            ->editColumn('products_count', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.products_count', compact('manufacturer'));
            })
            ->addColumn('option', function ($manufacturer) {
                return view('admin.partials.actions.manufacturer.options', compact('manufacturer'));
            })
            ->rawColumns(['checkbox', 'id', 'custom_id', 'image', 'name', 'phone', 'email', 'country', 'products_count', 'option'])
            ->make(true);
    }

This is the datatable:
    let manufacturersTblColumns = {
        "aaSorting": [],
        "iDisplayLength": 10,
        "processing": true,
        "serverSide": true,
        "columns": [
            {
                'data': 'checkbox',
                'name': 'checkbox',
                'orderable': false,
                'searchable': false,
                'exportable': false,
                'printable': false
            },
            {
                'data': 'id',
                'name': 'id',
                'orderable': true,
                'searchable': false
            },
            {
                'data': 'custom_id',
                'name': 'custom_id',
                'orderable': true,
                'searchable': false
            },
            {
                'data': 'image',
                'name': 'image',
                'orderable': false,
                'searchable': false
            },
            {
                'data': 'name',
                'name': 'name',
                'orderable': false,
                'searchable': true
            },
            {
                'data': 'phone',
                'name': 'phone',
                'orderable': false,
                'searchable': true
            },
            {
                'data': 'email',
                'name': 'email',
                'orderable': false,
                'searchable': true
            },
            {
                'data': 'country',
                'name': 'country',
                'orderable': false,
                'searchable': false
            },
            {
                'data': 'products_count',
                'name': 'products_count',
                'orderable': true,
                'searchable': false
            },
            {
                'data': 'option',
                'name': 'option',
                'orderable': false,
                'searchable': false,
                'exportable': false,
                'printable': false
            }
        ],
        "initComplete": function (settings, json) {
            // console.log(json);
        },
        "drawCallback": function (settings) {
            $(".massAction, .checkbox-toggle").unbind();
            $(".fa", '.checkbox-toggle').removeClass("fa-check-square-o").addClass('fa-square-o');
            initMassActions();
        },
        "oLanguage": {
            "sInfo": "_START_ {{trans('app.dt_to')}} _END_ {{trans('app.dt_of')}} _TOTAL_ {{trans('app.dt_entries')}}",
            "sInfoEmpty": "0 {{trans('app.dt_to')}} 0 {{trans('app.dt_of')}} 0 {{trans('app.dt_entries')}}",
            "sLengthMenu": "{{trans('app.dt_show')}} _MENU_",
            "buttons": {
                "sLengthMenu": "{{trans('app.dt_show')}} _MENU_",
                "pageLength": {
                    _: "{{trans('app.dt_show')}} %d {{trans('app.dt_rows')}}"
                },
            },
            "sSearch": "",
            "sEmptyTable": "{{ trans('app.no_data_found') }}",
            "oPaginate": {
                "sNext": '<i class="fa fa-hand-o-right"></i>',
                "sPrevious": '<i class="fa fa-hand-o-left"></i>',
            },
        },
        "aoColumnDefs": [{
            "bSortable": false,
            "aTargets": [-1]
        }],
        "lengthMenu": [
            [10, 20, 50, 100],
            ['10 {{trans('app.dt_rows')}}', '20 {{trans('app.dt_rows')}}', '50 {{trans('app.dt_rows')}}', '100 {{trans('app.dt_rows')}}']
        ],
        "dom": 'Bfrtip',
        "buttons": [
            'pageLength'
        ]
    };

        // Load manufacturers table
        $('#manufacturers_table').DataTable($.extend({}, manufacturersTblColumns, {
            "ajax": {
                url: "{{ route('admin.catalog.manufacturer.getMore') }}",
                type: "GET",
                async: true,
                error: function (xhr, error, code) {
                    if (xhr.status === 401) {
                        window.location.href = "{{ route('admin.login') }}";
                    }
                }
            }
        }));

However, getting a "No available engine for Illuminate\Pagination\LengthAwarePaginator"

Any ideas on how to run this table with Laravle's paginate()? Or, if that's not possible - how to render it properly? There's images and so forth...

Laravel 8.83.27, laravel-datatables v. 9.21.2

yajra commented 5 months ago

Don't paginate as the package will handle it for you. Use query/eloquent builder too instead of collection if possible for better performance.

$manufacturers = Manufacturer::active();
github-actions[bot] commented 4 months ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 4 months ago

This issue was closed because it has been inactive for 7 days since being marked as stale.