<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/2.1.8/css/dataTables.bootstrap4.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/buttons/3.2.0/css/buttons.bootstrap4.min.css" rel="stylesheet">
 <script src="https://cdn.datatables.net/2.1.8/js/dataTables.min.js"></script>
<script src="https://cdn.datatables.net/2.1.8/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<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/buttons/3.2.0/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/3.2.0/js/buttons.bootstrap4.min.js"></script>
<script src="https://cdn.datatables.net/buttons/3.2.0/js/buttons.colVis.min.js"></script>
<script src="https://cdn.datatables.net/buttons/3.2.0/js/buttons.html5.min.js"></script>
    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
  </head>
  <body>
    <div class="row">
      <div class="col-12">
      <table id="example" class="display" width="100%" style="width:100%">
        <thead>
          <tr>
            <th class="noVis">Name</th>
            <th>VAT ID</th>
            <th>Office</th>
            <th>Age</th>
            <th class="sum">Salary</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Tiger Nixon</td>
            <td class="text-left">001538456</td>
            <td>Edinburgh <a href="#" class="noExport">(Link to info)</a></td>
            <td class="age">61</td>
            <td class="valuta">2000.00</td>
          </tr>
          <tr>
            <td>Garrett Winters</td>
            <td class="text-left">56789452</td>
            <td>Edinburgh</td>
            <td class="age">63</td>
            <td class="valuta">1000.50</td>
          </tr>
          <tr>
            <td>Ashton Cox</td>
            <td class="text-left">123456789</td>
            <td>San Francisco</td>
            <td class="age">66</td>
            <td class="valuta">100.22</td>
          </tr>
        </tbody>
        
        <tfoot>
          <tr> 
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th class="valuta"></th>
          </tr>
        </tfoot>
        
      </table>
    </div>
      </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', {
   layout: {
        topStart: 
            'buttons'
        
    },
  "footerCallback": function(row, data, start, end, display) {
                
                var api = this.api();
                
                api.columns('.sum', {
                    page: 'current'
                    }).every(function() {
                    
                    var sum = 0;
                    
                    var td = this.nodes();
                    
                    $(td).each(function(index, value) {
                          var valore = parseFloat($(this).text());
                          sum = sum + valore; 
                    });
                  
                    $(this.footer()).html(sum.toFixed(2)); 
                    
                });
                
    },
    buttons: [
                {
                    extend: 'colvis',
                    text: 'Show columns ',
                    columns: ':not(.noVis)'
                },
                {
                    extend: 'collection',
                    text: 'Export',
                    buttons: [
                        {
                            extend: 'excelHtml5',
                            footer: true,
                            text: 'Excel',
                            exportOptions: {
                                columns: [ ':not(.noVis):visible' ],
                                format: {
                                    body: function(data, row, column, node) {
                                        if (typeof data === 'string' || data instanceof String) {
                                            
                                            data = $('<div>' + data + '</div>'); // rimuovi i noExport tramite jquery
                                            data.find(".noExport").remove();
                                            data = data.html();
                                            
                                            data = data.replace( /<\/li>/ig, "\n"); //convert <li> tag to newline 
                                            data = data.replace( /<li\b[^>]*>/ig, "- ");
                                            
                                            data = data.replace( /<\/p>/ig, "\n"); //convert <p> tag to newline 
                                            data = data.replace( /<p\b[^>]*>/ig, "");
                                            
                                            data = data.replace( /<br\s*\/?>/ig, "\n"); //convert <br> tag to newline 
                                            
                                            data = data.replace(/[ \t]+/g, ' '); //remove unwanted tabs
                                            data = data.split(/\r?\n/); //split by newline
                                            data = data.map(s => s.trim()); // trim all string in array
                                            data = data.filter(item => item); //remove empty array string
                                            data = data.join('\n'); //recreate clean string
                                            
                                        }
                                        
                                        data = $('<p>' + data + '</p>').text(); // dopo le varie manipolazioni, posso eliminare l'html
                                        
                                        data = data.trim(); //elimino eventuali spaziature inizio/fine residue
                                        
                                        return data;
                                        
                                    },
                                    footer: function(data, row, column, node) {
                                        if (typeof data === 'string' || data instanceof String) {
                                            
                                            data = $('<div>' + data + '</div>'); // rimuovi i noExport tramite jquery
                                            data.find(".noExport").remove();
                                            data = data.html();
                                            
                                            data = data.replace( /<\/li>/ig, "\n"); //convert <li> tag to newline 
                                            data = data.replace( /<li\b[^>]*>/ig, "- ");
                                            
                                            data = data.replace( /<\/p>/ig, "\n"); //convert <p> tag to newline 
                                            data = data.replace( /<p\b[^>]*>/ig, "");
                                            
                                            data = data.replace( /<br\s*\/?>/ig, "\n"); //convert <br> tag to newline 
                                            
                                            data = data.replace(/[ \t]+/g, ' '); //remove unwanted tabs
                                            data = data.split(/\r?\n/); //split by newline
                                            data = data.map(s => s.trim()); // trim all string in array
                                            data = data.filter(item => item); //remove empty array string
                                            data = data.join('\n'); //recreate clean string
                                            
                                        }
                                        
                                        data = $('<p>' + data + '</p>').text(); // dopo le varie manipolazioni, posso eliminare l'html
                                        
                                        data = data.trim(); //elimino eventuali spaziature inizio/fine residue
                                        
                                        return data;
                                        
                                    }
                                }
                            },
                            createEmptyCells: true, // <-- importante per manipolare gli stili delle celle vuote
                            customize: function( xlsx ) {
                                var styles = $('cellXfs', xlsx.xl['styles.xml']);
                                
                                // Append vertical="top" + wrap text style
                                styles.append('<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyFont="1" applyFill="1" applyBorder="1" applyAlignment="1">' + '<alignment vertical="top" wrapText="1" />' + '</xf>');
                                
                                var sheet = xlsx.xl.worksheets['sheet1.xml'];
                                
                                var oxf = $('xf', xlsx.xl['styles.xml']);
                                
                                var rows = $('row', sheet);
                                
                                /*
                                    oxf starts with a default format of:
                                    <xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
                                    
                                    DataTables creates it's styles here:
                                    https://github.com/DataTables/Buttons/blob/c25ea69f9241501b36193afffb91196928aab18f/js/buttons.html5.js#L645
                                    
                                    oxf.length = oxf default (1) + datatables styles + additional styles appended above
                                    
                                    For example 1 + 68 + 1 = 70
                                    
                                    To choose the appended style above 1 must be subtracted for the default 
                                    and 1 subtracted for 0 index, thus styleIndex - 2 to access the last appended style.
                                    
                                */
                                
                                var styleIndex = oxf.length;
                                var vertTopWrapIndex = styleIndex - 2;
                                
                                // corpo
                                $('row c', sheet).attr( 's', vertTopWrapIndex );    // set cell style: vertical top + wrapped text
                                
                                //header (2 righe) in bold con sfondo gray e bordo sottile: lista codici: https://datatables.net/reference/button/excelHtml5
                                $('row:eq(0) c', sheet).attr( 's', '32' );
                                $('row:eq(1) c', sheet).attr( 's', '32' );
                                
                                $('row:eq('+(rows.length-1)+') c', sheet).attr( 's', '32' ); //footer
                                
                            }
                        },
                        {
                            extend: 'pdfHtml5',
                            footer: true,
                            orientation: 'landscape',
                            pageSize : 'A4',
                            text: 'PDF',
                            customize: function(doc) {
                                
                                var rows = doc.content[1].table.body.length;  // ALLINEA A DX GLI IMPORTi
                                
                                for (var i = 0; i < rows; i++) {
                                    
                                    var colonne = doc.content[1].table.body[i].length;
                                    
                                    for (var k = 0; k < colonne; k++) {
                                        
                                        if (
                                          $(table.row(i).column(k).nodes()).hasClass("valuta") ||
                                          $(table.row(i).column(k).nodes()).hasClass("age")
                                        ) {
                                            
                                            doc.content[1].table.body[i][k].alignment = 'right';
                                            
                                        }
                                        
                                    }
                                    
                                }
                                
                                
                                
                                doc['footer']=(function(page, pages) {  //  CONTA PAGINE
                                    return {
                                        columns: [
                                            '', //LEFT
                                            {
                                                alignment: 'right',
                                                text: [
                                                    'Pag. ', 
                                                    { text: page.toString() },
                                                    ' di ',
                                                    { text: pages.toString() }
                                                ]
                                            }
                                        ],
                                        margin: [10, 0]
                                    }
                                });
                                
                            },
                            exportOptions: {
                                columns: [ ':not(.noVis):visible' ],
                                format: {
                                    body: function ( data, row, column, node ) {
                                        
                                        if (typeof data === 'string' || data instanceof String) {
                                            
                                            data = $('<div>' + data + '</div>'); // rimuovi i noExport tramite jquery
                                            data.find(".noExport").remove();
                                            data = data.html();
                                            
                                            data = data.replace( /<\/li>/ig, "\n"); //convert <li> tag to newline 
                                            data = data.replace( /<li\b[^>]*>/ig, "- ");
                                            
                                            data = data.replace( /<\/p>/ig, "\n"); //convert <p> tag to newline 
                                            data = data.replace( /<p\b[^>]*>/ig, "");
                                            
                                            data = data.replace( /<br\s*\/?>/ig, "\n"); //convert <br> tag to newline 
                                            
                                            data = data.replace(/[ \t]+/g, ' '); //remove unwanted tabs
                                            data = data.split(/\r?\n/); //split by newline
                                            data = data.map(s => s.trim()); // trim all string in array
                                            data = data.filter(item => item); //remove empty array string
                                            data = data.join('\n'); //recreate clean string
                                            
                                        }
                                        
                                        data = $('<p>' + data + '</p>').text();
                                        
                                        data = data.trim(); //elimino eventuali spaziature inizio/fine residue
                                        
                                        return data;
                                        
                                    },
                                    footer: function ( data, column, node ) {
                                        
                                        if (typeof data === 'string' || data instanceof String) {
                                            
                                            data = $('<div>' + data + '</div>'); // rimuovi i noExport tramite jquery
                                            data.find(".noExport").remove();
                                            data = data.html();
                                            
                                            data = data.replace( /<\/li>/ig, "\n"); //convert <li> tag to newline 
                                            data = data.replace( /<li\b[^>]*>/ig, "- ");
                                            
                                            data = data.replace( /<\/p>/ig, "\n"); //convert <p> tag to newline 
                                            data = data.replace( /<p\b[^>]*>/ig, "");
                                            
                                            data = data.replace( /<br\s*\/?>/ig, "\n"); //convert <br> tag to newline 
                                            
                                            data = data.replace(/[ \t]+/g, ' '); //remove unwanted tabs
                                            data = data.split(/\r?\n/); //split by newline
                                            data = data.map(s => s.trim()); // trim all string in array
                                            data = data.filter(item => item); //remove empty array string
                                            data = data.join('\n'); //recreate clean string
                                            
                                        }
                                        
                                        data = $('<p>' + data + '</p>').text();
                                        
                                        data = data.trim(); //elimino eventuali spaziature inizio/fine residue
                                        
                                        return data;
                                        
                                    }
                                }
                            }
                        }
                        
                    ]
                },
                'pageLength'
            ] 
  
});
1 error 9 warnings
Line 66: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 67: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 96: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 97: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 185: ['footer'] is better written in dot notation.
Line 200: Missing semicolon.
Line 225: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 226: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 256: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 257: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Output 300px

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

Dismiss x
public
Bin info
anonymouspro
0viewers