Skip to content

Commit

Permalink
support csv formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenFang committed May 7, 2016
1 parent 636922b commit acd12b1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});

Expand Down
2 changes: 2 additions & 0 deletions src/TableHeaderColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -154,6 +155,7 @@ TableHeaderColumn.defaultProps = {
dataAlign: 'left',
dataSort: false,
dataFormat: undefined,
csvFormat: undefined,
isKey: false,
editable: true,
onSort: undefined,
Expand Down
6 changes: 4 additions & 2 deletions src/csv_export_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 += ',';
});
Expand Down

0 comments on commit acd12b1

Please sign in to comment.