From d810c9c51ab036c43eaa76a46617c493f4f430c6 Mon Sep 17 00:00:00 2001 From: AllenFang Date: Mon, 18 Sep 2017 23:42:59 +0800 Subject: [PATCH] fix #1613 --- src/BootstrapTable.js | 1 + src/Const.js | 4 +++- src/TableHeaderColumn.js | 2 ++ src/csv_export_util.js | 8 +++++--- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/BootstrapTable.js b/src/BootstrapTable.js index 3fb929fd7..66e9fc153 100644 --- a/src/BootstrapTable.js +++ b/src/BootstrapTable.js @@ -1104,6 +1104,7 @@ class BootstrapTable extends Component { column.props.hidden === false)) { keys.push({ field: column.props.dataField, + type: column.props.csvFieldType, format: column.props.csvFormat, extraData: column.props.csvFormatExtraData, header: column.props.csvHeader || column.props.dataField, diff --git a/src/Const.js b/src/Const.js index 3fc57ad2a..9c0b9d8c0 100644 --- a/src/Const.js +++ b/src/Const.js @@ -59,7 +59,9 @@ const CONST_VAR = { REMOTE_SEARCH: 'search', REMOTE_EXPORT_CSV: 'exportCSV', INSERT_FAIL_INDICATOR: 'Validation errors, please check!', - DEFAULT_CSV_SEPARATOR: ',' + DEFAULT_CSV_SEPARATOR: ',', + CSV_STRING_TYPE: 'string', + CSV_NUMBER_TYPE: 'number' }; CONST_VAR.REMOTE = {}; diff --git a/src/TableHeaderColumn.js b/src/TableHeaderColumn.js index b6fd04999..2a81b7884 100644 --- a/src/TableHeaderColumn.js +++ b/src/TableHeaderColumn.js @@ -263,6 +263,7 @@ TableHeaderColumn.propTypes = { dataFormat: PropTypes.func, csvFormat: PropTypes.func, csvHeader: PropTypes.string, + csvFieldType: PropTypes.oneOf([ Const.CSV_STRING_TYPE, Const.CSV_NUMBER_TYPE ]), isKey: PropTypes.bool, editable: PropTypes.any, hidden: PropTypes.bool, @@ -323,6 +324,7 @@ TableHeaderColumn.defaultProps = { dataFormat: undefined, csvFormat: undefined, csvHeader: undefined, + csvFieldType: Const.CSV_STRING_TYPE, isKey: false, editable: true, onSort: undefined, diff --git a/src/csv_export_util.js b/src/csv_export_util.js index f70461bed..c1c614b23 100644 --- a/src/csv_export_util.js +++ b/src/csv_export_util.js @@ -3,6 +3,7 @@ /* eslint no-var: 0 */ /* eslint no-unused-vars: 0 */ import Util from './util'; +import Const from './Const'; if (Util.canUseDOM()) { const filesaver = require('./filesaver'); @@ -45,9 +46,10 @@ function toString(data, keys, separator, excludeCSVHeader) { data.map(function(row) { keys.map(function(col, i) { - const { field, format, extraData } = col; - const value = typeof format !== 'undefined' ? format(row[field], row, extraData) : row[field]; - const cell = typeof value !== 'undefined' ? ('"' + value + '"') : ''; + const { field, format, extraData, type } = col; + let value = typeof format !== 'undefined' ? format(row[field], row, extraData) : row[field]; + value = type === Const.CSV_NUMBER_TYPE ? Number(value) : `"${value}"`; + const cell = typeof value !== 'undefined' ? value : ''; dataString += cell; if (i + 1 < keys.length) dataString += separator; });