Skip to content

Commit

Permalink
Add ellipsis to long scanDataTable strings
Browse files Browse the repository at this point in the history
Addresses: #1, #263

Signed-off-by: Steven Esser <[email protected]>
  • Loading branch information
steven-esser committed Jul 11, 2019
1 parent 1dc4f20 commit d1aecbe
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions assets/app/js/controllers/scanDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,16 @@ class ScanDataTable extends Controller {
deferRender: true,
initComplete: () => this._initComplete(),
drawCallback: () => this._drawCallback(),
columnDefs: [{
targets: [4, 5, 8, 16, 17, 19, 20, 22, 23, 28, 31],
className: 'column-right-justify',
}],
columnDefs: [
{
targets: [4, 5, 8, 16, 17, 19, 20, 22, 23, 28, 31],
className: 'column-right-justify',
},
{
targets: '_all',
render: this._ellipsis(45, true)
}
],
buttons: [
{ // Do not allow the first column to be hidden
extend: 'colvis',
Expand Down Expand Up @@ -201,6 +207,45 @@ class ScanDataTable extends Controller {
return this._dataTable;
}

// Taken from https://datatables.net/plug-ins/dataRender/ellipsis
_ellipsis(cutoff, wordbreak, escapeHtml) {
var esc = function(t) {
return t
.replace( /&/g, '&amp;' )
.replace( /</g, '&lt;' )
.replace( />/g, '&gt;' )
.replace( /"/g, '&quot;' );
};

return function(d, type, row) {
if (type !== 'display') {
return d;
}

if (typeof d !== 'number' && typeof d !== 'string') {
return d;
}

d = d.toString(); // cast numbers

if (d.length < cutoff) {
return d;
}

var shortened = d.substr(0, cutoff-1);

if (wordbreak) {
shortened = shortened.replace(/\s([^\s]*)$/, '');
}

if (escapeHtml) {
shortened = esc(shortened);
}

return '<span class="ellipsis" title="'+esc(d)+'">'+shortened+'&#8230;</span>';
};
}

// This function is called every time DataTables needs to be redrawn.
// For details on the parameters https://datatables.net/manual/server-side
_query(dataTablesInput, dataTablesCallback) {
Expand Down

0 comments on commit d1aecbe

Please sign in to comment.