From acd12b178d8dd46f1e06e6051ff16f829b6fff78 Mon Sep 17 00:00:00 2001 From: AllenFang Date: Sat, 7 May 2016 20:16:32 +0800 Subject: [PATCH] support csv formatter --- src/BootstrapTable.js | 5 ++++- src/TableHeaderColumn.js | 2 ++ src/csv_export_util.js | 6 ++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/BootstrapTable.js b/src/BootstrapTable.js index 68b390ee2..295ce7581 100644 --- a/src/BootstrapTable.js +++ b/src/BootstrapTable.js @@ -622,7 +622,10 @@ class BootstrapTable extends Component { const keys = []; this.props.children.map(function(column) { if (column.props.hidden === false) { - keys.push(column.props.dataField); + keys.push({ + field: column.props.dataField, + format: column.props.csvFormat + }); } }); diff --git a/src/TableHeaderColumn.js b/src/TableHeaderColumn.js index 4d552d525..6a0e53c4f 100644 --- a/src/TableHeaderColumn.js +++ b/src/TableHeaderColumn.js @@ -120,6 +120,7 @@ TableHeaderColumn.propTypes = { dataSort: PropTypes.bool, onSort: PropTypes.func, dataFormat: PropTypes.func, + csvFormat: PropTypes.func, isKey: PropTypes.bool, editable: PropTypes.any, hidden: PropTypes.bool, @@ -154,6 +155,7 @@ TableHeaderColumn.defaultProps = { dataAlign: 'left', dataSort: false, dataFormat: undefined, + csvFormat: undefined, isKey: false, editable: true, onSort: undefined, diff --git a/src/csv_export_util.js b/src/csv_export_util.js index fdfa1f803..12f4f891a 100644 --- a/src/csv_export_util.js +++ b/src/csv_export_util.js @@ -13,11 +13,13 @@ function toString(data, keys) { let dataString = ''; if (data.length === 0) return dataString; - dataString += keys.join(',') + '\n'; + dataString += keys.map(x => x.field).join(',') + '\n'; data.map(function(row) { keys.map(function(col, i) { - const cell = typeof row[col] !== 'undefined' ? ('"' + row[col] + '"') : ''; + const { field, format } = col; + const value = typeof format !== 'undefined' ? format(row[field]) : row[field]; + const cell = typeof value !== 'undefined' ? ('"' + value + '"') : ''; dataString += cell; if (i + 1 < keys.length) dataString += ','; });