Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dataTables Export to Excel button is not showing

I have a problem in jquery dataTables. The "Export to Excel" is not showing in iPad and mobile devices. It is showing in desktop. Other buttons like copy, csv and pdf are showing in iPad and desktop. Here is my code :

$('#productDatatable').DataTable({
    dom: 'Bfrtip',
    buttons: [
        'copy', 'excel', 'pdf', 'csv'
    ]
} );
like image 401
Ninju Avatar asked Jun 22 '16 11:06

Ninju


11 Answers

Try loading required JS libraries in the following order, if anyone still having issues in showing the DataTable export buttons.

<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.3.1/js/dataTables.buttons.min.js"></script> 
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.3.1/js/buttons.html5.min.js"></script>

Cheers!

like image 74
Anjana Silva Avatar answered Sep 30 '22 02:09

Anjana Silva


I know this is super old but since I was using Webpack 4 and Babel and I was importing the files (ES6) I had to put in the global scope the jsZip:

import 'datatables.net-bs';
import jsZip from 'jszip';
import 'datatables.net-buttons-bs';
import 'datatables.net-buttons/js/buttons.colVis.min';
import 'datatables.net-buttons/js/dataTables.buttons.min';
import 'datatables.net-buttons/js/buttons.flash.min';
import 'datatables.net-buttons/js/buttons.html5.min';

// This line was the one missing
window.JSZip = jsZip;

Hope it helps Cheers

like image 30
jpacareu Avatar answered Sep 30 '22 03:09

jpacareu


For me I was missing the dom: 'Bfrtip', attribute in my table definition.

like image 22
Augustin Riedinger Avatar answered Sep 30 '22 02:09

Augustin Riedinger


Okay so I had the same exact problem (Excel button not showing while the other buttons are), and it appears that you have to include your JavaScript files in a specific order, or it won't work.

For me the problem was that I was including buttons.html5.js before jszip.js, but you have to put them in this order:

jszip.js
buttons.html5.js

And you also have to put these two files after the DataTables and DataTables.buttons files

What I find problematic is that if you put them in the wrong order, there isn't any JavaScript error displayed in the browser console.

like image 29
Kupay Avatar answered Sep 30 '22 03:09

Kupay


You should refer to the ★html5 versions

$('#productDatatable').DataTable({
  dom: 'Bfrtip',
  buttons: [
    'copyHtml5', 'excelHtml5', 'pdfHtml5', 'csvHtml5'
  ]
} );

Reason: When using copy, excel etc, you are in fact in risk of referring to the flash implementation, which really should be considered as a fallback only. Flash is removed / disabled from iPads and most smartphones, thats why the Excel button not is working. If you still have problems, ensure you have included these library files

jszip.min.js
pdfmake.min.js
vfs_fonts.js
buttons.html5.min.js
like image 27
davidkonrad Avatar answered Sep 30 '22 02:09

davidkonrad


Just add these references before your data table script. It works for me.

For generating Datatable use this -

<link rel="stylesheet" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>

And for data table button use these references -

<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.4.1/css/buttons.dataTables.min.css">
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/1.4.1/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/1.4.1/js/buttons.flash.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/1.4.1/js/buttons.html5.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/1.4.1/js/buttons.print.min.js"></script>
like image 35
sakib rahman Avatar answered Sep 30 '22 02:09

sakib rahman


I had same problem, it was not because of jquery scripts.

Go to browser setting(chrome in my case) > Search Flash >

Click on Control what information websites can use > it will navigate to content settings (chrome://settings/content?search=flash) >

Click on Flash >

In the Allow block add your website using Add button

like image 41
Rajdeep Avatar answered Sep 30 '22 01:09

Rajdeep


Adding info if someone else has problem with pdf button not showing with webpack 4, you have to use:

import pdfMake from 'pdfmake/build/pdfmake'
import pdfFonts from 'pdfmake/build/vfs_fonts'
import jsZip from 'jszip'
pdfMake.vfs = pdfFonts.pdfMake.vfs;
window.pdfMake = pdfMake;
window.JSZip = jsZip;
like image 42
Herz3h Avatar answered Sep 30 '22 01:09

Herz3h


Export to Excel doesn't work on Mac either. From the datatable's website: "The excelHtml5 button will not work in Safari at all - as noted in the button's documentation. This is because of an unimplemented feature in Safari."

Note that even if you use Chrome in apple products, it still uses Safari's engine so it won't work.

It might be fixed in the future but for now I just added the CSV button

like image 23
Alx Avatar answered Sep 30 '22 03:09

Alx


Try this

<link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css"> 
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.0.1/css/buttons.dataTables.min.css">

<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/2.0.1/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/2.0.1/js/buttons.html5.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/2.0.1/js/buttons.print.min.js"></script>

<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        } );
    } );
</script>

datatables.net:

like image 28
Merrin K Avatar answered Sep 30 '22 03:09

Merrin K


i ran into the same problem in Asp.NET MVC 4. the thing is if you add your CDN/JS files in _layout page then the buttons wont show in Main View page. instead add those CDN/JS files into the page you are using for data Table. also make sure they are in correct order.

jquery.min.js
jszip.min.js
pdfmake.min.js
vfs_fonts.js
buttons.html5.min.js
like image 39
Al Masum Fahim Avatar answered Sep 30 '22 03:09

Al Masum Fahim