jquery php implements the method of exporting datatables plug-in data to excel, jquerydatatables_PHP tutorial

WBOY
Release: 2016-07-13 09:47:08
Original
1135 people have browsed it

jquery php implements the method of exporting datatables plug-in data to excel, jquerydatatables

This article describes the example of jquery php implementing the method of exporting datatables plug-in data to excel. Share it with everyone for your reference. The details are as follows:

DataTables is a jQuery table plug-in. This is a highly flexible tool based on progressive enhancements that will add advanced interactive controls and support for any HTML form. Main features:

1. Automatic paging
2. Instant table data filtering
3. Data sorting and automatic detection of data types
4. Automatically handle column width
5. Styles can be customized through CSS
6. Support hidden columns
7. Easy to use
8. Scalability and flexibility
9. Internationalization
10. Dynamically create tables
11. Free

Plug-in address http://www.datatables.net/

However, it is a pity that the official website table data export method uses the tabletools plug-in, which uses flash to export data, and does not support Chinese data. By searching the official API and information, I found the method of exporting data using jquery and php.

Javascript function to export data

function table2csv(oTable, exportmode, tableElm) { 
    var csv = ''; 
    var headers = []; 
    var rows = []; 
    // Get header names 
    $(tableElm+' thead').find('th').each(function() { 
      var $th = $(this); 
      var text = $th.text(); 
      var header = '"' + text + '"'; 
      // headers.push(header); // original code 
      if(text != "") headers.push(header); // actually datatables seems to copy my original headers so there ist an amount of TH cells which are empty 
    }); 
    csv += headers.join(',') + "\n"; 
    // get table data 
    if (exportmode == "full") { // total data 
      var total = oTable.fnSettings().fnRecordsTotal() 
      for(i = 0; i < total; i++) { 
        var row = oTable.fnGetData(i); 
        row = strip_tags(row); 
        rows.push(row); 
      } 
    } else { // visible rows only 
      $(tableElm+' tbody tr:visible').each(function(index) { 
        var row = oTable.fnGetData(this); 
        row = strip_tags(row); 
        rows.push(row); 
      }) 
    } 
    csv += rows.join("\n"); 
    // if a csv div is already open, delete it 
    if($('.csv-data').length) $('.csv-data').remove(); 
    // open a div with a download link 
    $('body').append('<div class="csv-data"><form enctype="multipart/form-data" method="post" action="/csv.php"><textarea class="form" name="csv">'+csv+'</textarea><input type="submit" class="submit" value="Download as file" /></form></div>'); 
} 
function strip_tags(html) { 
  var tmp = document.createElement("div"); 
  tmp.innerHTML = html; 
  return tmp.textContent||tmp.innerText; 
} 

Copy after login

The function supports exporting all data and current page data

// export only what is visible right now (filters & paginationapplied)
$('#export_visible').click(function(event) {  
   var oTable; 
   oTable= $('#spdata').dataTable(); 
   event.preventDefault(); 
   table2csv(oTable, 'visible', '#spdata'); })
   // export all table data 
$('#export_all').click(function(event) {  
  var oTable; 
  oTable= $('#spdata').dataTable(); 
  event.preventDefault(); 
 table2csv(oTable, 'full', '#spdata'); }) 
Copy after login

Where #spdata is the id of the table

Backend php export excel code

header("Content-Type: application/vnd.ms-execl");  
header("Content-Disposition: attachment; filename=myExcel.csv");  
header("Pragma: no-cache");  
header("Expires: 0");  
$buffer = $_POST['csv'];     
$buffer=str_replace(",",",\t",$buffer); 
$buffer=mb_convert_encoding($buffer,"GB2312","UTF-8");  
echo $buffer;
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1027494.htmlTechArticlejquery php implements the method of exporting datatables plug-in data to excel, jquerydatatables This article describes the jquery php method of exporting datatables plug-in data method to excel. Share it with everyone...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!