86 lines
3.4 KiB
JavaScript
Executable File
86 lines
3.4 KiB
JavaScript
Executable File
(function ($) {
|
|
|
|
$.fn.tableExport = function (options) {
|
|
|
|
var settings = $.extend({}, $.fn.tableExport.defaults, options),
|
|
rowD = $.fn.tableExport.rowDel, bootstrapClass, bootstrapTheme, bootstrapSpacing;
|
|
|
|
if (settings.bootstrap) {
|
|
bootstrapClass = $.fn.tableExport.bootstrap[0] + " ";
|
|
bootstrapTheme = $.fn.tableExport.bootstrap[1] + " ";
|
|
|
|
} else {
|
|
bootstrapClass = bootstrapTheme = bootstrapSpacing = "";
|
|
}
|
|
|
|
|
|
return this.each(function () {
|
|
var $el = $(this),
|
|
$rows = settings.headings ? $el.find('tr') : $el.find('tr:has(td)'),
|
|
fileName = settings.fileName === "id" ? $el.attr('id') : settings.fileName,
|
|
exporters = {
|
|
csv: function (rdel, name) {
|
|
rdel = '"' + rdel + '"';
|
|
var colD = '"' + $.fn.tableExport.csv.separator + '"',
|
|
dataURL = 'data:text/csv;charset=utf-8,' +
|
|
encodeURIComponent('"' + $rows.map(function (i, val) {
|
|
var $cols = $(val).find('th, td');
|
|
return $cols.map(function (i, val) {
|
|
return $(val).text().replace(/"/g, '""')
|
|
}).get().join(colD);
|
|
}).get().join(rdel) + '"'),
|
|
myFile = name + ".csv",
|
|
myContent = $.fn.tableExport.csv.buttonContent,
|
|
myClass = $.fn.tableExport.csv.defaultClass;
|
|
createButton(dataURL, myFile, myContent, myClass);
|
|
}
|
|
};
|
|
|
|
settings.formats.forEach(
|
|
function (key) {
|
|
exporters[key](rowD, fileName);
|
|
}
|
|
);
|
|
|
|
function checkCaption(exportButton) {
|
|
var $caption = $el.find('caption:not(.head)');
|
|
$caption.length ? $caption.append(exportButton) : $el.prepend('<caption class="' + bootstrapSpacing + settings.position + '">' + exportButton + '</caption>');
|
|
}
|
|
|
|
function createButton(dataURL, myFile, myContent, myClass) {
|
|
var exportButton = "<a href='" + dataURL + "' download='" + myFile + "' role='button' class='" + bootstrapClass + bootstrapTheme + myClass + "'>" + myContent + "</a>";
|
|
checkCaption(exportButton);
|
|
}
|
|
});
|
|
};
|
|
|
|
// Define the plugin default properties.
|
|
$.fn.tableExport.defaults = {
|
|
headings: true, // [Boolean], display table headings (th elements) in the first row, [default: true]
|
|
formats: ["csv"],
|
|
fileName: "id", // [id, name, String], filename for the downloaded file, [default: "export"]
|
|
bootstrap: true, // [String], additional button classes to add, [default: ""]
|
|
position: "bottom" // [top, bottom], position of the caption element relative to table, [default: "bottom"]
|
|
};
|
|
|
|
|
|
|
|
$.fn.tableExport.csv = {
|
|
defaultClass: "csv",
|
|
buttonContent: "Excel",
|
|
separator: ","
|
|
};
|
|
|
|
|
|
|
|
$.fn.tableExport.bootstrap = ["btn", "btn-default", "btn-toolbar"];
|
|
|
|
$.fn.tableExport.rowDel = "\r\n";
|
|
|
|
|
|
}(jQuery));
|
|
|
|
|
|
|
|
|