3

I want to have 3 buttons - to download csv, xls and pdf file. In my case when I click on the download csv file button, it saves with extension of csv file, but when I open it it is excel - it's not comma separated file. PDF download button is working correctly. xls button does not work - it's not clickable at all. I tried to write this code:

"TableTools": {
     "aButtons": [ {
          "sExtends":    "download",
          "sButtonText": "Download XLS",
          "sUrl":        "/generate_xls.php"
      }]
 }

But then each button download excel file.

Here's my code:

<html>
<head>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css" type="text/css"  />

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script>
<script src="https://www.datatables.net/release-datatables/media/js/jquery.js"></script>
<script src="https://www.datatables.net/release-datatables/media/js/jquery.dataTables.js"></script>
<script src="https://www.datatables.net/release-datatables/extensions/TableTools/js/dataTables.tableTools.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.2.0/ZeroClipboard.Core.min.js"></script>
<link rel="stylesheet" 
href="https://www.datatables.net/release-datatables/media/css/jquery.dataTables.css" type="text/css" />
<script>
$(document).ready(function() {
    $('#example').dataTable( {
        "pagingType": "full_numbers",
         "bSort": true,
         "sDom": 'T<"clear">lfrtip',
      "tableTools": {
     "sSwfPath": "http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf",
    
  aButtons: [
    { sExtends: "csv",
      sFileName: 'download.csv',
      sFieldSeperator: "," 
    },
    { sExtends: "xls",
      sFileName: 'download.xls'
    },
    { sExtends: "pdf",
      sFileName: 'download.pdf'
    }   
] 
 }


    } );
} );
</script>

<script>
$(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('#example tfoot th').each( function () {
        var title = $('#example thead th').eq( $(this).index() ).text();
        $(this).html( '<input type="text" placeholder="Търси '+title+'" />' );
    } );
 
    // DataTable
    var table = $('#example').DataTable();
 
    // Apply the search
    table.columns().eq( 0 ).each( function ( colIdx ) {
        $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
            table
                .column( colIdx )
                .search( this.value )
                .draw();
        } );
    } );
} );
</script>
<script>

</script>
</head>
<body>
<div class='col-md-8' id='student_results'>
<br/><br/>

<table  id='example'>
 <thead>
   <tr><th>Потребител</th><th>Училище</th><th>Клас</th><th>Анкета</th><th>Въпрос</th><th>Отговор</th></tr>
  </thead>  
  <tfoot>
            <tr>
                <th>Потребител</th>
                <th>Училище</th>
                <th>Клас</th>
                <th>Анкета</th>
                <th>Въпрос</th>
                <th>Отговор</th>
            </tr>
        </tfoot> 
  <tbody>

<?php 

foreach ($student_results as $row)
{

?>
 <tr><td>
 <?php echo $row->username; ?>
 </td><td>
 <?php echo $row->school; ?>
 </td><td>
 <?php echo $row->class; ?>
 </td><td>
 <?php echo $row->survey_id; ?>
   </td><td>
   <?php echo $row->question_id; ?>
   </td><td>
   <?php echo $row->answer; ?>
   </td></tr>
   <?php 
 }
 ?>
 
 </table>

 <table border='1' id='example'>
 <tr><th>Въпрос</th><th>Среден резултат</th></tr>
<?php
foreach ($average_results as $row)
{
?>   
   <td>
   <?php echo $row->question_id ; ?>
   </td><td>
   <?php echo round("$row->answer",2); ?>
   </td></tr>
   <?php
}
?>
</tbody>
 </table>



 </div>
 </body>
 </html>
ci_lover
  • 710
  • 1
  • 14
  • 38

1 Answers1

2

You need to target all desired buttons, when you are using this approach. Just having a "xls" breaks it all up.

aButtons: [
    { sExtends: "csv",
      sFileName: 'download.csv',
      sFieldSeperator: "," //<--- example of how to set the delimiter
    },
    { sExtends: "xls",
      sFileName: 'download.xls'
    },
    { sExtends: "pdf",
      sFileName: 'download.pdf'
    }   
] 
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • I don't know why if I use that type of code for aButtons, only first button downloads a file and in this case with your code when I click on the first button for csv file, it downloads a pdf file. I have tried many times... – ci_lover Apr 01 '15 at 19:03
  • @iveto - remove anything you have and use just that above. I do not believe clicking on the CSV button produces a PDF unless you have code duplicating the buttons behaviour. – davidkonrad Apr 01 '15 at 19:13
  • I removed the cache, but it's still the same. – ci_lover Apr 01 '15 at 19:28
  • @iveto, hmm. Can you please post the _complete_ markup and JS you are using in the original post? It sounds very weird. Then I tested I used your `.swf` etc, so it has nothing to do with that. – davidkonrad Apr 01 '15 at 19:34
  • Your code works right away for me. Have you any errors in the console? – davidkonrad Apr 01 '15 at 19:42
  • No, no errors. I have no idea what the problem is. It is shown in this way in the Data Tables examples. – ci_lover Apr 01 '15 at 19:46
  • For that link: //cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf it is written: Failed to load response data. – ci_lover Apr 01 '15 at 19:49
  • Oh! try http://cdn.datatables.net/tabletools/2.2.3/swf/copy_csv_xls.swf instead - or any other -> http://cdn.datatables.net/tabletools/ but it clears up some things :) – davidkonrad Apr 01 '15 at 19:54
  • I used this link -"sSwfPath": "//cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf" from your link, but still the same. Only first link, and as pdf not csv – ci_lover Apr 01 '15 at 20:02
  • hmmm. Can you upload it to a page where I can see it fail? – davidkonrad Apr 01 '15 at 20:03
  • I can give you link, but I have to tell you user and pass for my site and that be good to be in mail ot skype, not here. – ci_lover Apr 01 '15 at 20:04
  • I wrote to you. :) Write there.. :) – ci_lover Apr 01 '15 at 20:13