Update: When I tried to submit to the official jQuery Plugin list, I found that my original name (table2CSV) had already been taken, so I have changed every occurrence of 'table2CSV' to 'TableCSVExport'
This plugin converts any set of columns of a HTML table to a CSV file that can then be viewed in a popup window, as a data string, or downloaded as a file.
I had a need for such a utility at my day job (at LectureTools Inc.) and searching netted me only one interesting possibility - the initial plugin that I have built upon. It appears that development on this stopped around June 2009, so I decided to 'fork' it and enhance it. You can find my project here, on my Github account; It has the plugin and a HTML test page.
Dependencies:
This plugin was developed using jQuery 1.6.2 and is not tested with other versions.
Usage:
Include script file however you need to for your particular project.
HTML:[code][/code] cakePHP 1.2: [code]echo $javascript->link("jquery.TableCSVExport.js");[/code]
cakePHP 1.3: [code]echo $html->script('jquery.TableCSVExport.js');[/code]
Then, simply execute the javascript line [code]jQuery('#table-id').TableCSVExport();[/code] to use the plugin with the default options.
Options:
separator (Default: ',') The character that separates entries in the resulting CSV file.
delivery (Default: 'popup')
The manner in which to return the CSV data file. Options are:
'popup' - opens a new window and displays the CSV data in a box where it can be copied from. 'value' - returns the CSV data as a single long string 'download' - triggers a download of a file containing the CSV data
header (Default: [])
An array containing strings of the column headers
Example: [code] jQuery('#table-id').TableCSVExport({header:['Name','Mon','Tue','Wed','Thr','Fri']}) [/code]
columns (Default: [])
An array containing strings consisting of the headers for the columns to export. This MUST be a subset of the array passed to the header option.
Example: [code] jQuery('#table-id').TableCSVExport({header:['Name','Mon','Tue','Wed','Thr','Fri'], columns:['Name','Mon']}) [/code]
This will export only the 'Name' and 'Mon' columns as CSV data.
-- WARNING --
When using the columns option to only export certain columns, you must also set the header array and the columns array must be a subset of the header array.
emptyValue (Default: '')
Column default value if the column is empty.
showHiddenRows (Default: false)
if it's true, will export rows with display: 'none'.
rowFilter (Default: '')
You can add jquery selector filter, to filter table rows.
Ex: rowFilter: '.myClass'
filename (Default: 'download.csv')
Choose what the downloaded filename will be when using the 'download' delivery. Currently only reliable in Chrome.
Example usage on a download button: $('#download-btn').click(function (e) { e.preventDefault(); $('#my-table').TableCSVExport({ delivery: 'download', filename: 'device-usage.csv' }); });
Important Notes:
The javascript .trim() function is called on each table datum, so be aware that beginning or terminating whitespace will be lost.
jquery.TableCSVExport.js is released under the MIT License and LectureTools Inc. retains all copyrights.