<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<link href="https://cdn.datatables.net/v/dt/dt-2.1.8/b-3.2.0/b-html5-3.2.0/datatables.min.css" rel="stylesheet">
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/v/dt/dt-2.1.8/b-3.2.0/b-html5-3.2.0/datatables.min.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>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>My 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 class="valuta">$3,120</td>
          </tr>
          <tr>
            <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>Ashton Cox</td>
            <td>Technical Author</td>
            <td>San Francisco</td>
            <td>66</td>
            <td>2009/01/12</td>
            <td class="valuta">$4,800</td>
          </tr>
          <tr>
            <td>Cedric Kelly</td>
            <td>Javascript Developer</td>
            <td>Edinburgh</td>
            <td>22</td>
            <td>2012/03/29</td>
            <td class="valuta">$3,600</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;
}
 
var table = new DataTable('#example', {
  columnDefs: [
    {
      targets: 3,
      className: 'age'
    },
    {
      targets: 2,
      visible: false
    }
  ],
  layout: {
    topStart: {
      buttons: [
        {
          extend: 'pdfHtml5',
          exportOptions: {
            columns: ':visible'
          },
          customize: function (doc) {
            var body = doc.content[1].table.body;
            
            // Get visible column indexes
            var visbibleColumns = table.columns(':visible').indexes();
            
            // Loop all rows
            table.rows().every(function (rowIdx, tableLoop, rowLoop) {
              var row = this;
              
              // PDF column counter
              var colCounter = 0;
              
              // Loop through visible columns in the row
              table.cells(row, table.columns(':visible').indexes()).every( function (rowIdx, colIdx) {
                var node = this.node();
                
                // Align right if td classname matches
                if ( $(node).hasClass('valuta') || $(node).hasClass('age') ) {
                  
                  // Add 1 ro rowLoop to skip header
                  body[rowLoop + 1][colCounter].alignment = 'right';
                }
                
                // PDF column counter
                colCounter++;
                
              } );
              
            });
          }
        }
      ]
    }
  }
});
Output

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

Dismiss x
public
Bin info
anonymouspro
0viewers