Skip to content

Commit 056ea3f

Browse files
authored
Merge pull request #1486 from jogoodma/custom-tsv-csv-button
Custom CSV/TSV download button
2 parents 6a5087f + 5a26648 commit 056ea3f

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* eslint max-len: 0 */
2+
/* eslint no-unused-vars: 0 */
3+
/* eslint no-alert: 0 */
4+
import React, { PropTypes } from 'react';
5+
import DropdownButton from 'react-bootstrap/lib/DropdownButton';
6+
import MenuItem from 'react-bootstrap/lib/MenuItem';
7+
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
8+
9+
// Initialize the product array.
10+
const products = [];
11+
function addProducts(quantity) {
12+
const startId = products.length;
13+
for (let i = 0; i < quantity; i++) {
14+
const id = startId + i;
15+
products.push({
16+
id: id,
17+
name: 'Item name ' + id,
18+
price: 2100 + i
19+
});
20+
}
21+
}
22+
addProducts(5);
23+
24+
/*
25+
Custom Download button that uses the DropdownButton component
26+
from react-bootstrap.
27+
*/
28+
function DownloadButton({ bootStrapTableOnClick, onSelect }) {
29+
/*
30+
This handler is fired by the react-bootstrap DropdownButton component
31+
when the user has selected an export option (tsv or csv). It passes
32+
in the eventKey from the MenuItem that was selected by the user.
33+
*/
34+
function handleOnSelect(eventKey) {
35+
/*
36+
Fire the user specified action and pass the key
37+
and react-bootstrap-table onClick function.
38+
*/
39+
onSelect(eventKey, bootStrapTableOnClick);
40+
}
41+
return (
42+
<div>
43+
<DropdownButton bsStyle='default' title='Export' id='export' onSelect={ handleOnSelect }>
44+
<MenuItem eventKey='csv'>CSV</MenuItem>
45+
<MenuItem eventKey='tsv'>TSV</MenuItem>
46+
</DropdownButton>
47+
</div>
48+
);
49+
}
50+
DownloadButton.propTypes = {
51+
// The onClick handler received from react-bootstrap-table.
52+
bootStrapTableOnClick: PropTypes.func.isRequired,
53+
// A handler that fires when the user selects an export option.
54+
onSelect: PropTypes.func.isRequired
55+
};
56+
57+
class CustomCSVOrTSVButtonTable extends React.Component {
58+
constructor(props) {
59+
super(props);
60+
this.state = {
61+
extension: 'csv',
62+
separator: ','
63+
};
64+
this.updateExportOpts = this.updateExportOpts.bind(this);
65+
this.getFilename = this.getFilename.bind(this);
66+
}
67+
68+
getFilename() {
69+
return 'spreadsheet' + '.' + this.state.extension;
70+
}
71+
72+
updateExportOpts(format, tableOnClick) {
73+
/*
74+
Here we pass the react-bootstrap-table onClick event handler to setState
75+
to ensure that it gets called after the state has been mutated.
76+
*/
77+
if (format === 'tsv') {
78+
this.setState({ extension: 'tsv', separator: '\t' }, tableOnClick);
79+
} else {
80+
this.setState({ extension: 'csv', separator: ',' }, tableOnClick);
81+
}
82+
}
83+
84+
render() {
85+
const options = {
86+
exportCSVBtn: (onClick) => <DownloadButton bootStrapTableOnClick={ onClick } onSelect={ this.updateExportOpts } />,
87+
exportCSVSeparator: this.state.separator
88+
};
89+
return (
90+
<BootstrapTable data={ products } options={ options } csvFileName={ this.getFilename } exportCSV={ true }>
91+
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
92+
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
93+
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
94+
</BootstrapTable>
95+
);
96+
}
97+
}
98+
export default CustomCSVOrTSVButtonTable;

examples/js/custom/demo.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import DefaultCustomShowSelectButtonTable from './show-only-select-button/defaul
1717
import FullyCustomShowSelectButtonTable from './show-only-select-button/fully-custom-show-select-button';
1818
import DefaultCustomExportCSVButtonTable from './csv-button/default-custom-csv-button';
1919
import FullyCustomExportCSVButtonTable from './csv-button/fully-custom-csv-button';
20+
import CustomCSVOrTSVButtonTable from './csv-button/custom-csv-or-tsv-button';
2021
import DefaultCustomClearButtonTable from './search/default-custom-search-clear-button';
2122
import FullyCustomClearButtonTable from './search/fully-custom-search-clear-button';
2223
import DefaultCustomSearchFieldTable from './search/default-custom-search-field';
@@ -88,6 +89,10 @@ class Demo extends React.Component {
8889
{ renderLinks('custom/csv-button/fully-custom-csv-button.js') }
8990
<FullyCustomExportCSVButtonTable/>
9091
</Panel>
92+
<Panel header={ 'Custom CSV or TSV export button.' }>
93+
{ renderLinks('custom/csv-button/custom-csv-or-tsv-button.js') }
94+
<CustomCSVOrTSVButtonTable />
95+
</Panel>
9196
<Panel header={ 'Default Custom for Show Only Select Button Demo' }>
9297
{ renderLinks('custom/show-only-select-button/default-custom-show-select-button.js') }
9398
<DefaultCustomShowSelectButtonTable/>

0 commit comments

Comments
 (0)