Last active
March 9, 2022 14:43
-
-
Save itzikbenh/5e99c957101b2b2837092c778cbd3ac0 to your computer and use it in GitHub Desktop.
Laravel server side processing for Datatables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Just so you can get the idea, your data might be different. | |
//Note that I changed the dataSrc to fit the data we return. | |
var table = $('table.tickets-table').DataTable({ | |
processing: true, | |
serverSide: true, | |
lengthChange: true, | |
"ajax": { | |
"url": '/tickets', | |
"dataSrc": "data.data" | |
}, | |
"columns": [ | |
{ "data": "id", "name": "id", | |
fnCreatedCell: function (nTd, sData, oData, iRow, iCol) { | |
$(nTd).html("<a href='/tickets/"+oData.id+"'>"+oData.id+"</a>"); | |
} | |
}, | |
{ "data": "client_name", "name": "client_name", | |
fnCreatedCell: function (nTd, sData, oData, iRow, iCol) { | |
console.log(oData); | |
if(oData.client_name) { | |
$(nTd).html("<a href='/clients/"+oData.client_id+"'>"+oData.client_name+"</a>"); | |
} | |
} | |
}, | |
{ "data": "location", "name": "location", "defaultContent": "" }, | |
{ "data": "priority_name", "name": "priority_name" }, | |
{ "data": "status_name", "name": "status_name" }, | |
{ "data": "date", "name": "date" }, | |
], | |
dom: '<Blf<t>ip>', | |
order: [ [0, 'desc'] ], | |
buttons: [ | |
'csv', | |
{ | |
extend: 'excelHtml5', | |
title: 'Data export' | |
}, | |
{ | |
extend: 'pdfHtml5', | |
title: 'Data export' | |
}, | |
], | |
pageLength: 25, | |
lengthMenu: [25, 50, 75, 100], | |
initComplete: function(settings, json) { | |
table.buttons().container().appendTo( '.colsm6:eq(0)', table.table().container() ); | |
$('table.tickets-table').show(); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Pagination\Paginator; | |
//This example is a bit more comlex since I have columns that are foreign keys of the Ticket table. | |
public function index(Request $request) | |
{ | |
if($request->ajax()) { | |
$columns = ['tickets.id', 'client_name', 'location', 'priority_name', 'status_name', 'date']; | |
$draw = $request->draw; | |
$start = $request->start; //Start is the offset | |
$length = $request->length; //How many records to show | |
$column = $request->order[0]['column']; //Column to orderBy | |
$dir = $request->order[0]['dir']; //Direction of orderBy | |
$searchValue = $request->search['value']; //Search value | |
//Sets the current page | |
Paginator::currentPageResolver(function () use ($start, $length) { | |
return ($start / $length + 1); | |
}); | |
$tickets = Ticket::whereHas('client', function($query) use ($searchValue) { | |
$query->where('name', 'like', '%'.$searchValue.'%'); | |
}) | |
->orWhereHas('location', function($query) use ($searchValue) { | |
$query->where('location', 'like', '%'.$searchValue.'%'); | |
}) | |
->orWhereHas('priority', function($query) use ($searchValue) { | |
$query->where('name', 'like', '%'.$searchValue.'%'); | |
}) | |
->orWhereHas('status', function($query) use ($searchValue) { | |
$query->where('name', 'like', '%'.$searchValue.'%'); | |
}) | |
->orWhere('tickets.id', 'like', '%'.$searchValue.'%') | |
->orWhere('date', 'like', '%'.str_replace('/', '-', $searchValue).'%') | |
->leftJoin('clients', 'tickets.client_id', '=', 'clients.id') | |
->leftJoin('locations', 'tickets.location_id', '=', 'locations.id') | |
->leftJoin('priorities', 'tickets.priority_id', '=', 'priorities.id') | |
->leftJoin('statuses', 'tickets.status_id', '=', 'statuses.id') | |
->select('clients.name as client_name', 'tickets.*', 'locations.location', 'priorities.name as priority_name', 'statuses.name as status_name') | |
->orderBy($columns[$column], $dir) | |
->paginate($length); | |
return [ | |
'draw' => $draw, | |
'recordsTotal' => $tickets->total(), | |
'recordsFiltered' => $tickets->total(), | |
'data' => $tickets | |
]; | |
} | |
return view('ticket/index'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you.