0.0.9 • Published 7 years ago

vanilla-datatables-exportable v0.0.9

Weekly downloads
8
License
MIT
Repository
github
Last release
7 years ago

Exportable

npm version license npm.io npm.io

A Vanilla-DataTables extension to allow for exporting to various formats.

NOTE: This extension is only compatable with v2.0.0 and above of Vanilla-DataTables.

DEMO


Install

Bower

bower install vanilla-datatables-exportable --save

npm

npm install vanilla-datatables-exportable --save

Browser

Grab the file from one of the CDNs and include them in your page:

<script src="https://unpkg.com/vanilla-datatables-exportable@latest/datatable.exportable.min.js" type="text/javascript"></script>

//or

<script src="https://cdn.jsdelivr.net/npm/vanilla-datatables-exportable@latest/datatable.exportable.min.js" type="text/javascript"></script>

You can replace latest with the required release number.

NOTE: Make sure the above js file is included AFTER the main Vanilla-DataTables js file.


Options

var datatable = new DataTable(myTable, {
    exportable: {
        // options go here
    }
});
OptionTypeDefaultEffect
typestring"json"The format to export to: json, csv or sql.
downloadbooleantrueExport the table data to chosen file format. Set to false to return a string.
escapeHTMLbooleantrueStrip HTML from the exported data.
filenamestring"datatable_export"The filename for the downloaded file.
skipColumnsarrayundefinedA collection of column indexes to omit from the exported data.
pagesnumber|arrayundefinedA single page or collection of pages to export. All other pages will be omitted.
lineDelimiterstring"\n"The line delimiter for CSV or text data.
columnDelimiterstring","The column delimiter for CSV or text data.
includeHeadingsbooleantrueSet the first line of the CSV string as the table headings.
tableNamestring"table"The MySQL table name for SQL data.
columnizebooleanfalseFormat the text string into columns.
paddingCharacterstring" "The character used to pad the columns when columnize is enabled.
replacerarray|functionundefinedThe JSON.stringify() replacer parameter. (More info).
spacenumber|stringThe JSON.stringify() space argument. (More info).
modalbooleantrueShows the print modal when calling the print() method. Set to false to just display a printable version.

Public Methods

Each of the following methods accepts an optional Object of options as it's only parameter to configure the exporter on the fly. These options will override the global options set above.

Methods are called using the exportable property of the current Vanilla-DataTables instance:

datatable.exportable.methodName();

export(options)

Export with given options.

// Export to json string
datatable.exportable.export({
    type: "json",
});

You can also override global options:

// Export to json string
datatable.exportable.export({
    type: "json",
    download: false,
    space: 2
});

toText()

Export to plain text file.

datatable.exportable.toText();

// Export to json string
datatable.exportable.toText({ download: false });


// Columnize the data
datatable.exportable.toText({ columnize: true, paddingCharacter: " | " });

// before:
Conan the Barbarian|1982|Conan|
Conan the Destroyer|1984|Conan|
The Terminator|1984|T-800 / Terminator|
Red Sonja|1985|Kalidor|
Commando|1985|Colonel John Matrix|
Raw Deal|1986|Kaminsky|
Predator|1987|Major Alan "Dutch" Schaefer|
The Running Man|1987|Ben Richards|
Red Heat|1988|Captain Ivan Danko|
Twins|1988|Julius Benedict|
Total Recall|1990|Douglas Quaid / Hauser|
Kindergarten Cop|1990|Detective John Kimble|
Terminator 2: Judgment Day|1991|T-800|
Last Action Hero|1993|Jack Slater / Himself|Also executive producer

// after
Conan the Barbarian        | 1982 | Conan                       |                        
Conan the Destroyer        | 1984 | Conan                       |                        
The Terminator             | 1984 | T-800 / Terminator          |                        
Red Sonja                  | 1985 | Kalidor                     |                        
Commando                   | 1985 | Colonel John Matrix         |                        
Raw Deal                   | 1986 | Kaminsky                    |                        
Predator                   | 1987 | Major Alan "Dutch" Schaefer |                        
The Running Man            | 1987 | Ben Richards                |                        
Red Heat                   | 1988 | Captain Ivan Danko          |                        
Twins                      | 1988 | Julius Benedict             |                        
Total Recall               | 1990 | Douglas Quaid / Hauser      |                        
Kindergarten Cop           | 1990 | Detective John Kimble       |                        
Terminator 2: Judgment Day | 1991 | T-800                       |                        
Last Action Hero           | 1993 | Jack Slater / Himself       | Also executive producer

toJSON()

Export to JSON file.

datatable.exportable.toJSON();

// Export to json string
datatable.exportable.toJSON({ download: false });

toCSV()

Export to CSV file.

datatable.exportable.toCSV();

// Export to csv string
datatable.exportable.toCSV({ download: false });

toSQL()

Export to SQL file.

datatable.exportable.toSQL();

// Export to sql string
datatable.exportable.toSQL({ download: false });

print()

Print the table. This method will open a new window/tab and display a printable version of the table.

datatable.exportable.print();

Changelog

v0.0.9

  • Added toText() method to export to text file/string;
  • Allow formatting (padding) of text string to columns

v0.0.7

  • Allow export of headings in CSV strings (#1)
  • Allow HTML to be stripped (#1)

v0.0.6

  • Added destroy() method
  • Fix bug causing filename concatenation

v0.0.5

  • Fix incorrect filename reference

v0.0.4

  • Fix toCSV() method not returning string.

v0.0.3

  • Incorrect file names.

v0.0.2

  • Add modal option for print() method.

v0.0.1

  • Initial commit.

Copyright © 2017 Karl Saunders | MIT license

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

0.0.10

7 years ago