Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added printStyles option to print extension #7238

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions site/docs/extensions/print.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ group: extensions
toc: true
---

Adds a button to the toolbar for printing the table in a predefined configurable format.
Adds a button to the toolbar to print the table in a predefined configurable format.

## Usage

Expand All @@ -28,7 +28,7 @@ Adds a button to the toolbar for printing the table in a predefined configurable

- **Detail:**

Set true to show the Print button on the toolbar.
Set `true` to show the Print button on the toolbar.

- **Default:** `false`

Expand All @@ -40,7 +40,7 @@ Adds a button to the toolbar for printing the table in a predefined configurable

- **Detail:**

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
Set `true` to print table as sorted and filtered on UI. If `true` is set, explicit predefined print options for filtering and sorting (`printFilter`, `printSortOrder`, `printSortColumn`). They will be applied to data already filtered and sorted by UI controls. For printing data as filtered and sorted on UI - do not set these three options: `printFilter`, `printSortOrder`, `printSortColumn`.

- **Default:** `true`

Expand All @@ -52,14 +52,15 @@ Adds a button to the toolbar for printing the table in a predefined configurable

- **Detail:**

Receive html `<table>` element as string parameter, returns html string for printing. Used for styling and adding header or footer.
Receive HTML `<table>` element as a string parameter, returns HTML string for printing. This option is used for styling and adding a header or footer.

- **Default:**
{% highlight javascript %}
printPageBuilder: function(table) {
printPageBuilder: function(table, styles) {
return `
<html>
<head>
${styles}
<style type="text/css" media="print">
@page {
size: auto;
Expand Down Expand Up @@ -88,7 +89,7 @@ printPageBuilder: function(table) {
margin-right: 3%;
}
div.bs-table-print {
text-align:center;
text-align: center;
}
</style>
</head>
Expand All @@ -110,7 +111,7 @@ printPageBuilder: function(table) {

- **Detail:**

set column field name to sort by for the printed table
Set column field name to sort by for the printed table.

- **Default:** `undefined`

Expand All @@ -122,13 +123,25 @@ printPageBuilder: function(table) {

- **Detail:**

Valid values: 'asc', 'desc'. Relevant only if printSortColumn is set
Valid values: 'asc', 'desc'. Relevant only if `printSortColumn` is set.

- **Default:** `'asc'`

### printStyles

- **attribute:** `data-print-styles`

- **type:** `Array`

- **Detail:**

Add styles to the printed page, such as custom icons.

- **Default:** `[]`

### Icons

* print: `'glyphicon-print icon-share'`
* print: `'fa-print'`

## Column options

Expand All @@ -140,7 +153,7 @@ printPageBuilder: function(table) {

- **Detail:**

set value to filter the printed data by this column.
Set the value to filter the printed data by this column.

- **Default:** `undefined`

Expand All @@ -152,7 +165,7 @@ printPageBuilder: function(table) {

- **Detail:**

function(value, row, index) - returns a string. Formats the cell values for this column in the printed table. Function behavior is similar to the 'formatter' column option
A custom `function(value, row, index)` - returns a string. Formats the cell values for this column in the printed table. Function behavior is similar to the 'formatter' column option.

- **Default:** `undefined`

Expand All @@ -164,7 +177,7 @@ printPageBuilder: function(table) {

- **Detail:**

set true to hide this column in the printed page.
Set `true` to hide this column on the printed page.

- **Default:** `false`

Expand Down
37 changes: 28 additions & 9 deletions src/extensions/print/bootstrap-table-print.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

const Utils = $.fn.bootstrapTable.utils

function printPageBuilderDefault (table) {
function printPageBuilderDefault (table, styles) {
return `
<html>
<head>
${styles}
<style type="text/css" media="print">
@page {
size: auto;
Expand Down Expand Up @@ -36,7 +37,7 @@ function printPageBuilderDefault (table) {
margin-right: 3%;
}
div.bs-table-print {
text-align:center;
text-align: center;
}
</style>
</head>
Expand All @@ -61,8 +62,9 @@ Object.assign($.fn.bootstrapTable.defaults, {
printAsFilteredAndSortedOnUI: true,
printSortColumn: undefined,
printSortOrder: 'asc',
printPageBuilder (table) {
return printPageBuilderDefault(table)
printStyles: [],
printPageBuilder (table, styles) {
return printPageBuilderDefault(table, styles)
}
})

Expand Down Expand Up @@ -281,13 +283,30 @@ $.BootstrapTable = class extends $.BootstrapTable {
data = sortRows(data, this.options.printSortColumn, this.options.printSortOrder)
const table = buildTable(data, this.options.columns)
const newWin = window.open('')

const calculatedPrintPage = Utils.calculateObjectValue(this, this.options.printPageBuilder, [table], printPageBuilderDefault(table))
const printStyles = typeof this.options.printStyles === 'string' ?
this.options.printStyles.replace(/\[|\]| /g, '').toLowerCase().split(',') :
this.options.printStyles
const styles = printStyles.map(it =>
`<link rel="stylesheet" href="${it}" />`).join('')

const calculatedPrintPage = Utils.calculateObjectValue(this, this.options.printPageBuilder,
[table, styles], printPageBuilderDefault(table, styles))
const startPrint = () => {
newWin.focus()
newWin.print()
newWin.close()
}

newWin.document.write(calculatedPrintPage)
newWin.document.close()
newWin.focus()
newWin.print()
newWin.close()

if (printStyles.length) {
const links = document.getElementsByTagName('link')
const lastLink = links[links.length - 1]

lastLink.onload = startPrint
} else {
startPrint()
}
}
}
Loading