react-table不支持固定列,该问题已经被提出 Sticky columns。但是已经实现了解决方法,您可以在github或npm包中找到资源(链接取自线程Sticky columns)。 在线演示。 感谢GuillaumeJasmin。
不使用react-table或任何npm依赖项,只能通过在react应用程序中使用CSS技巧来实现固定列。
在此处找到完整的代码。
步骤1: 将固定列和可滚动列的数据集分开
步骤2: 将两个表并排放置,使其看起来像一个单一的表
步骤3: 使用单个div将两者包装起来,并使用固定宽度,为第二个表给出固定或响应式宽度,并使用overflow-x: scroll; 以便水平滚动,而第一个表的列将不可滚动。
react-table
的v7版本,并且进行了完全重写,react-table-hoc-fixed-columns与其不兼容。如果您正在使用版本<7,请参见上面@Alex的answer。react-table
v7,您可以使用同一作者的react-table-sticky。尝试以下方法,可能会有所帮助:
在React中,组件本身包含CSS部分:
fix: {
position: 'sticky',
right: 0,
padding: '11px 16px',
boxShadow: '0px 4px 4px 0px #999',
},
fixColumn: {
position: 'sticky',
right: 0,
},
<MuiTable component="table" {...getProps()}>
<TableHead>
{headerGroups.map(prop => (
<TableRow {...prop.getAllHeaderProps()}>
{prop.headers.map((column, index) => {
const fixColIndex = column.id === 'column_id_need_to_fix' ? index : '';);
const fixHeaderProps = column.getHeaderProps({
className: clsx({ [classes.fixColumn]: fixColIndex }, column.className),
});return (
<TableCell {...fixHeaderProps}></TableCell>
);
})}
</TableRow>
))}
</TableHead>
const Props = props.getProps({
className: clsx(
{
[classes.fix]: props.column.id === fixColumnId,
},
),
});
return (
<TableCell {...Props}>
{prop.render('Cell')}
</TableCell>
);