Datatable – responsive and select row

Home » Software Development » Datatable – responsive and select row
Software Development, Web App No Comments

The default behavior of responsive datatable with checkbox srow selection use the same columsn for expansion and select.

To resolve, you assign a separate column (first column) to handle the responsiveness, and a different column (2nd column) for row selection.

Here’s an example. https://www.titanwolf.org/Network/q/f8b83600-61df-49b0-92de-15625240b4b0/yQuestion Responsive datatable with first column as checkboxwww.titanwolf.org

Some extra explanation on how to have the responsive (+ sign) handling in a separate column. https://datatables.net/extensions/responsive/examples/child-rows/column-control.html

$(document).ready(function() {
  var table = $('#example').DataTable({
    select: {
      style: 'multi',
      selector: '.select-checkbox',
      items: 'row',
    },
    responsive: {
      details: {
        type: 'column',
        target: 0
      }
    },
    columnDefs: [{
        targets: 0,
        className: 'control'
      },
      {
        targets: 1,
        className: 'select-checkbox'
      },
      {
        targets: [0, 1],
        orderable: false
      }
    ],
    order: [2, 'asc']
  });
});
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.1.2/css/buttons.dataTables.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/responsive/2.0.2/css/responsive.dataTables.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/select/1.1.2/css/select.dataTables.min.css" />

<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.1.2/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.1.2/js/buttons.flash.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.1.2/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/responsive/2.0.2/js/dataTables.responsive.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/select/1.1.2/js/dataTables.select.min.js"></script>

<div class="container">
  <table id="example" class="display nowrap" width="100%">
    <thead>
      <tr>
        <th></th>
        <th></th>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Age</th>
        <th>Start date</th>
        <th>Salary</th>
      </tr>
    </thead>

    <tfoot>
      <tr>
        <th></th>
        <th></th>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Age</th>
        <th>Start date</th>
        <th>Salary</th>
      </tr>
    </tfoot>

    <tbody>
      <tr>
        <td></td>
        <td></td>
        <td>Tiger Nixon</td>
        <td>System Architect</td>
        <td>Edinburgh</td>
        <td>61</td>
        <td>2011/04/25</td>
        <td>$3,120</td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td>Garrett Winters</td>
        <td>Director</td>
        <td>Edinburgh</td>
        <td>63</td>
        <td>2011/07/25</td>
        <td>$5,300</td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td>Ashton Cox</td>
        <td>Technical Author</td>
        <td>San Francisco</td>
        <td>66</td>
        <td>2009/01/12</td>
        <td>$4,800</td>
      </tr>
    </tbody>