Skip to content

Commit acd12b1

Browse files
committed
support csv formatter
1 parent 636922b commit acd12b1

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/BootstrapTable.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,10 @@ class BootstrapTable extends Component {
622622
const keys = [];
623623
this.props.children.map(function(column) {
624624
if (column.props.hidden === false) {
625-
keys.push(column.props.dataField);
625+
keys.push({
626+
field: column.props.dataField,
627+
format: column.props.csvFormat
628+
});
626629
}
627630
});
628631

src/TableHeaderColumn.js

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ TableHeaderColumn.propTypes = {
120120
dataSort: PropTypes.bool,
121121
onSort: PropTypes.func,
122122
dataFormat: PropTypes.func,
123+
csvFormat: PropTypes.func,
123124
isKey: PropTypes.bool,
124125
editable: PropTypes.any,
125126
hidden: PropTypes.bool,
@@ -154,6 +155,7 @@ TableHeaderColumn.defaultProps = {
154155
dataAlign: 'left',
155156
dataSort: false,
156157
dataFormat: undefined,
158+
csvFormat: undefined,
157159
isKey: false,
158160
editable: true,
159161
onSort: undefined,

src/csv_export_util.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ function toString(data, keys) {
1313
let dataString = '';
1414
if (data.length === 0) return dataString;
1515

16-
dataString += keys.join(',') + '\n';
16+
dataString += keys.map(x => x.field).join(',') + '\n';
1717

1818
data.map(function(row) {
1919
keys.map(function(col, i) {
20-
const cell = typeof row[col] !== 'undefined' ? ('"' + row[col] + '"') : '';
20+
const { field, format } = col;
21+
const value = typeof format !== 'undefined' ? format(row[field]) : row[field];
22+
const cell = typeof value !== 'undefined' ? ('"' + value + '"') : '';
2123
dataString += cell;
2224
if (i + 1 < keys.length) dataString += ',';
2325
});

0 commit comments

Comments
 (0)