LYiub
1 year ago
535
2
Laravel

Yajra DataTable (search box and loading data) response very slow around 15 sec. and more

Posted 1 year ago by LYiub

I'm using Yajra DataTable with Laravel. but the table loaded very small and take around 15 seconds and more. The reason is the for loop and if conditions inside addColumn, which make it slow.

I only know this way (by using addColumn) to embed html tags in the table. While I need to put select tag, buttons, and other links based on some conditions. where these tags make loading the table very slow.

This is my Ajax code:

var allUsersTable = $('.allleads').DataTable({
                processing: true,
                serverSide: true,
                ajax: {
                    url: "customer/GetAllcusts",
                },
                columns: [
                    {"data": "note", "name": "note", orderable: false, searchable: false},
                    {"data": "sms", "name": "sms"},
                    {"data": "assign", "name": "assign"},
                ],
                bSort: false,
                dom: 'Bfrtip',
                buttons: [
                     'excel', 'pdf', 'print', 'colvis'
                ]
            });

and herein the function in controller to load the data:

return DataTables::of($allClients)
            ->addColumn('note', function ($allClient) {
                return '<a style="cursor: pointer" title="Add Note" onclick="addNotes(' . $allClient->id . ',' . $allClient->quote_id . ',this)"><i class="material-icons">event_note </i></a>';
            })
            ->addColumn('sms', function ($allClient) {
                return '<a style="cursor: pointer" title="Send SMS" id="send_smsclick" onclick="send_smsclick(' . $allClient->id . ',' . $allClient->primary_number . ')" ><i class="material-icons notranslate">message</i></a>';
            })
->addColumn('assign', function ($allClient) use ($user_status, $all_users) {

                if ($user_status == 'Admin') {
                    $options = '<option hidden="true">Select Stuff...</option>';
                    foreach ($all_users as $users) {
                        if ($allClient->user_id == $users->id) {
                            $options = $options . '<option value = "' . $users->id . '" disabled selected >' . $users->name . '</option >';
                        } else {
                            $options = $options . '<option value = "' . $users->id . '" >' . $users->name . '</option>';
                        }
                    }

                    return '<select name="user_id" class="form-control activity_status"
                            onchange="assign_users(' . $allClient->id . ', this)" >' . $options . ' </select>';


                } else {
                    $query = User::find($allClient->user_id);
                    return $query->name;
                }

            })
            ->rawColumns(['note', 'sms', 'assign'])
            ->make(true);

I will appreciate any hep to improve the speed to load the table, and lunch the data as well.

Thank u

Please sign in or create an account to participate in this conversation.