jQuery DataTables: How to select columns - Using table cells

Example for article at https://www.gyrocode.com/articles/jquery-datatables-how-to-select-columns/

by gyrocode

HTML

<link rel="stylesheet" href="https://cdn.datatables.net/v/dt/dt-1.10.13/se-1.2.0/datatables.min.css">
<script src="https://cdn.datatables.net/v/dt/dt-1.10.13/se-1.2.0/datatables.min.js"></script>
<h3><a target="_blank" href="https://www.gyrocode.com/articles/jquery-datatables-how-to-navigate-rows-with-keytable-plugin/">jQuery DataTables: How to select columns</a> <small>Using  table cells</small></h3>

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </tfoot>
    <tbody>
        <tr>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$320,800</td>
        </tr>
        <tr>
            <td>Garrett Winters</td>
            <td>Accountant</td>
            <td>Tokyo</td>
            <td>63</td>
            <td>2011/07/25</td>
            <td>$170,750</td>
        </tr>
        <tr>
            <td>Ashton Cox</td>
            <td>Junior Technical Author</td>
            <td>San Francisco</td>
            <td>66</td>
            <td>2009/01/12</td>
            <td>$86,000</td>
        </tr>
        <tr>
            <td>Bradley Greer</td>
            <td>Software Engineer</td>
            <td>London</td>
            <td>41</td>
            <td>2012/10/13</td>
            <td>$132,000</td>
        </tr>
        <tr>
            <td>Dai Rios</td>
            <td>Personnel Lead</td>
            <td>Edinburgh</td>
            <td>35</td>
            <td>2012/09/26</td>
            <td>$217,500</td>
       ...

CSS

.dataTable tbody td {
  cursor: pointer;
}

JavaScript

$(document).ready(function (){
    var table = $('#example').DataTable({
       'select': 'api'
    });
    
    // Handle click event on table cell
    $('#example').on('click', 'tbody td', function(){
       // If this column is selected
       if(table.column(this, { selected: true }).length){
          table.column(this).deselect();
          
       // Otherwise, if this column is not selected
       } else {
          table.column(this).select();      
       }
    });
});