Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sticky Headers #1203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/js/others/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MouseEventTable from './mouse-event-table';
import TableInTabs from './table-in-tabs';
import GetPageNumByKeyTable from './expose-api-table';
import PrintableTable from './printable-table';
import TableWithStickyHeaders from './table-with-sticky-headers';
import { Col, Panel } from 'react-bootstrap';

class Demo extends React.Component {
Expand All @@ -28,6 +29,11 @@ class Demo extends React.Component {
<h5>Source in /examples/js/others/printable-table.js</h5>
<PrintableTable/>
</Panel>
<Panel header={ 'Sticky Headers Example' }>
<h5>Source in /examples/js/others/table-with-sticky-headers.js</h5>
<h5>Make your headers sticky</h5>
<TableWithStickyHeaders/>
</Panel>
</Col>
);
}
Expand Down
31 changes: 31 additions & 0 deletions examples/js/others/table-with-sticky-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint max-len: 0 */
import React from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';

const products = [];

function addProducts(quantity) {
const startId = products.length;
for (let i = 0; i < quantity; i++) {
const id = startId + i;
products.push({
id: id,
name: 'Item name ' + id,
price: 2100 + i
});
}
}

addProducts(50);

export default class TableWithStickyHeaders extends React.Component {
render() {
return (
<BootstrapTable data={ products } stickyHeaders={ true }>
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
</BootstrapTable>
);
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"dependencies": {
"classnames": "^2.1.2",
"react-modal": "^1.4.0",
"@allenfang/react-toastr": "2.8.2"
"@allenfang/react-toastr": "2.8.2",
"react-overlays": "^0.6.12"
},
"peerDependencies": {
"react": "^0.14.3 || ^15.0.0"
Expand Down
16 changes: 13 additions & 3 deletions src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ class BootstrapTable extends Component {
reset={ this.state.reset }
expandColumnVisible={ expandColumnOptions.expandColumnVisible }
expandColumnComponent={ expandColumnOptions.expandColumnComponent }
expandColumnBeforeSelectColumn={ expandColumnOptions.expandColumnBeforeSelectColumn }>
expandColumnBeforeSelectColumn={ expandColumnOptions.expandColumnBeforeSelectColumn }
autoAffixContainer={ this }
stickyHeaders={ this.props.stickyHeaders }
affixStyle={ this.props.affixStyle }>
{ this.props.children }
</TableHeader>
<TableBody ref='body'
Expand Down Expand Up @@ -1396,7 +1399,9 @@ BootstrapTable.propTypes = {
expandColumnVisible: PropTypes.bool,
expandColumnComponent: PropTypes.func,
expandColumnBeforeSelectColumn: PropTypes.bool
})
}),
stickyHeaders: PropTypes.bool,
affixStyle: PropTypes.object
};
BootstrapTable.defaultProps = {
scrollTop: undefined,
Expand Down Expand Up @@ -1531,7 +1536,12 @@ BootstrapTable.defaultProps = {
},
exportCSV: false,
csvFileName: 'spreadsheet.csv',
ignoreSinglePage: false
ignoreSinglePage: false,
stickyHeaders: false,
affixStyle: {
zIndex: 10,
backgroundColor: '#fff'
}
};

export default BootstrapTable;
34 changes: 27 additions & 7 deletions src/TableHeader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import AutoAffix from 'react-overlays/lib/AutoAffix';
import Const from './Const';
import classSet from 'classnames';
import SelectRowHeaderColumn from './SelectRowHeaderColumn';
Expand Down Expand Up @@ -92,14 +93,30 @@ class TableHeader extends Component {
);
});

const tableHeaders = (
<table className={ tableClasses }>
{ React.cloneElement(this.props.colGroups, { ref: 'headerGrp' }) }
<thead ref='header'>
{ trs }
</thead>
</table>
);

if (this.props.stickyHeaders) {
return (
<AutoAffix affixStyle={ this.props.affixStyle } container={ () => {
return this.props.autoAffixContainer;
} }>
<div ref='container' className={ containerClasses } style={ this.props.style }>
{ tableHeaders }
</div>
</AutoAffix>
);
}

return (
<div ref='container' className={ containerClasses } style={ this.props.style }>
<table className={ tableClasses }>
{ React.cloneElement(this.props.colGroups, { ref: 'headerGrp' }) }
<thead ref='header'>
{ trs }
</thead>
</table>
{ tableHeaders }
</div>
);
}
Expand Down Expand Up @@ -154,7 +171,10 @@ TableHeader.propTypes = {
reset: PropTypes.bool,
expandColumnVisible: PropTypes.bool,
expandColumnComponent: PropTypes.func,
expandColumnBeforeSelectColumn: PropTypes.bool
expandColumnBeforeSelectColumn: PropTypes.bool,
autoAffixContainer: PropTypes.object,
stickyHeaders: PropTypes.bool,
affixStyle: PropTypes.object
};

export default TableHeader;