I am using DataTables to display, paginate and sort a set of data.
new DataTable('#employees', {
info: false,
paging: true,
filter: false,
"aLengthMenu": [5, 10],
initComplete: function() {
if (this.api().page.info().pages < 2) {
$('.dt-paging').hide();
}
}
});
$(document).ready(function() {
$('.truncate').block_ellipsis({
lines: 2,
more: function(nb) {
return '<span class="badge bg-secondary">Show ' + nb + ' more</span>';
},
less: '<span class="badge bg-secondary">Show less</span>'
});
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/2.1.8/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.datatables.net/2.1.8/js/dataTables.min.js"></script>
<script src="https://cdn.datatables.net/2.1.8/js/dataTables.bootstrap5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://www.jqueryscript.net/demo/jQuery-Plugin-To-Show-ide-Block-Elements-Block-Ellipsis/dist/block_ellipsis.js"></script>
<div class="container-fluid my-2">
<h2>Data Tables</h2>
<table id="employees" class="table table-bordered table-striped mx-1">
<thead>
<tr>
<th class="w-auto">Name</th>
<th class="w-25">Position</th>
<th class="w-10">Office</th>
<th class="w-25">Age</th>
<th class="w-10">Skills</th>
<th class="w-10">Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">JavaScript</span>
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">PHP</span>
<span class="badge rounded-pill bg-secondary">Laravel</span>
<span class="badge rounded-pill bg-secondary">Java</span>
<span class="badge rounded-pill bg-secondary">Node</span>
<span class="badge rounded-pill bg-secondary">CI/CD</span>
</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">JavaScript</span>
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">PHP</span>
</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Senior Software Developer</td>
<td>San Francisco</td>
<td>66</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">JavaScript</span>
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">PHP</span>
<span class="badge rounded-pill bg-secondary">Laravel</span>
<span class="badge rounded-pill bg-secondary">React</span>
<span class="badge rounded-pill bg-secondary">Angular</span>
<span class="badge rounded-pill bg-secondary">Java</span>
</td>
<td>$86,000</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">JavaScript</span>
<span class="badge rounded-pill bg-secondary">CSS</span>
</td>
<td>$433,060</td>
</tr>
<tr>
<td>Ștefan Popa</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">React</span>
</td>
<td>$162,700</td>
</tr>
<tr>
<td>Allan Smith</td>
<td>Accountant</td>
<td>London</td>
<td>33</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">React</span>
</td>
<td>$162,700</td>
</tr>
</tbody>
</table>
</div>
I am using the Block Ellipsis plugin in order to truncate the cells on the "Skills" column if they are too long.
The problem I am faced with is that the truncation only works on the first page of results.
What causes this issue and how can I fix it?
A quick and simple fix would be do run the plugin before Datatable creation.
$(document).ready(function() {
$('.truncate').block_ellipsis({
lines: 2,
more: function(nb) {
return '<span class="badge bg-secondary">Show ' + nb + ' more</span>';
},
less: '<span class="badge bg-secondary">Show less</span>'
});
new DataTable('#employees', {
info: false,
paging: true,
filter: false,
"aLengthMenu": [5, 10],
initComplete: function() {
if (this.api().page.info().pages < 2) {
$('.dt-paging').hide();
}
}
});
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/2.1.8/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.datatables.net/2.1.8/js/dataTables.min.js"></script>
<script src="https://cdn.datatables.net/2.1.8/js/dataTables.bootstrap5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://www.jqueryscript.net/demo/jQuery-Plugin-To-Show-ide-Block-Elements-Block-Ellipsis/dist/block_ellipsis.js"></script>
<div class="container-fluid my-2">
<h2>Data Tables</h2>
<table id="employees" class="table table-bordered table-striped mx-1">
<thead>
<tr>
<th class="w-auto">Name</th>
<th class="w-25">Position</th>
<th class="w-10">Office</th>
<th class="w-25">Age</th>
<th class="w-10">Skills</th>
<th class="w-10">Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">JavaScript</span>
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">PHP</span>
<span class="badge rounded-pill bg-secondary">Laravel</span>
<span class="badge rounded-pill bg-secondary">Java</span>
<span class="badge rounded-pill bg-secondary">Node</span>
<span class="badge rounded-pill bg-secondary">CI/CD</span>
</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">JavaScript</span>
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">PHP</span>
</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Senior Software Developer</td>
<td>San Francisco</td>
<td>66</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">JavaScript</span>
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">PHP</span>
<span class="badge rounded-pill bg-secondary">Laravel</span>
<span class="badge rounded-pill bg-secondary">React</span>
<span class="badge rounded-pill bg-secondary">Angular</span>
<span class="badge rounded-pill bg-secondary">Java</span>
</td>
<td>$86,000</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">JavaScript</span>
<span class="badge rounded-pill bg-secondary">CSS</span>
</td>
<td>$433,060</td>
</tr>
<tr>
<td>Ștefan Popa</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">React</span>
</td>
<td>$162,700</td>
</tr>
<tr>
<td>Allan Smith</td>
<td>Accountant</td>
<td>London</td>
<td>33</td>
<td class="truncate">
<span class="badge rounded-pill bg-secondary">CSS</span>
<span class="badge rounded-pill bg-secondary">React</span>
</td>
<td>$162,700</td>
</tr>
</tbody>
</table>
</div>