Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix table column resizable #2324

Merged
merged 4 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 38 additions & 5 deletions src/table/hooks/useColumnResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,45 @@ export default function useColumnResize(params: {
};
};

const getFixedToLeftResizeInfo = (targetBoundRect: DOMRect, tableBoundRect: DOMRect) => {
const resizeLinePos = targetBoundRect.left - tableBoundRect.left;
const colLeft = targetBoundRect.left - tableBoundRect.left;
return {
resizeLinePos,
minResizeLineLeft: colLeft,
maxResizeLineLeft: colLeft,
};
};

const getTotalTableWidth = (thWidthList: { [key: string]: number }): number => {
let tableWidth = 0;
leafColumns.value.forEach((col) => {
tableWidth += thWidthList[col.colKey];
});
return tableWidth;
};
const getSiblingColCanResizable = (
newThWidthList: { [key: string]: number },
effectNextCol: BaseTableCol,
distance: number,
index: number,
) => {
let isWidthAbnormal = true;
if (effectNextCol) {
const { minColWidth, maxColWidth } = getMinMaxColWidth(effectNextCol);
const targetNextColWidth = newThWidthList[effectNextCol.colKey] + distance;
isWidthAbnormal = targetNextColWidth < minColWidth || targetNextColWidth > maxColWidth;
}
return !(isWidthAbnormal || isWidthOverflow.value || index === leafColumns.value.length - 1);
};
const getOtherResizeInfo = (
col: BaseTableCol<TableRowData>,
effectPrevCol: BaseTableCol,
targetBoundRect: DOMRect,
tableBoundRect: DOMRect,
) => effectPrevCol
? getNormalResizeInfo(col, effectPrevCol, targetBoundRect, tableBoundRect)
: getFixedToLeftResizeInfo(targetBoundRect, tableBoundRect);

// 调整表格列宽
const onColumnMousedown = (e: MouseEvent, col: BaseTableCol<TableRowData>, index: number) => {
Expand All @@ -204,7 +236,7 @@ export default function useColumnResize(params: {
const effectPrevCol = effectColMap.value[col.colKey]?.prev;
const { resizeLinePos, minResizeLineLeft, maxResizeLineLeft } = isColRightFixActive(col)
? getFixedToRightResizeInfo(target, col, effectNextCol, targetBoundRect, tableBoundRect)
: getNormalResizeInfo(col, effectPrevCol, targetBoundRect, tableBoundRect);
: getOtherResizeInfo(col, effectNextCol, targetBoundRect, tableBoundRect);

// 开始拖拽,记录下鼠标起始位置
resizeLineParams.isDragging = true;
Expand All @@ -231,13 +263,14 @@ export default function useColumnResize(params: {
*/
const thWidthList = getThWidthList('calculate');
const currentCol = effectColMap.value[col.colKey]?.current;
const currentSibling = resizeLineParams.effectCol === 'next' ? currentCol.prevSibling : currentCol.nextSibling;
const currentSibling = resizeLineParams.effectCol === 'next' ? currentCol.nextSibling : currentCol.prevSibling;
// 多行表头,列宽为最后一层的宽度,即叶子结点宽度
const newThWidthList = { ...thWidthList };
// 当前列不允许修改宽度,就调整相邻列的宽度
const tmpCurrentCol = col.resizable !== false ? col : currentSibling;
// 是否允许调整相邻列宽:列宽未超出时,且并非是最后一列(最后一列的右侧拉伸会认为是表格整体宽度调整)
const canResizeSiblingColWidth = !(isWidthOverflow.value || index === leafColumns.value.length - 1);
// 是否允许调整后一列的列宽:列宽未超出时,满足后一列设置的最大最小值时且并非是最后一列(最后一列的右侧拉伸会认为是表格整体宽度调整)
const rightCol = resizeLineParams.effectCol === 'next' ? currentCol.nextSibling : col;
const canResizeSiblingColWidth = getSiblingColCanResizable(newThWidthList, rightCol, moveDistance, index);

if (resizeLineParams.effectCol === 'next') {
// 右侧激活态的固定列,需特殊调整
Expand All @@ -259,7 +292,7 @@ export default function useColumnResize(params: {
if (canResizeSiblingColWidth) {
newThWidthList[tmpCurrentCol.colKey] += moveDistance;
}
newThWidthList[effectPrevCol.colKey] -= moveDistance;
effectPrevCol && (newThWidthList[effectPrevCol.colKey] -= moveDistance);
}
updateThWidthList(newThWidthList);
const tableWidth = getTotalTableWidth(newThWidthList);
Expand Down
9 changes: 7 additions & 2 deletions src/table/thead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ export default defineComponent({
const resizeColumnListener = this.resizable || !canDragSort
? {
mousedown: (e: MouseEvent) => {
this.columnResizeParams?.onColumnMousedown?.(e, col, index);
e.stopPropagation();
if (this.resizable) {
this.columnResizeParams?.onColumnMousedown?.(e, col, index);
}
if (!canDragSort) {
const timer = setTimeout(() => {
const thList = this.theadRef.querySelectorAll('th');
Expand All @@ -171,7 +174,9 @@ export default defineComponent({
}, 10);
}
},
mousemove: (e: MouseEvent) => this.columnResizeParams?.onColumnMouseover?.(e, col),
mousemove: (e: MouseEvent) => {
this.resizable && this.columnResizeParams?.onColumnMouseover?.(e, col);
},
}
: {};
const content = isFunction(col.ellipsisTitle) ? col.ellipsisTitle(h, { col, colIndex: index }) : undefined;
Expand Down