This commit is contained in:
AllenFang
2018-09-01 16:26:48 +08:00
parent 37db43f5a7
commit d136ec3197
6 changed files with 40 additions and 8 deletions

View File

@@ -123,4 +123,7 @@ Default is `false`. Give true to avoid to attach the csv header.
Default is `true`.
#### exportAll - [bool]
Default is `true`. `false` will only export current data which display on table.
Default is `true`. `false` will only export current data which display on table.
#### onlyExportSelection - [bool]
Default is `false`. `true` will only export the data which is selected.

View File

@@ -26,7 +26,9 @@ class ToolkitProvider extends statelessDrcorator(React.Component) {
fileName: PropTypes.string,
separator: PropTypes.string,
ignoreHeader: PropTypes.bool,
noAutoBOM: PropTypes.bool
noAutoBOM: PropTypes.bool,
exportAll: PropTypes.bool,
onlyExportSelection: PropTypes.bool
})
])
}

View File

@@ -11,7 +11,7 @@ const ExportCSVButton = (props) => {
return (
<button
type="button"
onClick={ onExport }
onClick={ () => onExport() }
{ ...rest }
>
{ children }

View File

@@ -5,13 +5,14 @@ const csvDefaultOptions = {
separator: ',',
ignoreHeader: false,
noAutoBOM: true,
exportAll: true
exportAll: true,
onlyExportSelection: false
};
export default Base =>
class CSVOperation extends Base {
handleExportCSV = () => {
const { columns, exportCSV } = this.props;
handleExportCSV = (source) => {
const { columns, exportCSV, keyField } = this.props;
const meta = getMetaInfo(columns);
const options = exportCSV === true ?
csvDefaultOptions :
@@ -20,7 +21,19 @@ export default Base =>
...exportCSV
};
const data = options.exportAll ? this.props.data : this.getData();
// get data for csv export
let data;
if (typeof source !== 'undefined') {
data = source;
} else {
data = options.exportAll ? this.props.data : this.getData();
}
// filter data
if (options.onlyExportSelection) {
const selections = this.getSelected();
data = data.filter(row => !!selections.find(sel => row[keyField] === sel));
}
const content = transform(data, meta, this._.get, options);
save(content, options);
}

View File

@@ -269,8 +269,9 @@ const withContext = Base =>
}
render() {
const { keyField, columns, bootstrap4 } = this.props;
const { keyField, columns, bootstrap4, registerExposedAPI } = this.props;
const baseProps = { keyField, columns };
if (registerExposedAPI) baseProps.registerExposedAPI = registerExposedAPI;
let base = this.renderBase();

View File

@@ -15,6 +15,14 @@ export default (
keyField: PropTypes.string.isRequired
}
constructor(props) {
super(props);
if (props.registerExposedAPI) {
const getSelected = () => this.getSelected();
props.registerExposedAPI(getSelected);
}
}
state = { selected: (this.props.selectRow && this.props.selectRow.selected) || [] };
componentWillReceiveProps(nextProps) {
@@ -25,6 +33,11 @@ export default (
}
}
// exposed API
getSelected() {
return this.state.selected;
}
handleRowSelect = (rowKey, checked, rowIndex, e) => {
const { data, keyField, selectRow: { mode, onSelect } } = this.props;
const { ROW_SELECT_SINGLE } = Const;