Skip to content

Instantly share code, notes, and snippets.

@michaeljymsgutierrez
Last active July 9, 2018 07:19
Show Gist options
  • Save michaeljymsgutierrez/353f16072e6ff8e50dc2a5161a455dda to your computer and use it in GitHub Desktop.
Save michaeljymsgutierrez/353f16072e6ff8e50dc2a5161a455dda to your computer and use it in GitHub Desktop.
buttons.print.js Configurable Library Datatables.net
/*!
* Print button for Buttons and DataTables.
* 2015 SpryMedia Ltd - datatables.net/license
*/
(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD
define(["jquery", "datatables.net", "datatables.net-buttons"], function($) {
return factory($, window, document);
});
} else if (typeof exports === "object") {
// CommonJS
module.exports = function(root, $) {
if (!root) {
root = window;
}
if (!$ || !$.fn.dataTable) {
$ = require("datatables.net")(root, $).$;
}
if (!$.fn.dataTable.Buttons) {
require("datatables.net-buttons")(root, $);
}
return factory($, root, root.document);
};
} else {
// Browser
factory(jQuery, window, document);
}
})(function($, window, document, undefined) {
"use strict";
var DataTable = $.fn.dataTable;
var _link = document.createElement("a");
/**
* Convert a `link` tag's URL from a relative to an absolute address so it will
* work correctly in the popup window which has no base URL.
*
* @param {node} el Element to convert
*/
var _relToAbs = function(el) {
var url;
var clone = $(el).clone()[0];
var linkHost;
if (clone.nodeName.toLowerCase() === "link") {
_link.href = clone.href;
linkHost = _link.host;
// IE doesn't have a trailing slash on the host
// Chrome has it on the pathname
if (linkHost.indexOf("/") === -1 && _link.pathname.indexOf("/") !== 0) {
linkHost += "/";
}
clone.href =
_link.protocol + "//" + linkHost + _link.pathname + _link.search;
}
return clone.outerHTML;
};
DataTable.ext.buttons.print = {
className: "buttons-print",
text: function(dt) {
return dt.i18n("buttons.print", "Print");
},
action: function(e, dt, button, config) {
var data = dt.buttons.exportData(config.exportOptions);
var addRow = function(d, tag) {
var str = "<tr>";
try {
for (var i = 0, ien = d.length; i < ien; i++) {
str += "<" + tag + ">" + d[i] + "</" + tag + ">";
}
} catch (err) {}
return str + "</tr>";
};
// Construct a table for printing
var html = '<table class="' + dt.table().node().className + '">';
if (config.header) {
html += "<thead>" + addRow(data.header, "th") + "</thead>";
}
html += "<tbody>";
for (var i = 0, ien = data.body.length; i < ien; i++) {
html += addRow(data.body[i], "td");
}
html += "</tbody>";
if (config.footer) {
html += "<tfoot>" + addRow(data.footer, "th") + "</tfoot>";
}
/*
This line of code is implemented by CG
Data are sent directly through HTML5 LocalStorage
*/
if (config.customFooter === true) {
var _get_custom_footer = window.localStorage.getItem("customFooter");
if (_get_custom_footer) {
html += JSON.parse(_get_custom_footer);
}
}
// Open a new window for the printable table
var win = window.open("", "");
// var title = config.title;
var x = window.localStorage.getItem("report_headers");
var title = JSON.parse(x);
if (typeof title === "function") {
title = title();
}
// if ( title.indexOf( '*' ) !== -1 ) {
// title= title.replace( '*', $('title').text() );
// }
win.document.close();
// Inject the title and also a copy of the style and link tags from this
// document so the table can retain its base styling. Note that we have
// to use string manipulation as IE won't allow elements to be created
// in the host document and then appended to the new window.
var head = "<title>" + title.company_name + "</title>";
$("style, link").each(function() {
head += _relToAbs(this);
});
$(win.document.head).html(head);
if (title.profile_tab == "") {
// For Section without Tab Name
// Inject the table and other surrounding information
$(win.document.body).html(
'<span style="font-size:15px;">' +
title.store_name +
"</span><br/>" +
'<span style="font-size:10px;">' +
title.store_details +
"</span><br/>" +
'<span style="font-size:10px;">' +
title.date_print +
"</span>" +
'<img style="height:5%;position: absolute; width:30%;right:0px;margin-top:-50px;float:right;"src="' +
title.logo +
'"></img>' +
"<div>" +
config.message +
"</div>" +
html
);
} else {
// Inject the table and other surrounding information
$(win.document.body).html(
'<span style="font-size:15px;">' +
title.store_name +
"</span><br/>" +
'<span style="font-size:10px;">' +
title.store_details +
"</span><br/>" +
'<span style="font-size:10px;">' +
title.profile_tab +
"</span><br/>" +
'<span style="font-size:10px;">' +
title.date_print +
"</span>" +
'<img style="height:5%;position: absolute; width:30%;right:0px;margin-top:-50px;float:right;"src="' +
title.logo +
'"></img>' +
"<div>" +
config.message +
"</div>" +
html
);
}
if (config.customize) {
config.customize(win);
}
setTimeout(function() {
if (config.autoPrint) {
win.print(); // blocking - so close will not
win.close(); // execute until this is done
}
}, 250);
},
title: "*",
message: "",
exportOptions: {},
header: true,
footer: false,
autoPrint: true,
customize: null
};
return DataTable.Buttons;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment