JSFiddle - React, Tailwind, and code Playground

by Richard

HTML

<!DOCTYPE html>
<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

    <link href="https://editor.datatables.net/extensions/Editor/css/editor.dataTables.min.css" rel="stylesheet" type="text/css" />
    <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>
    <script src="https://editor.datatables.net/extensions/Editor/js/dataTables.editor.min.js"></script>

    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
  </head>
  <body>
    <div class="container">
      <br>
      <button id="addRow">Add new row</button>
      <table id="example" class="display nowrap" width="100%">
        <thead>
          <tr>
            <th>Name</th>
            <th>Price</th>
            <th>Qty</th>
            <th>Total</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Seat</td>
            <td>690</td>
            <td>2</td>
            <td></td>
          </tr>
        </tbody>

      </table>
      <div>
        TOTAL: <span class="total"></span>
      </div>
    </div>
  </body>
</html>

CSS

body {
  font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
  margin: 0;
  padding: 0;
  color: #333;
  background-color: #fff;
}

JavaScript

var table;

// JSON data from WebSocket (I don't use AJAX)
function addRow(json) {
  var row = JSON.parse(json);
//   var node = table.row.add(row).draw().node();
  var node = table.row.add({
    "name": row.name,
    "price": row.price,
    "quantity": row.quantity,
//     "total": row.total
  }).draw().node();

}


$(document).ready( function () {
  
  
  table = $('#example').DataTable({
    "columns": [
      { "data": "name" },
      { "data": "price" },
      { "data": "quantity" },
      { "data": null, render: function (data, type, row) { // null is to get all data for computing values, see "Computing values" section at https://datatables.net/manual/data/renderers#Functions 
          return ((data.price * data.quantity));
      }},    ],
  
  });
  var json = '{"name":"Tyre", "price": 149, "quantity": 2, "total": 100}';
  
  $('#addRow').on( 'click', function () {
    addRow(json);
  });
          
  // Automatically add a first row of data
  $('#addRow').click();
      console.log(table.column(3).data());
  $('.total').text(table.column(3).data());
} );