How to create button with function to export the DataTable to (.csv) in jQuery?

3

I have the following button:

$(document).ready(function() {
  $('#example').DataTable({
    "dom": 'T<"clear">lfrtip',
    "tableTools": {
      "sSwfPath": "/swf/copy_csv_xls_pdf.swf"
    }
  });
});
<script src="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script><scriptsrc="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.js"></script>
<link href="https://cdn.datatables.net/1.10.2/css/jquery.dataTables.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="http://www.datatables.net/release-datatables/extensions/TableTools/js/dataTables.tableTools.js"></script><linkhref="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><buttonid="btnExportar">Exportar</button>

<table id="example" class="display" cellspacing="0" width="100%">

  <thead>
    <tr>
      <th>Nome</th>
      <th>E-Mail</th>
      <th>Profissão</th>


    </tr>
  </thead>

  <tfoot>
    <tr>
      <th>
        Nome
      </th>
      <th>
        E-Mail
      </th>
      <th>
        Profissão
      </th>



    </tr>
  </tfoot>

  <tbody>
    <td>Tiago Ferezin</td>
    <td>[email protected]</td>
    <td>Programador</td>

  </tbody>
</table>

As we see in the code the function is not working, I do not know the reason, and I would like to associate this function with the export button so that when the user clicks this button, it already opens the download window to download in * .csv, DataTable content, on the user's PC.

How do I resolve it?

    
asked by anonymous 12.03.2016 / 21:03

1 answer

2

Datatables has an implementation of File Export that it does what you need and have a specific topic for csv . For everything to work you have to make the amounts of all the files listed just below the example on the site. Here's a jsfiddle for you to test:)

    
14.03.2016 / 15:09