Hi :)
I am working on a Laravel project where I have a table containing tens of thousands of records, which I must display to the user in a blade view (in a table).
Currently, I am using a function in the controller to get all records ($devices) from the table (devices table) using Laravel's pagination, as shown below:
In the controller:
public function index()
{
$devices = Device::paginate(50);
return view('devices', [
'devices'=>$devices,
]);
}
In my devices.blade.php view, I am then displaying all rows and columns in a bootstrapTable, as shown below:
@if(count($devices))
{{-- Show all devices table --}}
<div
id="toolbar">
</div>
<table id="table" class="table table-responsive table-theme v-middle" data-plugin="bootstrapTable" data-toolbar="#toolbar" data-search="false" data-search-align="left" data-show-export="false" data-show-columns="true" data-detail-view="false" data-mobile-responsive="true"
data-pagination="false" data-page-list="[10, 25, 50, 100, ALL]">
<thead>
<tr>
<th data-field="id">ID</th>
<th data-field="code">Device Code</th>
<th data-field="comments">Comments</th>
Other fields...
</tr>
</thead>
{{-- Table content --}}
<tbody>
@foreach ($devices as $device)
<tr class=" " data-id=" ">
{{-- <td style="min-width:30px;text-align:center"> --}}
<td class="flex">
{{ $device->id }}
</td>
<td class="flex">
{{ $device->code }}
</td>
<td class="flex">
{{ $device->comments }}
</td>
Other fields...
</tr>
@endforeach
</tbody>
</table>
<div>
{{ $devices->links() }}
</div>
@else
<div class="row row-sm sr">
<p>Sorry, but nothing matched your search terms. Please try again with some different keywords.</p>
</div>
@endif
The issues I am encountering are the following:
-
Since I'm using Laravel's pagination feature to deal with the large quantity of data which I need to display, Bootstrap table features such as data-search, data-show-columns and data-sortable only work for the table on the current page. This means that when using the bootstrap table's search bar, you would only be searching in the current page and not in all records.
-
Similarly, when sorting the fields in ascending/descending order, the sorting is only applied to the 10/25/50/100 records currently being displayed and not to the entire list of records (even those which are in other pagination pages).
-
With regards to show/hide columns, when I click on the next page button (the one added when using Laravel Pagination, to go from mywebiste/devices/ to mywebiste/devices/?page=2 (the second page of the paginated results), all columns are displayed again, even if some were hidden in the previous page.
-
'Passing' all records (thousands) to the view without Laravel's Pagination was not an option due to the large amount of data that needs to be displayed.
What would be the best solution to achieve the required search, sort, filter, hide columns features in the table, while not running into problems related to loading tens of thousands of records from the database please?
Note: I have successfully implemented search and sorting features using a POST form in my devices view and a search/sort method in the DeviceController, however, I still believe there is a better/cleaner and simpler way of achieving these features - And I still haven't managed to do something similar for 'hiding/showing columns' in the table.
Thanks!