DataTables Lazy Loading using PHP Laravel

There are times when reading data from the DataTables is simply too slow, particularly when dealing with thousands of data rows. To address this slowness of data, DataTables’ server-side processing feature provides a method to let all the “heavy loading” be done by a database engine on the server-side 

Simple query to get data from server

 $listing = DB::table('table_name')->get();
//Records around 50K

Pass your query result to DataTable object in PHP Laravel

return DataTables::of($listing)->make();

HTML DataTable

<table class="YOURTABLE mdl-data-table table-striped table-hover dataTable" cellspacing="0" width="100%" role="grid" style="width: 100%;">
<thead>
<tr>
<th>First</th>
<th>Second</th>
<th>Third</th>
<th>Forth</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

Configuration

Lazy Loading in DataTables is enabled through use of the serverSide option. Simply set it to true and DataTables will operate in server-side processing mode. In JS file–

var url = '/fetch-list';
    $('.YOURTABLE').DataTable({
        processing: true,
        serverSide: true,
        destroy: true,
        ajax: url,
        dom: 'Bfrtip',
        "columnDefs": [
            { "searchable": false, "targets": 6 }
        ],
        buttons: [
            'pageLength', 'csv', 'excel', 'pdf', 'print'
        ],
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, 100]],
        "pagingType": "full_numbers",
        "columns": [
            {"data": "FIRST"},
            {"data": "SECOND"},
            {"data": "THIRD"},
            {"data": "FOURTH"}
]
    });

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.