I'm having some difficulty migrating some Client Side Datatables logic to Server Side.
My current issue is that with Datatables, if you want to paginate a large set of data (20,000+ rows) I first need to load all the rows in the Controller and then pass them to the view:
$records = \App\Records::get();
return view("example.datatables")->with(["records" => $records]);
Following that, this takes about 2 minutes of waiting before everything is loaded and Datatables paginates the records into pages of 500:
$("#table").DataTable({
paging: true,
pageLength: 500,
...
});
I've changed the Datatables declaration to handle Server Side processing via ajax like so:
$("#table").DataTable({
processing: true,
serverSide: true,
ajax: {
url: "...",
type: "GET"
}, paging: true,
pageLength: 500,
...
});
Getting this to work as I'd like has gone quite well, but the issue is that Datatables is overriding or ignoring what I'm sending back from my ajax request:
$columnData = [];
foreach($recordsFromDatabase AS $record){
$columnDataObject = [];
$columnDataObject[0] = '<td class="myClass" data-property="myProperty"><input type="text" name="customInput[]"/></td>'
...
$columnData[] = $columnDataObject;
}
return response()->json([
"draw" => (int)$request->input("draw"),
"recordsTotal" => $totalRecords,
"recordsFiltered" => $totalFilteredRecords,
"data" => $columnData,
"error" => null
]);
Basically, what I'm sending back is a json response containing data, which is an array of columns, which are <td> elements, instead of just a plain value. So <td>Value</td> vs Value.
Datatables gives the illusion that this is working correctly, but I end up with
<tr role="row" class="even">
<td class="sorting_1"><input type="text" name="customInput[]"/></td>
...
</tr>
<tr role="row" class="odd">
<td class="sorting_1"><input type="text" name="customInput[]"/></td>
...
</tr>
It renders the <input> inside of the <td> correctly, but class="myClass" data-property="myProperty" are missing, which destroys the extended functionality of my table.
Is there some way to tell Datatables that what I'm sending back are valid <td> elements, and all it needs to do is add an odd or even class? (and even then, that's just for styling I think).