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

feat(table): 新增 column.resizable 支持自定义任意列是否可拖拽调整宽度 #1732

Merged
merged 1 commit into from
Sep 26, 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
35 changes: 20 additions & 15 deletions src/table/hooks/useColumnResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function useColumnResize(

// 表格列宽拖拽事件
// 只在表头显示拖拽图标
const onColumnMouseover = (e: MouseEvent) => {
const onColumnMouseover = (e: MouseEvent, col: BaseTableCol<TableRowData>) => {
if (!resizeLineRef.value) return;

const target = (e.target as HTMLElement).closest('th');
Expand All @@ -63,25 +63,30 @@ export default function useColumnResize(
// 当离右边框的距离不超过 8 时,显示拖拽图标
const distance = 8;
if (targetBoundRect.right - e.pageX <= distance) {
target.style.cursor = 'col-resize';
resizeLineParams.draggingCol = target;
resizeLineParams.effectCol = 'next';
const colResizable = col.resizable ?? true;
if (colResizable) {
target.style.cursor = 'col-resize';
resizeLineParams.draggingCol = target;
resizeLineParams.effectCol = 'next';
return;
}
} else if (e.pageX - targetBoundRect.left <= distance) {
const prevEl = target.previousElementSibling;
if (prevEl) {
target.style.cursor = 'col-resize';
resizeLineParams.draggingCol = prevEl as HTMLElement;
resizeLineParams.effectCol = 'prev';
} else {
target.style.cursor = '';
resizeLineParams.draggingCol = null;
resizeLineParams.effectCol = null;
const effectPrevCol = effectColMap.value[col.colKey].prev;
const colResizable = effectPrevCol.resizable ?? true;
if (colResizable) {
target.style.cursor = 'col-resize';
resizeLineParams.draggingCol = prevEl as HTMLElement;
resizeLineParams.effectCol = 'prev';
return;
}
}
} else {
target.style.cursor = '';
resizeLineParams.draggingCol = null;
resizeLineParams.effectCol = null;
}
// 重置记录值
target.style.cursor = '';
resizeLineParams.draggingCol = null;
resizeLineParams.effectCol = null;
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/table/thead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface TheadProps {
columnResizeParams: {
resizeLineRef: Ref<HTMLDivElement>;
resizeLineStyle: CSSProperties;
onColumnMouseover: (e: MouseEvent) => void;
onColumnMouseover: (e: MouseEvent, col: BaseTableCol<TableRowData>) => void;
onColumnMousedown: (e: MouseEvent, col: BaseTableCol<TableRowData>) => void;
};
resizable: Boolean;
Expand Down Expand Up @@ -110,7 +110,7 @@ export default defineComponent({
const resizeColumnListener = this.resizable
? {
onMousedown: (e: MouseEvent) => this.columnResizeParams?.onColumnMousedown?.(e, col),
onMousemove: (e: MouseEvent) => this.columnResizeParams?.onColumnMouseover?.(e),
onMousemove: (e: MouseEvent) => this.columnResizeParams?.onColumnMouseover?.(e, col),
}
: {};
const content = isFunction(col.ellipsisTitle) ? col.ellipsisTitle(h, { col, colIndex: index }) : undefined;
Expand Down
5 changes: 5 additions & 0 deletions src/table/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ export interface BaseTableCol<T extends TableRowData = TableRowData> {
* 限制拖拽调整的最小宽度和最大宽度。`resize.minWidth` 默认为 `80`,`resize.maxWidth` 默认为 `600`
*/
resize?: TableColumnResizeConfig;
/**
* 是否允许拖拽调整该列大小,仅当 `table.resizable = true` 时生效
* @default true
*/
resizable?: boolean;
/**
* 自定义表头渲染。值类型为 Function 表示以函数形式渲染表头。值类型为 string 表示使用插槽渲染,插槽名称为 title 的值。优先级高于 render
*/
Expand Down