Skip to content

Commit

Permalink
Added an option to exclude the header row from a CSV export.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Havill committed Jul 2, 2017
1 parent 018bf76 commit 16ba815
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ class BootstrapTable extends Component {
let result = {};

let { csvFileName } = this.props;
const { onExportToCSV, exportCSVSeparator } = this.props.options;
const { onExportToCSV, exportCSVSeparator, excludeCSVHeader } = this.props.options;
if (onExportToCSV) {
result = onExportToCSV();
} else {
Expand Down Expand Up @@ -1073,7 +1073,7 @@ class BootstrapTable extends Component {
csvFileName = csvFileName();
}

exportCSVUtil(result, keys, csvFileName, separator);
exportCSVUtil(result, keys, csvFileName, separator, excludeCSVHeader);
}

handleSearch = searchText => {
Expand Down Expand Up @@ -1531,6 +1531,7 @@ BootstrapTable.propTypes = {
firstPageTitle: PropTypes.string,
lastPageTitle: PropTypes.string,
searchDelayTime: PropTypes.number,
excludeCSVHeader: PropTypes.bool,
exportCSVText: PropTypes.string,
exportCSVSeparator: PropTypes.string,
insertText: PropTypes.string,
Expand Down Expand Up @@ -1683,6 +1684,7 @@ BootstrapTable.defaultProps = {
lastPageTitle: Const.LAST_PAGE_TITLE,
pageStartIndex: 1,
searchDelayTime: undefined,
excludeCSVHeader: false,
exportCSVText: Const.EXPORT_CSV_TEXT,
exportCSVSeparator: Const.DEFAULT_CSV_SEPARATOR,
insertText: Const.INSERT_BTN_TEXT,
Expand Down
9 changes: 5 additions & 4 deletions src/csv_export_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (Util.canUseDOM()) {
var saveAs = filesaver.saveAs;
}

function toString(data, keys, separator) {
function toString(data, keys, separator, excludeCSVHeader) {
let dataString = '';
if (data.length === 0) return dataString;

Expand All @@ -25,7 +25,8 @@ function toString(data, keys, separator) {
}
});

for (let i = 0; i <= rowCount; i++) {
const firstRow = excludeCSVHeader ? 1 : 0;
for (let i = firstRow; i <= rowCount; i++) {
dataString += headCells.map(x => {
if ((x.row + (x.rowSpan - 1)) === i) {
return x.header;
Expand Down Expand Up @@ -57,8 +58,8 @@ function toString(data, keys, separator) {
return dataString;
}

const exportCSV = function(data, keys, filename, separator) {
const dataString = toString(data, keys, separator);
const exportCSV = function(data, keys, filename, separator, excludeCSVHeader) {
const dataString = toString(data, keys, separator, excludeCSVHeader);
if (typeof window !== 'undefined') {
saveAs(new Blob([ dataString ],
{ type: 'text/plain;charset=utf-8' }),
Expand Down

0 comments on commit 16ba815

Please sign in to comment.