Skip to content

Commit 178bfb6

Browse files
authored
fix(HeaderCell): improve left positioning logic for frozen multi-row scenarios (#8327)
* fix(HeaderCell): improve left positioning logic for frozen multi-row scenarios * fix(HeaderCell): remove unnecessary direct manipulation of elementRef for left positioning
1 parent 7a0f3d5 commit 178bfb6

1 file changed

Lines changed: 40 additions & 11 deletions

File tree

components/lib/datatable/HeaderCell.js

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,43 @@ export const HeaderCell = React.memo((props) => {
115115
styleObject.right = right + 'px';
116116
} else {
117117
let left = 0;
118-
let prev = elementRef.current && elementRef.current.previousElementSibling;
119-
120-
while (prev) {
121-
if (prev && prev.classList.contains('p-frozen-column')) {
122-
left = DomHandler.getOuterWidth(prev) + parseFloat(prev.style.left || 0);
123-
elementRef.current.style.left = left + 'px';
124-
break;
118+
let prev = elementRef.current?.previousElementSibling;
119+
120+
const thead = elementRef.current?.closest('thead');
121+
const scrollContainer = thead?.parentElement.parentElement;
122+
const scrollLeft = scrollContainer?.scrollLeft || 0;
123+
124+
const isMultiRow = thead && thead.querySelectorAll('tr').length > 1;
125+
126+
if (elementRef.current) {
127+
if (!isMultiRow) {
128+
while (prev) {
129+
if (prev.classList.contains('p-frozen-column')) {
130+
left = DomHandler.getOuterWidth(prev) + parseFloat(prev.style.left || 0);
131+
break;
132+
}
133+
134+
prev = prev.previousElementSibling;
135+
}
136+
} else {
137+
const targetLeft = elementRef.current.offsetLeft - scrollLeft;
138+
const frozenCells = Array.from(thead.querySelectorAll('th.p-frozen-column'));
139+
140+
let candidate = null;
141+
142+
for (const cell of frozenCells) {
143+
const cellLeft = cell.offsetLeft - scrollLeft;
144+
const cellRight = cellLeft + DomHandler.getOuterWidth(cell);
145+
146+
if (cellRight <= targetLeft && (!candidate || cellRight > candidate.offsetLeft + DomHandler.getOuterWidth(candidate))) {
147+
candidate = cell;
148+
}
149+
}
150+
151+
if (candidate) {
152+
left = candidate.offsetLeft + DomHandler.getOuterWidth(candidate) - scrollLeft;
153+
}
125154
}
126-
127-
prev = prev.previousElementSibling;
128155
}
129156

130157
styleObject.left = left + 'px';
@@ -135,8 +162,10 @@ export const HeaderCell = React.memo((props) => {
135162
if (filterRow) {
136163
let index = DomHandler.index(elementRef.current);
137164

138-
filterRow.children[index].style.left = styleObject.left;
139-
filterRow.children[index].style.right = styleObject.right;
165+
if (filterRow.children[index]) {
166+
filterRow.children[index].style.left = styleObject.left;
167+
filterRow.children[index].style.right = styleObject.right;
168+
}
140169
}
141170

142171
const isSameStyle = styleObjectState.left === styleObject.left && styleObjectState.right === styleObject.right;

0 commit comments

Comments
 (0)