<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-1.12.4.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>
    <script src="https://cdn.datatables.net/buttons/1.2.2/js/buttons.html5.js"></script>
    <link href="https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css" rel="stylesheet" type="text/css" />
    <script src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.colVis.min.js"></script>
    <script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
  </head>
  <body>
    <div class="container">
      <table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </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({
      dom: 'Bfrtip',
    paging: true,
      "ajax": {
        "url": "ajax/objects_root_array.txt",
        "dataSrc": ""
      },
      "columns": [
        { "data": "name" },
        { "data": "position" },
        { "data": "office" },
        { "data": "extn" },
        { "data": "start_date" },
        { "data": "salary" }
      ],
      "createdRow": function(row, data, index) {
        if (data.office == "London") {
          $('td:eq(2)', row).css('background-color', 'Red');
        } else if (data.office == "New York") {
          $('td:eq(2)', row).css('background-color', 'Green');
        }
        
        if (data.position == "Accountant") {
          $('td:eq(1)', row).css('background-color', 'Red');
        } else if (data.position == "Software Engineer") {
          $('td:eq(1)', row).css('background-color', 'Green');
        }
        if (data.name == "Ashton Cox") {
          $('td:eq(0)', row).css('background-color', 'Red');
        } else if (data.name == "Caesar Vance") {
          $('td:eq(0)', row).css('background-color', 'Green');
        }
      },
    buttons: [{
      extend: 'excelHtml5',
      customize: function ( xlsx ) {
        var sheet = xlsx.xl.worksheets['sheet1.xml'];
        
        // Map used to map column index to Excel index
        var excelMap = {
          0: 'A',
          1: 'B',
          2: 'C',
          3: 'D',
          4: 'E',
          5: 'F'
        };
        
        var count = 0;
        var skippedHeader = false;
        $('row', sheet).each( function () {
          var row = this;
          if (skippedHeader) {
//             var colour = $('tbody tr:eq('+parseInt(count)+') td:eq(2)').css('background-color');
            
            // Output first row
            if (count === 0) {
              console.log(this);
            }
            
            for (td=0; td<6; td++) {
              
              // Output cell contents for first row
              if (count === 0) {
                console.log($('c[r^="' + excelMap[td] + '"]', row).text());
              }
              var colour = $(table.cell(':eq('+count+')',td).node()).css('background-color');            
              if (colour === 'rgb(255, 0, 0)' || colour === 'red') {
                $('c[r^="' + excelMap[td] + '"]', row).attr( 's', '35' );
              }
              else if (colour === 'rgb(0, 128, 0)' || colour === 'green') {
                $('c[r^="' + excelMap[td] + '"]', row).attr( 's', '40' );
              }
            }
            count++;
          }
          else {
            skippedHeader = true;
          }
        });
      }
    }]
  });
});
Output 300px

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

Dismiss x
public
Bin info
anonymouspro
0viewers