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): 列宽调整需要考虑列的最小宽度 #1456

Merged
merged 5 commits into from
Sep 4, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/table/hooks/useColumnResize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ref, Ref, reactive } from '@vue/composition-api';
import isNumber from 'lodash/isNumber';
import { RecalculateColumnWidthFunc } from '../interface';
import { BaseTableCol, TableRowData } from '../type';
import setThWidthListByColumnDrag from '../../_common/js/table/set-column-width-by-drag';
Expand Down Expand Up @@ -89,18 +90,30 @@ export default function useColumnResize(
// 非 resize 的点击,不做处理
if (!resizeLineParams.draggingCol) return;

const getMinMaxColWidth = (col: BaseTableCol<TableRowData>, effectPrevCol: BaseTableCol<TableRowData>) => {
let targetCol = null;
if (resizeLineParams.effectCol === 'next') {
targetCol = col;
} else {
targetCol = effectPrevCol;
}
const propMinWidth = isNumber(targetCol.minWidth) ? targetCol.minWidth : parseFloat(targetCol.minWidth);
return {
minColWidth: Math.max(targetCol.resize?.minWidth || DEFAULT_MIN_WIDTH, propMinWidth || DEFAULT_MIN_WIDTH),
maxColWidth: targetCol.resize?.maxWidth || DEFAULT_MAX_WIDTH,
};
};

const target = resizeLineParams.draggingCol;
const targetBoundRect = target.getBoundingClientRect();
const tableBoundRect = tableContentRef.value?.getBoundingClientRect();
const resizeLinePos = targetBoundRect.right - tableBoundRect.left;
const colLeft = targetBoundRect.left - tableBoundRect.left;
const minColWidth = col.resize?.minWidth || DEFAULT_MIN_WIDTH;
const maxColWidth = col.resize?.maxWidth || DEFAULT_MAX_WIDTH;
const minResizeLineLeft = colLeft + minColWidth;
const maxResizeLineLeft = colLeft + maxColWidth;

const effectNextCol = effectColMap.value[col.colKey].next;
const effectPrevCol = effectColMap.value[col.colKey].prev;
const { minColWidth, maxColWidth } = getMinMaxColWidth(col, effectPrevCol);
const minResizeLineLeft = colLeft + minColWidth;
const maxResizeLineLeft = colLeft + maxColWidth;

// 开始拖拽,记录下鼠标起始位置
resizeLineParams.isDragging = true;
Expand Down
12 changes: 12 additions & 0 deletions src/table/hooks/useFixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ export default function useFixed(
}, 0);
};

const resetThWidthList = () => {
thWidthList.value = {};
};

const emitScrollEvent = (e: WheelEvent) => {
props.onScrollX?.({ e });
// Vue3 ignore next line
Expand Down Expand Up @@ -480,6 +484,14 @@ export default function useFixed(
{ immediate: true },
);

watch(finalColumns, () => {
updateTableWidth();
resetThWidthList();
if (columnResizable.value) {
recalculateColWidth.value(finalColumns.value, thWidthList.value, tableLayout.value, tableElmWidth.value);
}
});

const refreshTable = debounce(() => {
updateTableWidth();
updateFixedHeader();
Expand Down