2017-08-17 20:51:14 +08:00
|
|
|
import React, { Component } from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
|
|
|
|
|
|
|
|
class DataTable extends Component {
|
2017-08-21 21:36:22 +08:00
|
|
|
static cleanColumnAttributes(col) {
|
|
|
|
// Remove additional attributes from the columns
|
|
|
|
const {
|
|
|
|
id, isKey, textId, name, position, visible,
|
|
|
|
sortable, locked, ...cleanCol
|
|
|
|
} = col;
|
|
|
|
return cleanCol;
|
|
|
|
}
|
|
|
|
|
2017-08-17 20:51:14 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.cleanProps = this.cleanProps.bind(this);
|
|
|
|
this.displayHeader = this.displayHeader.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanProps() {
|
|
|
|
// Remove additional props from the props value
|
2017-08-21 21:36:22 +08:00
|
|
|
const {columns, ...cleanProps} = this.props;
|
2017-08-17 20:51:14 +08:00
|
|
|
return cleanProps;
|
|
|
|
}
|
|
|
|
|
2017-08-21 21:36:22 +08:00
|
|
|
|
2017-08-17 20:51:14 +08:00
|
|
|
displayHeader() {
|
2017-08-21 21:36:22 +08:00
|
|
|
const orderedCols = [...this.props.columns].sort((a, b) => a.position - b.position);
|
|
|
|
return orderedCols.map((col) =>
|
|
|
|
<TableHeaderColumn
|
|
|
|
key={col.id}
|
|
|
|
dataField={col.textId}
|
|
|
|
isKey={col.isKey}
|
|
|
|
hidden={('visible' in col) && !col.visible}
|
|
|
|
dataSort={col.sortable}
|
|
|
|
{...DataTable.cleanColumnAttributes(col)}
|
|
|
|
>
|
|
|
|
{col.name}
|
|
|
|
</TableHeaderColumn>
|
|
|
|
);
|
2017-08-17 20:51:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<BootstrapTable {...this.cleanProps()}>
|
|
|
|
{this.displayHeader()}
|
|
|
|
</BootstrapTable>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTable.propTypes = {
|
|
|
|
columns: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
id: PropTypes.number.isRequired,
|
|
|
|
isKey: PropTypes.bool.isRequired,
|
|
|
|
textId: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
position: PropTypes.number.isRequired,
|
|
|
|
visible: PropTypes.bool,
|
|
|
|
sortable: PropTypes.bool,
|
|
|
|
locked: PropTypes.bool
|
|
|
|
})
|
2017-08-21 21:36:22 +08:00
|
|
|
).isRequired,
|
|
|
|
data: PropTypes.arrayOf(PropTypes.object).isRequired
|
2017-08-17 20:51:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default DataTable;
|