Sticky components for react-table v7
npm install react-table-sticky* Demo on CodeSandbox
* Features
* Simple example
* Sticky columns for react-table v6
* Browser support
* Contribute
Steps:
1. create CSS classes full example: `` const Styles = styled.div .tr { .th, :last-child { &.sticky { .header { .footer { .body { [data-sticky-td] { [data-sticky-last-left-td] { [data-sticky-first-right-td] { function TableDemo() { const data = [ const { // Workaround as react-table footerGroups doesn't provide the same internal data than headerGroups return ( ` Tips: if your table contain at least one header group, place yours sticky columns into a group too (even with an empty Header name) ` Check CSS sticky support https://caniuse.com/#search=sticky * git clone https://github.com/guillaumejasmin/react-table-sticky.git.table .header .body .sticky etc... (check following example)
2. create HTML elements , , etc...
3. add useSticky hook
4. then, add sticky: 'left' or sticky: 'right' to your columnjs
import React from 'react';
import styled from 'styled-components';
import { useTable, useBlockLayout } from 'react-table';
import { useSticky } from 'react-table-sticky';
.table {
border: 1px solid #ddd;
:last-child {
.td {
border-bottom: 0;
}
}
}
.td {
padding: 5px;
border-bottom: 1px solid #ddd;
border-right: 1px solid #ddd;
background-color: #fff;
overflow: hidden;
border-right: 0;
}
}
overflow: scroll;
.header,
.footer {
position: sticky;
z-index: 1;
width: fit-content;
}
top: 0;
box-shadow: 0px 3px 3px #ccc;
}
bottom: 0;
box-shadow: 0px -3px 3px #ccc;
}
position: relative;
z-index: 0;
}
position: sticky;
}
box-shadow: 2px 0px 3px #ccc;
}
box-shadow: -2px 0px 3px #ccc;
}
}
};
const columns = [
{
Header: 'First Name',
accessor: 'firstName',
sticky: 'left',
},
{
Header: 'Last Name',
accessor: 'lastName',
sticky: 'left',
},
...
{
Header: 'age',
accessor: 'age',
sticky: 'right',
}
]
...
]
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable(
{
columns,
data,
},
useBlockLayout,
useSticky,
);
const footerGroups = headerGroups.slice().reverse();
{headerGroups.map((headerGroup) => (
{headerGroup.headers.map((column) => (
{column.render('Header')}
))}
))}
{rows.map((row) => {
prepareRow(row);
return (
{row.cells.map((cell) => (
{cell.render('Cell')}
))}
);
})}
{footerGroups.map((footerGroup) => (
{footerGroup.headers.map((column) => (
{column.render('Footer')}
))}
))}
);
}js`
const columns = [
{
Header: ' ',
sticky: 'left',
columns: [
{
Header: 'First Name',
accessor: 'firstName',
},
{
Header: 'Last Name',
accessor: 'lastName',
},
]
},
{
Header: 'Other group',
columns: [
...
]
}
]Sticky columns for React Table v6
If you search tool for sticky position in React Table v6, take a look at react-table-hoc-fixed-columnsBrowser support
Contribute
npm install
* npm run demo`
*
* Go to http://localhost:8080