(function (global, factory) { if (typeof define === "function" && define.amd) { define([], factory); } else if (typeof exports !== "undefined") { factory(); } else { var mod = { exports: {} }; factory(); global.bootstrapTablePrint = mod.exports; } })(this, function () { 'use strict'; (function ($) { 'use strict'; var sprintf = $.fn.bootstrapTable.utils.sprintf; function printPageBuilderDefault(table) { return '' + '' + 'Print Table' + '

Printed on: ' + new Date() + '

' + '
' + table + "
"; } $.extend($.fn.bootstrapTable.defaults, { showPrint: false, printAsFilteredAndSortedOnUI: true, //boolean, when true - print table as sorted and filtered on UI. //Please note that if true is set, along with explicit predefined print options for filtering and sorting (printFilter, printSortOrder, printSortColumn)- then they will be applied on data already filtered and sorted by UI controls. //For printing data as filtered and sorted on UI - do not set these 3 options:printFilter, printSortOrder, printSortColumn printSortColumn: undefined, //String, set column field name to be sorted by printSortOrder: 'asc', //String: 'asc' , 'desc' - relevant only if printSortColumn is set printPageBuilder: function printPageBuilder(table) { return printPageBuilderDefault(table); } // function, receive html element as string, returns html string for printing. by default delegates to function printPageBuilderDefault(table). used for styling and adding header or footer }); $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, { printFilter: undefined, //set value to filter by in print page printIgnore: false, //boolean, set true to ignore this column in the print page printFormatter: undefined //function(value, row, index), formats the cell value for this column in the printed table. Function behaviour is similar to the 'formatter' column option }); $.extend($.fn.bootstrapTable.defaults.icons, { print: 'glyphicon-print icon-share' }); var BootstrapTable = $.fn.bootstrapTable.Constructor, _initToolbar = BootstrapTable.prototype.initToolbar; BootstrapTable.prototype.initToolbar = function () { this.showToolbar = this.showToolbar || this.options.showPrint; _initToolbar.apply(this, Array.prototype.slice.apply(arguments)); if (this.options.showPrint) { var that = this, $btnGroup = this.$toolbar.find('>.btn-group'), $print = $btnGroup.find('button.bs-print'); if (!$print.length) { $print = $([''].join('')).appendTo($btnGroup); $print.click(function () { function formatValue(row, i, column) { var value = row[column.field]; if (typeof column.printFormatter === 'function') { return column.printFormatter.apply(column, [value, row, i]); } else { return typeof value === 'undefined' ? "-" : value; } } function buildTable(data, columnsArray) { var html = ['
']; for (var k = 0; k < columnsArray.length; k++) { var columns = columnsArray[k]; html.push(''); for (var h = 0; h < columns.length; h++) { if (!columns[h].printIgnore) { html.push('%s', columns[h].title)); } } html.push(''); } html.push(''); for (var i = 0; i < data.length; i++) { html.push(''); for (var l = 0; l < columnsArray.length; l++) { var columns = columnsArray[l]; for (var j = 0; j < columns.length; j++) { if (!columns[j].printIgnore && columns[j].field) { html.push(''); } } } html.push(''); } html.push('
', formatValue(data[i], i, columns[j]), '
'); return html.join(''); } function sortRows(data, colName, sortOrder) { if (!colName) { return data; } var reverse = sortOrder != 'asc'; reverse = -(+reverse || -1); return data.sort(function (a, b) { return reverse * a[colName].localeCompare(b[colName]); }); } function filterRow(row, filters) { for (var index = 0; index < filters.length; ++index) { if (row[filters[index].colName] != filters[index].value) { return false; } } return true; } function filterRows(data, filters) { return data.filter(function (row) { return filterRow(row, filters); }); } function getColumnFilters(columns) { return !columns || !columns[0] ? [] : columns[0].filter(function (col) { return col.printFilter; }).map(function (col) { return { colName: col.field, value: col.printFilter }; }); } var doPrint = function doPrint(data) { data = filterRows(data, getColumnFilters(that.options.columns)); data = sortRows(data, that.options.printSortColumn, that.options.printSortOrder); var table = buildTable(data, that.options.columns); var newWin = window.open(""); newWin.document.write(that.options.printPageBuilder.call(this, table)); newWin.print(); newWin.close(); }; doPrint(that.options.printAsFilteredAndSortedOnUI ? that.getData() : that.options.data.slice(0)); }); } } }; })(jQuery); });