Skip to content
Open
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
1 change: 1 addition & 0 deletions .cspell-words.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
addrbar
akottr
apos
bootcss
borderless
bowser
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ restrictions:

* Please **do not** open issues or pull requests regarding the code in [`bootstrap-table-examples`](https://github.com/wenzhixin/bootstrap-table-examples) and [`extensions plugin dependence`](https://github.com/wenzhixin/bootstrap-table/tree/develop/src/extensions) (open them in their respective repositories), the dependence list:
* Table Editable: [x-editable](https://github.com/vitalets/x-editable)
* Table Export: [tableExport.jquery.plugin](https://github.com/hhurz/tableExport.jquery.plugin)
* Table Export: built-in exporter (no external plugin)
* Table Reorder-Columns: [jquery-ui](https://code.jquery.com/ui/) and [dragTable](https://github.com/akottr/dragtable/)
* Table Reorder-Rows: [tablednd](https://github.com/isocra/TableDnD)
* Table Resizable: [jquery-resizable-columns](https://github.com/dobtco/jquery-resizable-columns)
Expand Down
103 changes: 69 additions & 34 deletions cypress/extensions/export/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,41 +174,76 @@ module.exports = (theme = '') => {
cy.visit(`${baseUrl}export-method.html`)
.get('.fixed-table-body')
.should('exist')
.window().then(win => {
const $table = win.$('#table')

// Verify the method exists
expect(typeof $table.bootstrapTable).to.equal('function')

let tableExportCalled = false
const originalTableExport = win.$.fn.tableExport

try {
// eslint-disable-next-line no-unused-vars
win.$.fn.tableExport = function (_options) {
tableExportCalled = true
return this
}

let exportStarted = false
let exportSaved = false

$table.on('export-started.bs.table', () => {
exportStarted = true
})
$table.on('export-saved.bs.table', () => {
exportSaved = true
})

$table.bootstrapTable('exportTable', { type: 'csv' })

expect(exportStarted, 'export-started event should be triggered').to.be.true
expect(exportSaved, 'export-saved event should be triggered').to.be.true
expect(tableExportCalled, 'tableExport plugin should be called').to.be.true
} finally {
win.$.fn.tableExport = originalTableExport
}

cy.window().then(win => {
const $table = win.$('#table')
let exportStarted = false
let exportSaved = false
let downloadName = null
let capturedBlob = null

const createObjectURL = cy.stub(win.URL, 'createObjectURL').callsFake(blob => {
capturedBlob = blob
return 'blob:mock'
})

cy.stub(win.URL, 'revokeObjectURL').callsFake(() => {})
cy.stub(win.HTMLAnchorElement.prototype, 'click').callsFake(function () {
downloadName = this.download
})

$table.on('export-started.bs.table', () => {
exportStarted = true
})
$table.on('export-saved.bs.table', () => {
exportSaved = true
})

$table.bootstrapTable('exportTable', { type: 'csv' })

expect(exportStarted, 'export-started event should be triggered').to.be.true
expect(exportSaved, 'export-saved event should be triggered').to.be.true
expect(createObjectURL, 'a blob should be created').to.have.been.called
expect(downloadName, 'the file should be named table.csv').to.equal('table.csv')

return capturedBlob.text().then(text => {
expect(text).to.contain('ID,Name,Price')
expect(text).to.contain('1,Item 1,$1')
expect(text).to.contain('2,Item 2,$2')
})
})
})

// Methods: exportTable with an unsupported type
it('Test exportTable() method does not export unsupported types', () => {
cy.visit(`${baseUrl}export-method.html`)
.get('.fixed-table-body')
.should('exist')

cy.window().then(win => {
const $table = win.$('#table')
let exportStarted = false
let exportSaved = false

const createObjectURL = cy.stub(win.URL, 'createObjectURL').returns('blob:mock')

cy.stub(win.URL, 'revokeObjectURL').callsFake(() => {})
cy.stub(win.HTMLAnchorElement.prototype, 'click').callsFake(() => {})

$table.on('export-started.bs.table', () => {
exportStarted = true
})
$table.on('export-saved.bs.table', () => {
exportSaved = true
})

const result = $table.bootstrapTable('exportTable', { type: 'pdf' })

expect(result).to.equal(false)
expect(exportStarted, 'export-started event should not be triggered').to.be.false
expect(exportSaved, 'export-saved event should not be triggered').to.be.false
expect(createObjectURL).not.to.have.been.called
})
})

// Options: exportOptions.fileName as function
Expand Down
49 changes: 44 additions & 5 deletions site/src/pages/docs/extensions/export.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ group: extensions
toc: true
---

Use Plugin: [tableExport.jquery.plugin](https://github.com/hhurz/tableExport.jquery.plugin)
This extension now uses a built-in exporter (no external plugin required).

This is an important link to check out as some file types may require extra steps.
> **Note**
>
> Since the deprecated [tableExport.jquery.plugin](https://github.com/hhurz/tableExport.jquery.plugin) has been removed,
> the following types are **no longer supported**: `png`, `pdf`, `xlsx`, `doc` and `powerpoint`.
> Export / table values are read from the table data model instead of the rendered DOM, so the plugin specific
> options such as `ignoreColumn`, `csvSeparator`, `csvEnclosure`, `ignoreRow` and `displayTableName` are ignored.
> If you need one of the removed formats, please use a dedicated library (for example SheetJS or jsPDF) together with
> the `getData` method.

## Usage

Expand Down Expand Up @@ -44,6 +51,14 @@ This is an important link to check out as some file types may require extra step

Export data type, support: `'basic'`, `'all'`, `'selected'`.

* `'basic'`: exports the rows of the current page only.
* `'all'`: exports all rows of all pages.
* `'selected'`: exports the selected rows only, nothing is exported if no row is selected.

The selected columns are defined by the `forceExport`/`forceHide` and `visible` column options, the checkbox/radio
state column is always skipped. Cell values are the formatter results with their HTML tags stripped,
mirroring what is displayed in the table.

- **Default:** `basic`

### exportFooter
Expand All @@ -54,7 +69,8 @@ This is an important link to check out as some file types may require extra step

- **Detail:**

Set `true` to export the table footer.
Set `true` to export the table footer. The footer is appended as the last row of the exported file,
using the `footerFormatter` of every exported column.

- **Default:** `false`

Expand All @@ -66,7 +82,17 @@ This is an important link to check out as some file types may require extra step

- **Detail:**

Export [options](https://github.com/hhurz/tableExport.jquery.plugin#options) of `tableExport.jquery.plugin`
Options for the built-in exporter:

- `fileName`: `String` or `Function`, the filename without extension (default `table`).
- `csvDelimiter`: `String`, the delimiter used by the `csv` and `excel` types (default `,`).
- `tableName`: `String`, the table name used by the `sql` and `xml` types (default `table`).

Options passed to the `exportTable` method are merged into these options, for example:

```js
$('#table').bootstrapTable('exportTable', { type: 'csv', fileName: 'my-report' })
```

`exportOptions.fileName` can be a string or a function, for example:

Expand All @@ -86,7 +112,20 @@ This is an important link to check out as some file types may require extra step

- **Detail:**

Export types, support types: `['json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf']`.
Built-in export types: `['json', 'xml', 'csv', 'txt', 'sql', 'excel']`.
Note: `png`, `xlsx`, `pdf`, `doc`, `powerpoint` are not supported by the built-in exporter.

* `csv`/`txt`: values are joined with the delimiter, CSV files start with a UTF-8 BOM and use `CRLF` line endings
so that spreadsheet applications keep the encoding.
* `json`: each row is an object keyed by the column `field` (not by the column title).
* `xml`: the root element comes from `exportOptions.tableName`, the row elements are `<row>` children,
column tags are derived from the column `field` and sanitized to valid XML names.
* `sql`: generates a single `INSERT INTO` statement, no file is created when there is no row to export.
* `excel`: **generates CSV content with a `.xls` extension**, it is not a real Excel workbook.
Excel shows a "format differs from the file extension" warning when opening such a file.

Unsupported types are skipped: `exportTable` returns `false`, logs a warning and neither exports a file
nor triggers the `export-started`/`export-saved` events.

- **Default:** `['json', 'xml', 'csv', 'txt', 'sql', 'excel']`

Expand Down
53 changes: 42 additions & 11 deletions site/src/pages/zh-cn/docs/extensions/export.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ group: extensions
toc: true
---

数据导出扩展可以将表格数据导出为多种格式,包括 Excel、PDF、CSV 等。
数据导出扩展可以将表格数据导出为多种格式,包括 JSON、XML、CSV、TXT、SQL 和 Excel。

## 依赖插件
本扩展已内置导出实现,不再依赖任何外部插件。

本扩展依赖 [tableExport.jquery.plugin](https://github.com/hhurz/tableExport.jquery.plugin)。

> **注意**:某些导出格式可能需要额外的配置或依赖库,请参考上述文档进行配置。
> **注意**
>
> 已废弃的 [tableExport.jquery.plugin](https://github.com/hhurz/tableExport.jquery.plugin) 已被移除,
> 因此 `png`、`pdf`、`xlsx`、`doc`、`powerpoint` 这些格式**不再支持**。
> 导出内容取自表格的数据模型而非渲染后的 DOM,所以 `ignoreColumn`、`csvSeparator`、`csvEnclosure`、
> `ignoreRow`、`displayTableName` 等旧插件选项不再生效。
> 如果确实需要上述格式,请使用 SheetJS、jsPDF 等第三方库配合 `getData` 方法自行实现。

## 用法

Expand Down Expand Up @@ -48,8 +52,11 @@ toc: true

设置导出数据的范围:
* `'basic'`:仅导出当前页数据
* `'all'`:导出所有数据
* `'selected'`:仅导出选中的行
* `'all'`:导出所有页的全部数据
* `'selected'`:仅导出选中的行,未选中任何行时不导出

导出的列由 `visible`、`forceExport`、`forceHide` 列选项决定,复选框/单选框状态列始终会被跳过。
单元格取值为 formatter 的处理结果并去除其中的 HTML 标签,即与表格中显示的内容一致。

- **默认值:** `basic`

Expand All @@ -61,7 +68,8 @@ toc: true

- **详情:**

是否同时导出表格页脚。设置为 `true` 时,表格的页脚行也会包含在导出的文件中。
是否同时导出表格页脚。设置为 `true` 时,页脚会作为最后一行追加到导出的文件中,
每个导出列的值由该列的 `footerFormatter` 决定。

- **默认值:** `false`

Expand All @@ -73,7 +81,17 @@ toc: true

- **详情:**

传递给 `tableExport.jquery.plugin` 的[导出配置选项](https://github.com/hhurz/tableExport.jquery.plugin#options)。
内置导出器的配置选项:

- `fileName`:`String` 或 `Function`,导出文件名(不含扩展名,默认为 `table`)。
- `csvDelimiter`:`String`,`csv` 与 `excel` 类型使用的分隔符(默认为 `,`)。
- `tableName`:`String`,`sql` 与 `xml` 类型使用的表名(默认为 `table`)。

调用 `exportTable` 方法时传入的参数会合并到这些选项中,例如:

```js
$('#table').bootstrapTable('exportTable', { type: 'csv', fileName: 'my-report' })
```

其中 `exportOptions.fileName` 可以是字符串或函数,用于设置导出文件的名称:

Expand All @@ -93,8 +111,21 @@ toc: true

- **详情:**

设置可用的导出格式列表。支持的格式包括:
`json`、`xml`、`png`、`csv`、`txt`、`sql`、`doc`、`excel`、`xlsx`、`pdf` 等。
设置可用的导出格式列表。内置支持的格式包括:
`json`、`xml`、`csv`、`txt`、`sql`、`excel`。
注意:`png`、`xlsx`、`pdf`、`doc`、`powerpoint` 不再支持。

* `csv`/`txt`:按分隔符拼接各行,CSV 文件会写入 UTF-8 BOM 并使用 `CRLF` 换行,
以保证表格软件不会错误识别编码。
* `json`:每行是一个以列 `field` 为键的对象(而非以列标题为键)。
* `xml`:根元素来自 `exportOptions.tableName`,每行是一个 `<row>` 节点,
子节点名称由列 `field` 生成并会被处理为合法的 XML 名称。
* `sql`:生成一条 `INSERT INTO` 语句,若没有可导出的行则不生成文件。
* `excel`:**生成的是 CSV 内容,扩展名为 `.xls`**,并不是真正的 Excel 文件,
打开时 Excel 会提示"文件格式与扩展名不一致"。

不支持的格式会被跳过:`exportTable` 返回 `false` 并在控制台输出警告,
既不会导出文件,也不会触发 `export-started`/`export-saved` 事件。

- **默认值:** `['json', 'xml', 'csv', 'txt', 'sql', 'excel']`

Expand Down
Loading
Loading