contao-components/tablesort
contao-components/tablesort adds client-side table sorting to Contao projects. Easily enable sortable columns for HTML tables, improving data presentation and usability in the backend or frontend with minimal setup.
Installation
composer require contao-components/tablesort
npm install tablesort --save
Ensure tablesort.js and tablesort.css are published to your public/js and public/css directories (check the package docs for exact paths).
Basic Usage in Blade
<table id="sortable-table" class="tablesort">
<thead>
<tr>
<th data-sort="string">Name</th>
<th data-sort="number">Age</th>
<th data-sort="date">Join Date</th>
</tr>
</thead>
<tbody>
<!-- Table rows -->
</tbody>
</table>
<script>
$(document).ready(function() {
$('#sortable-table').tablesort();
});
</script>
First Use Case
foreach loop in Blade) by clicking column headers.created_at (date) or name (string).Dynamic Table Generation Use Laravel Blade to generate tables dynamically (e.g., from Eloquent queries):
@foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->age }}</td>
<td>{{ $user->created_at->format('Y-m-d') }}</td>
</tr>
@endforeach
Wrap in <table class="tablesort"> and initialize with tablesort().
Server-Side Sorting (Recommended for Large Datasets)
$('#sortable-table').tablesort({
onSort: function(table, column) {
$.get('/users/sort', { column: column, order: 'asc' }, function(data) {
$('#sortable-table tbody').html(data.html);
});
}
});
Route::get('/users/sort', [UserController::class, 'sort']);
public function sort(Request $request) {
$users = User::orderBy($request->column, $request->order)->get();
return response()->json([
'html' => view('users.table_rows', compact('users'))->render()
]);
}
Custom Sorting Logic
Extend tablesort with custom comparators:
$('#sortable-table').tablesort({
sortFunction: function(a, b, column) {
if (column === 'status') {
const statusOrder = { 'active': 1, 'inactive': 2 };
return statusOrder[a] - statusOrder[b];
}
}
});
Integration with Laravel Collectives
Pair with HTML::table() for cleaner Blade syntax:
{{ HTML::table('sortable', $users->toArray(), ['name', 'age', 'created_at']) }}
CSS Conflicts
tablesort.css may override Bootstrap/FlatUI table styles. Override with:
.tablesort th.sort-asc::after {
content: " ↑";
color: #007bff !important;
}
Case-Sensitive Sorting
localeCompare for case-insensitive:
sortFunction: (a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' })
Nested Tables
tablesort may misbehave.Initial Load State
$('#sortable-table').trigger('update');
tablesort.js is loaded and no 404s appear for dependencies (e.g., jQuery).data-sort Attributes
Missing or incorrect attributes (e.g., data-sort="invalid") will break sorting.column/order params in Laravel to confirm they match the request.Custom Icons Replace default arrows with Font Awesome:
$('#sortable-table').tablesort({
ascendingIcon: '<i class="fas fa-sort-up"></i>',
descendingIcon: '<i class="fas fa-sort-down"></i>'
});
Multi-Column Sorting
Use a plugin like tablesort-multicol or implement a custom solution with shift+click handlers.
Laravel Service Provider
Register a global JS asset for tablesort:
// app/Providers/AppServiceProvider.php
public function boot() {
View::composer('*', function ($view) {
$view->with('tablesortScript', '<script src="/js/tablesort.js"></script>');
});
}
Then include $tablesortScript in your layout.
Localization Override text labels (e.g., for RTL languages):
$('#sortable-table').tablesort({
text: {
asc: '↑',
desc: '↓'
}
});
How can I help you explore Laravel packages today?