<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
<link href="https://cdn.datatables.net/rowgroup/1.1.3/css/rowGroup.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.datatables.net/rowgroup/1.1.3/js/dataTables.rowGroup.js"></script>
    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
  </head>
  <body>
    <div class="container">
      <table id="example" class="display nowrap" width="100%">
        <thead>
          <tr>
            <th>index</th>
            <th>value</th>
            <th>diff</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>a</td>
            <td>60</td>
            <td></td>
          </tr>
          <tr>
            <td>a</td>
            <td>40</td>
            <td></td>
          </tr>
          <tr>
            <td>b</td>
            <td>60</td>
            <td></td>
          </tr>
          <tr>
            <td>b</td>
            <td>10</td>
            <td></td>
          </tr>
          <tr>
            <td>c</td>
            <td>60</td>
            <td></td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>
</html>
 
body {
  font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
  margin: 0;
  padding: 0;
  color: #333;
  background-color: #fff;
}
 
$(document).ready( function () {
  var table = $('#example').DataTable({
    order: [[0, 'desc']],  // Sort by index column
    columnDefs: [
      {
        targets: 0,
        orderData: [2, 0]  // Sort first by the diff column then by the index
      }
    ],
    rowGroup: {
      dataSrc: [0], 
    },
    initComplete: function () {
      var api = this.api();
      
      // Loop throug the unique rowGroup.dataSrc values
      api.column(0).data().unique().sort().each( function ( group, j ) {
        console.log(group);
        var diff = 0;
        // Get the values for the group
        var values = api
        .rows( function ( idx, data, node ) {
          return data[0] === group ?
            true : false;
        } )
        .data()
        .pluck(1)
        .toArray();
        console.log(values);
        // Calulate the difference if there are two values
        if (values.length === 2) {
          diff = (values[0] * 1) - (values[1] * 1);
        }
        // Get the rows() api for each row in the group
        var rows = api
        .rows( function ( idx, data, node ) {
          return data[0] === group ?
            true : false;
        } );
        // Use the rows API to update the hidden column
        rows.every( function ( rowIdx, tableLoop, rowLoop ) {
          var data = this.data();
          data[2] = diff;
          this.data( data );
        } );
      } );
      api.draw();
    }
  });
  
} );
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers