Skip to content

Commit

Permalink
feat: crud2中自适应高度使用粘性定位固定表头 (#11637)
Browse files Browse the repository at this point in the history
* feat: crud2中自适应高度使用粘性定位固定表头

* feat: crud2中自适应高度使用粘性定位固定表头
  • Loading branch information
hzh11012 authored Feb 20, 2025
1 parent b594a93 commit a39a5a3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
20 changes: 20 additions & 0 deletions packages/amis-ui/scss/components/_table2.scss
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,26 @@
.#{$ns}Table-header {
padding: 0;
}

&-self-sticky {
position: relative !important;

.#{$ns}Table-content {
overflow-x: unset !important;
}

.#{$ns}Table-row {
z-index: 1;
}

.#{$ns}Table-cell-self-sticky {
z-index: 2 !important;
}

.#{$ns}Table-cell-fix-left {
z-index: 3 !important;
}
}
}

&.#{$ns}Table-ping-left {
Expand Down
16 changes: 15 additions & 1 deletion packages/amis-ui/src/components/table/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const zIndex = 1;

export interface Props extends ThemeProps {
fixed?: string | boolean; // left | right
selfSticky?: boolean;
rowSpan?: number | any;
colSpan?: number | any;
key?: string | number;
Expand All @@ -33,6 +34,7 @@ export interface Props extends ThemeProps {
export default class BodyCell extends React.PureComponent<Props> {
static defaultProps = {
fixed: '',
selfSticky: false,
wrapperComponent: 'td',
rowSpan: null,
colSpan: null
Expand All @@ -41,6 +43,7 @@ export default class BodyCell extends React.PureComponent<Props> {
render() {
const {
fixed,
selfSticky,
rowSpan,
colSpan,
children,
Expand All @@ -54,16 +57,27 @@ export default class BodyCell extends React.PureComponent<Props> {
testIdBuilder
} = this.props;

let _style: object = {...style};

if (fixed || selfSticky) {
_style = {
position: 'sticky',
zIndex,
..._style
};
}

return (
<Component
rowSpan={rowSpan && rowSpan > 1 ? rowSpan : null}
colSpan={colSpan && colSpan > 1 ? colSpan : null}
className={cx('Table-cell', className, {
[cx(`Table-cell-fix-${fixed}`)]: fixed,
[`Table-cell-self-sticky`]: selfSticky,
[`text-${column?.align}`]: column?.align,
[`align-${column?.vAlign}`]: column?.vAlign
})}
style={fixed ? {position: 'sticky', zIndex, ...style} : {...style}}
style={_style}
data-depth={depth || null}
data-col={col}
{...testIdBuilder?.getTestId()}
Expand Down
14 changes: 11 additions & 3 deletions packages/amis-ui/src/components/table/Head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
getBuildColumns,
getAllSelectableRows,
updateFixedRow,
hasFixedColumn
hasFixedColumn,
updateStickyRow
} from './util';
import {
ColumnProps,
Expand Down Expand Up @@ -50,6 +51,7 @@ export interface Props extends ThemeProps {
onFilter?: Function;
onResizeMouseDown: Function;
testIdBuilder?: TestIdBuilder;
selfSticky?: boolean;
}

export default class Head extends React.PureComponent<Props> {
Expand Down Expand Up @@ -83,7 +85,7 @@ export default class Head extends React.PureComponent<Props> {
}

updateFixedRow() {
const {classnames: cx} = this.props;
const {classnames: cx, selfSticky} = this.props;
const thead = this.domRef.current;
const children = thead?.children;
for (let i = 0; i < (children?.length || 0); i++) {
Expand All @@ -92,6 +94,10 @@ export default class Head extends React.PureComponent<Props> {
this.prependColumns(cols);
}

if (selfSticky) {
updateStickyRow(children as HTMLCollection, i);
}

if (hasFixedColumn(cols)) {
updateFixedRow(children?.[i] as HTMLTableRowElement, cols, cx);
}
Expand Down Expand Up @@ -134,7 +140,8 @@ export default class Head extends React.PureComponent<Props> {
onFilter,
onResizeMouseDown,
testIdBuilder,
className
className,
selfSticky
} = this.props;

const {thColumns, tdColumns} = getBuildColumns(columns);
Expand Down Expand Up @@ -307,6 +314,7 @@ export default class Head extends React.PureComponent<Props> {
colSpan={item.colSpan}
classnames={cx}
classPrefix={classPrefix}
selfSticky={selfSticky}
fixed={item.fixed === true ? 'left' : item.fixed}
className={cx({
'Table-cell-last': thIndex === maxCount
Expand Down
15 changes: 12 additions & 3 deletions packages/amis-ui/src/components/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,9 @@ export class Table extends React.PureComponent<TableProps, TableState> {
onSelectAll,
onFilter,
testIdBuilder,
headerClassName
headerClassName,
sticky,
autoFillHeight
} = this.props;

const rowSelectionKeyField = this.getRowSelectionKeyField();
Expand All @@ -705,6 +707,7 @@ export class Table extends React.PureComponent<TableProps, TableState> {
return (
<Head
key="thead"
selfSticky={sticky && !!autoFillHeight}
columns={columns}
draggable={!!draggable}
selectable={!!rowSelection}
Expand Down Expand Up @@ -1649,6 +1652,7 @@ export class Table extends React.PureComponent<TableProps, TableState> {
resizable,
columns,
sticky,
autoFillHeight,
classnames: cx
} = this.props;

Expand Down Expand Up @@ -1677,10 +1681,15 @@ export class Table extends React.PureComponent<TableProps, TableState> {
</div>
) : null}

{hasScrollY || sticky ? (
{hasScrollY || (sticky && !autoFillHeight) ? (
this.renderScrollTable()
) : (
<div className={cx('Table-container')} ref={this.containerDom}>
<div
className={cx('Table-container', {
[cx('Table-container-self-sticky')]: sticky && !!autoFillHeight
})}
ref={this.containerDom}
>
{this.renderTable()}
</div>
)}
Expand Down
24 changes: 24 additions & 0 deletions packages/amis-ui/src/components/table/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ function getPreviousLeftWidth(
return width;
}

function getPreviousTopHeight(thead: HTMLCollection, rowIndex: number) {
let height = 0;

for (let i = 0; i < rowIndex; i++) {
if (thead && thead[i]) {
const dom = thead[i] as HTMLElement;
height += dom.offsetHeight;
}
}
return height;
}

function getAfterRightWidth(
doms: HTMLCollection,
index: number,
Expand Down Expand Up @@ -292,6 +304,18 @@ export function updateFixedRow(
}
}

// 更新一个tr下的th的top
export function updateStickyRow(thead: HTMLCollection, rowIndex: number) {
const children = thead[rowIndex]?.children || [];

for (let i = 0; i < children.length; i++) {
const dom = children[i] as HTMLElement;
dom.style.removeProperty('top');
dom.style.top =
rowIndex > 0 ? getPreviousTopHeight(thead, rowIndex) + 'px' : '0';
}
}

export function hasFixedColumn(columns: Array<ColumnProps | SummaryProps>) {
return find(columns, column => column.fixed);
}
Expand Down

0 comments on commit a39a5a3

Please sign in to comment.