Get filename for export (excelHtml5) from attribute table name

Get filename for export (excelHtml5) from attribute table name

Taras LisnyakTaras Lisnyak Posts: 4Questions: 1Answers: 0
buttons: [
      {
      extend: 'excelHtml5',
      text: 'Экспорт в Excel',
      filename: function () {
        var tableName =   //how catch attribute table.name??? 
                          //if i have many table on page!!! 
                          //$('table.dataTable').DataTable(); -- my selectid
         return tableName;
         },
         exportOptions: {
           modifier: {
                page: 'all'
           }
                            
         },
         className: 'btn btn-outline-success'

       }
   ]

This question has an accepted answers - jump to answer

Answers

  • Taras LisnyakTaras Lisnyak Posts: 4Questions: 1Answers: 0

    this is example on JSFiddler

    https://jsfiddle.net/Taras_wind/fk7nzpsd/

  • colincolin Posts: 15,232Questions: 1Answers: 2,597
    Answer ✓

    You could do something like this: http://live.datatables.net/bavoguro/1/edit

    It's not the particularly elegant, but it'll do the trick.

    Colin

  • Taras LisnyakTaras Lisnyak Posts: 4Questions: 1Answers: 0

    Colin, thank you very much!

    this code working!

    $(document).ready(function() {
    
        var filename = '';
    
        $.extend($.fn.DataTable.defaults, {
            dom: '<"html5buttons">sTgBf',
            buttons: [
    
                {
                    extend: 'excelHtml5',
                    filename: function() {
                        return filename;
                    }
                }
            ]
        });
    
        var table = $('.dataTable').DataTable();
    
        $('.buttons-excel').on('mousedown keydown', function() {
            filename = $(this).closest('.dataTables_wrapper').next('table').attr('id');
        });
    });
    
  • Taras LisnyakTaras Lisnyak Posts: 4Questions: 1Answers: 0

    this code, working too!!!

    $(document).ready(function() {
      $('table.dataTable').each(function(i) {
        var thisTable = $(this);
        var thisName = thisTable.attr('title');
        thisTable.DataTable({
          dom: 'Bfrtip',
          buttons: [{
            extend: "excelHtml5",
            text: "Export to XLSx",
            filename: function() {
              filename = thisName;
              return filename;
            }
          }]
        });
      })
    });
    
This discussion has been closed.