Skip to content

Commit 53bdd2e

Browse files
committed
Implement feature of sticky header
1 parent 26e2cb4 commit 53bdd2e

File tree

7 files changed

+92
-4
lines changed

7 files changed

+92
-4
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
3+
import BootstrapTable from 'react-bootstrap-table-next';
4+
import Code from 'components/common/code-block';
5+
import { productsGenerator } from 'utils/common';
6+
7+
const products = productsGenerator(87);
8+
9+
const columns = [{
10+
dataField: 'id',
11+
text: 'Product ID'
12+
}, {
13+
dataField: 'name',
14+
text: 'Product Name'
15+
}, {
16+
dataField: 'price',
17+
text: 'Product Price'
18+
}];
19+
20+
const sourceCode = `\
21+
import BootstrapTable from 'react-bootstrap-table-next';
22+
23+
const columns = [{
24+
dataField: 'id',
25+
text: 'Product ID'
26+
}, {
27+
dataField: 'name',
28+
text: 'Product Name'
29+
}, {
30+
dataField: 'price',
31+
text: 'Product Price'
32+
}];
33+
34+
<BootstrapTable sticky keyField="id" data={ products } columns={ columns } />
35+
`;
36+
37+
export default () => (
38+
<div>
39+
<BootstrapTable sticky keyField="id" data={ products } columns={ columns } />
40+
41+
<Code>{ sourceCode }</Code>
42+
</div>
43+
);

packages/react-bootstrap-table2-example/stories/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ import CustomCSV from 'examples/csv/custom-csv';
168168
import EmptyTableOverlay from 'examples/loading-overlay/empty-table-overlay';
169169
import TableOverlay from 'examples/loading-overlay/table-overlay';
170170

171+
// sticky header table
172+
import StickyHeaderTable from 'examples/sticky-header/default';
173+
171174
// remote
172175
import RemoteSort from 'examples/remote/remote-sort';
173176
import RemoteFilter from 'examples/remote/remote-filter';
@@ -362,6 +365,10 @@ storiesOf('Export CSV', module)
362365
.add('Export Custom Data', () => <ExportCustomData />)
363366
.add('Custom CSV', () => <CustomCSV />);
364367

368+
storiesOf('Sticky header', module)
369+
.addDecorator(bootstrapStyle())
370+
.add('Default sticky header', () => <StickyHeaderTable />);
371+
365372
storiesOf('EmptyTableOverlay', module)
366373
.addDecorator(bootstrapStyle())
367374
.add('Empty Table Overlay', () => <EmptyTableOverlay />)

packages/react-bootstrap-table2/src/bootstrap-table.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class BootstrapTable extends PropsBaseResolver(Component) {
5757
rowEvents,
5858
selectRow,
5959
expandRow,
60-
cellEdit
60+
cellEdit,
61+
sticky
6162
} = this.props;
6263

6364
const tableWrapperClass = cs('react-bootstrap-table', wrapperClasses);
@@ -66,7 +67,8 @@ class BootstrapTable extends PropsBaseResolver(Component) {
6667
'table-striped': striped,
6768
'table-hover': hover,
6869
'table-bordered': bordered,
69-
'table-condensed': condensed
70+
'table-condensed': condensed,
71+
'table-sticky': sticky
7072
}, classes);
7173

7274
const tableCaption = (caption && <Caption>{ caption }</Caption>);
@@ -118,6 +120,7 @@ BootstrapTable.propTypes = {
118120
noDataIndication: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
119121
striped: PropTypes.bool,
120122
bordered: PropTypes.bool,
123+
sticky: PropTypes.bool,
121124
hover: PropTypes.bool,
122125
tabIndexCell: PropTypes.bool,
123126
id: PropTypes.string,
@@ -190,6 +193,7 @@ BootstrapTable.defaultProps = {
190193
remote: false,
191194
striped: false,
192195
bordered: true,
196+
sticky: false,
193197
hover: false,
194198
condensed: false,
195199
noDataIndication: null,

packages/react-bootstrap-table2/src/row-selection/selection-cell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default class SelectionCell extends Component {
7373
<BootstrapContext.Consumer>
7474
{
7575
({ bootstrap4 }) => (
76-
<td onClick={ this.handleClick } { ...attrs }>
76+
<td data-row-selection onClick={ this.handleClick } { ...attrs }>
7777
{
7878
selectionRenderer ? selectionRenderer({
7979
mode: inputType,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "./sticky";
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.react-bootstrap-table {
2+
.table-sticky {
3+
thead,
4+
tbody {
5+
display: block;
6+
width: 100%;
7+
8+
tr {
9+
display: inline-flex;
10+
width: 100%;
11+
12+
th,
13+
td {
14+
width: 100%;
15+
16+
&[data-row-selection] {
17+
width: 30px;
18+
min-width: 30px;
19+
}
20+
}
21+
}
22+
}
23+
24+
tbody {
25+
max-height: 400px;
26+
overflow-y: auto;
27+
}
28+
}
29+
}

packages/react-bootstrap-table2/style/react-bootstrap-table2.scss

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import "partials/all";
2+
13
.react-bootstrap-table {
24

35
table {
@@ -55,8 +57,10 @@
5557
content: "\2193";
5658
}
5759

58-
th[data-row-selection] {
60+
th[data-row-selection],
61+
td[data-row-selection] {
5962
width: 30px;
63+
min-width: 30px;
6064
}
6165

6266
th > .selection-input-4,

0 commit comments

Comments
 (0)