Skip to content

Commit 47d6eb0

Browse files
committed
fix #1188
1 parent 76f4795 commit 47d6eb0

File tree

6 files changed

+121
-6
lines changed

6 files changed

+121
-6
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* eslint react/prop-types: 0 */
2+
import React from 'react';
3+
4+
import BootstrapTable from 'react-bootstrap-table-next';
5+
import ToolkitProvider, { CSVExport } from 'react-bootstrap-table2-toolkit';
6+
import Code from 'components/common/code-block';
7+
import { productsGenerator } from 'utils/common';
8+
9+
const { ExportCSVButton } = CSVExport;
10+
const products = productsGenerator();
11+
12+
const columns = [{
13+
dataField: 'id',
14+
text: 'Product ID',
15+
footer: 'Footer 1'
16+
}, {
17+
dataField: 'name',
18+
text: 'Product Name',
19+
footer: '',
20+
footerFormatter: column => column.text
21+
}, {
22+
dataField: 'price',
23+
text: 'Product Price',
24+
footer: columnData => columnData.reduce((acc, item) => acc + item, 0)
25+
}];
26+
27+
const sourceCode = `\
28+
import BootstrapTable from 'react-bootstrap-table-next';
29+
import ToolkitProvider, { CSVExport } from 'react-bootstrap-table2-toolkit';
30+
31+
const { ExportCSVButton } = CSVExport;
32+
const columns = [{
33+
dataField: 'id',
34+
text: 'Product ID',
35+
footer: 'Footer 1'
36+
}, {
37+
dataField: 'name',
38+
text: 'Product Name',
39+
footer: 'Footer 2'
40+
}, {
41+
dataField: 'price',
42+
text: 'Product Price',
43+
footer: 'Footer 3'
44+
}];
45+
46+
<ToolkitProvider
47+
keyField="id"
48+
data={ products }
49+
columns={ columns }
50+
exportCSV={ {
51+
fileName: 'custom.csv',
52+
separator: '|',
53+
ignoreHeader: true,
54+
noAutoBOM: false,
55+
blobType: 'text/csv;charset=ansi'
56+
} }
57+
>
58+
{
59+
props => (
60+
<div>
61+
<ExportCSVButton { ...props.csvProps }>Export CSV!!</ExportCSVButton>
62+
<hr />
63+
<BootstrapTable { ...props.baseProps } />
64+
</div>
65+
)
66+
}
67+
</ToolkitProvider>
68+
`;
69+
70+
export default () => (
71+
<div>
72+
<ToolkitProvider
73+
keyField="id"
74+
data={ products }
75+
columns={ columns }
76+
exportCSV={ {
77+
ignoreFooter: false
78+
} }
79+
>
80+
{
81+
props => (
82+
<div>
83+
<ExportCSVButton { ...props.csvProps }>Export CSV!!</ExportCSVButton>
84+
<hr />
85+
<BootstrapTable { ...props.baseProps } />
86+
</div>
87+
)
88+
}
89+
</ToolkitProvider>
90+
<Code>{ sourceCode }</Code>
91+
</div>
92+
);

packages/react-bootstrap-table2-example/stories/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ import CSVColumnType from 'examples/csv/csv-column-type';
213213
import CustomCSVButton from 'examples/csv/custom-csv-button';
214214
import ExportCustomData from 'examples/csv/export-custom-data';
215215
import CustomCSV from 'examples/csv/custom-csv';
216+
import ExportTableFooter from 'examples/csv/export-footer';
216217

217218
// Column toggle
218219
import BasicColumnToggle from 'examples/column-toggle';
@@ -484,7 +485,8 @@ storiesOf('Export CSV', module)
484485
.add('CSV Column Type', () => <CSVColumnType />)
485486
.add('Custom CSV Button', () => <CustomCSVButton />)
486487
.add('Export Custom Data', () => <ExportCustomData />)
487-
.add('Custom CSV', () => <CustomCSV />);
488+
.add('Custom CSV', () => <CustomCSV />)
489+
.add('Export Table Footer', () => <ExportTableFooter />);
488490

489491
storiesOf('EmptyTableOverlay', module)
490492
.addDecorator(bootstrapStyle())

packages/react-bootstrap-table2-toolkit/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ Custom the csv file separator.
209209
#### ignoreHeader - [bool]
210210
Default is `false`. Give true to avoid to attach the csv header.
211211

212+
#### ignoreFooter - [bool]
213+
Default is `true`. Give `false` to attach the table footer if enabled.
214+
212215
#### noAutoBOM - [bool]
213216
Default is `true`.
214217

packages/react-bootstrap-table2-toolkit/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ToolkitProvider extends statelessDecorator(React.Component) {
2828
fileName: PropTypes.string,
2929
separator: PropTypes.string,
3030
ignoreHeader: PropTypes.bool,
31+
ignoreFooter: PropTypes.bool,
3132
noAutoBOM: PropTypes.bool,
3233
blobType: PropTypes.string,
3334
exportAll: PropTypes.bool,

packages/react-bootstrap-table2-toolkit/src/csv/exporter.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ export const getMetaInfo = columns =>
1212
export: column.csvExport === false ? false : true,
1313
row: Number(column.row) || 0,
1414
rowSpan: Number(column.rowSpan) || 1,
15-
colSpan: Number(column.colSpan) || 1
15+
colSpan: Number(column.colSpan) || 1,
16+
footer: column.footer,
17+
footerFormatter: column.footerFormatter
1618
}))
1719
.filter(_ => _.export);
1820

1921
export const transform = (
2022
data,
2123
meta,
22-
getValue,
24+
columns,
25+
_,
2326
{
2427
separator,
25-
ignoreHeader
28+
ignoreHeader,
29+
ignoreFooter
2630
}
2731
) => {
2832
const visibleColumns = meta.filter(m => m.export);
@@ -37,7 +41,7 @@ export const transform = (
3741
content += data
3842
.map((row, rowIndex) =>
3943
visibleColumns.map((m) => {
40-
let cellContent = getValue(row, m.field);
44+
let cellContent = _.get(row, m.field);
4145
if (m.formatter) {
4246
cellContent = m.formatter(cellContent, row, rowIndex, m.formatExtraData);
4347
}
@@ -47,6 +51,18 @@ export const transform = (
4751
return cellContent;
4852
}).join(separator)).join('\n');
4953

54+
if (!ignoreFooter) {
55+
content += '\n';
56+
content += visibleColumns.map((m, i) => {
57+
if (typeof m.footer === 'function') {
58+
const columnData = _.pluck(data, columns[i].dataField);
59+
return `"${m.footer(columnData, columns[i], i)}"`;
60+
} else if (m.footerFormatter) {
61+
return `"${m.footerFormatter(columns[i], i)}"`;
62+
}
63+
return `"${m.footer}"`;
64+
}).join(separator);
65+
}
5066
return content;
5167
};
5268

packages/react-bootstrap-table2-toolkit/src/op/csv.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const csvDefaultOptions = {
44
fileName: 'spreadsheet.csv',
55
separator: ',',
66
ignoreHeader: false,
7+
ignoreFooter: true,
78
noAutoBOM: true,
89
blobType: 'text/plain;charset=utf-8',
910
exportAll: true,
@@ -46,7 +47,7 @@ export default Base =>
4647
data = data.filter(row => !!selections.find(sel => row[keyField] === sel));
4748
}
4849

49-
const content = transform(data, meta, this._.get, options);
50+
const content = transform(data, meta, columns, this._, options);
5051
save(content, options);
5152
}
5253
};

0 commit comments

Comments
 (0)