Showing and Hiding Long Data in a Table

09 Oct 2022

Runnable code snippet.

The JavaScript:

JavaScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var truncatedLength = 38; // set this to whatever you prefer

var table;
var origDataMap = new Map(); // the original (full) data for long text

$(document).ready(function() {

  table = $('#TableName').DataTable({
    columnDefs: [{
      targets: ['_all'],
      render: function(data, type, row, meta) {
        // sets up initially minimized text for content longer than truncatedLength:
        var api = new $.fn.dataTable.Api(meta.settings);
        var node = api.cell(meta.row, meta.col, {
          order: 'index'
        }).node();
        var hasToggler = $(node).children().length > 0;

        if (type === 'display' && !hasToggler && data.length > truncatedLength + 5) {
          origDataMap.set(meta.row + '-' + meta.col, data);
          var displayData = '<span>' + data.substr(0, truncatedLength) + '... </span>' +
            '<button class="moreless-button" data-state="trunc"' +
            ' onclick="showMoreOrLess(this, ' + meta.row + ', ' + meta.col + ')">more</button>';
          return displayData;
        } else {
          return data;
        }
      }
    }]
  });

});

//
// toggles the DOM table cell's contents (but it
// does not change the underlying DataTables data):
//
function showMoreOrLess(node, rowId, colId) {
  var displayData;
  var state = $(node).attr('data-state');
  var origData = origDataMap.get(rowId + '-' + colId);
  var cellNode = $(node).parent();
  if (state === 'trunc') {
    displayData = '<span>' + origData + ' </span>' +
      '<button class="moreless-button" data-state="full"' +
      ' onclick="showMoreOrLess(this, ' + rowId + ', ' + colId + ')">less</button>';
  } else {
    displayData = '<span>' + origData.substr(0, truncatedLength) + '... </span>' +
      '<button class="moreless-button" data-state="trunc"' +
      ' onclick="showMoreOrLess(this, ' + rowId + ', ' + colId + ')">more</button>';
  }
  cellNode.html(displayData);
}

The CSS:

CSS
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
/* makes a button look like a link */

.moreless-button {
  background: none!important;
  border: none;
  padding: 0!important;
  font-family: arial, sans-serif;
  color: #069;
  text-decoration: underline;
  cursor: pointer;
}

The HTML:

HTML
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Demo</title>

  <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  <script type="text/javascript" src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css" />

  <link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">

  <script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.10.24/dataRender/ellipsis.js"></script>



</head>

<body>

  <div style="margin: 20px;">

    <table id="TableName" class="display dataTable cell-border" style="width:100%">
      <thead>
        <tr>
          <th>Number</th>
          <th>Severity</th>
          <th>Description</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Number123</td>
          <td>S1</td>
          <td>Description size keeps on varying</td>
        </tr>
        <tr>
          <td>Number345</td>
          <td>S9 major alarm severe major alarm severe major alarm severe</td>
          <td>Unable to log on</td>
        </tr>
        <tr>
          <td>Number234</td>
          <td>S1</td>
          <td>Flashing red lights in the corner of my eyes</td>
        </tr>
        <tr>
          <td>Number765</td>
          <td>S3</td>
          <td>Description again is very very very long very very very long very very very long</td>
        </tr>
        <tr>
          <td>Number678</td>
          <td>S1</td>
          <td>Description size keeps on varying</td>
        </tr>
        <tr>
          <td>Number543</td>
          <td>S3</td>
          <td>Lorem ipsum dolor sit amet, consectetur "adipiscing" elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
            irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</td>
        </tr>
      </tbody>
    </table>

  </div>


</body>

</html>