Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: use cell content to help calculate the column width (#18469)

ecmadao43e1eb0ecb75
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
index f1211e91ee..711646819d 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
@@ -31,12 +31,16 @@
@click.stop
/>
- <!-- Expand button for long content -->
+ <!-- Expand button for long content -->
<NButton
v-if="clickable"
size="tiny"
circle
- class="dark:bg-dark-bg!"
+ class="shadow bg-white! dark:bg-dark-bg!"
+ :class="{
+ 'opacity-90': clickable,
+ 'hover:opacity-100': clickable,
+ }"
@click.stop="showDetail"
>
<template #icon>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/VirtualDataTable.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/VirtualDataTable.vue
index 975c9062b3..f84932522b 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/VirtualDataTable.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/VirtualDataTable.vue
@@ -111,6 +111,9 @@
ref="virtualListRef"
:items="rows"
:item-size="ROW_HEIGHT"
+ :style="{
+ minWidth: `${tableResize.effectiveWidth.value}px`,
+ }"
>
<template #default="{ item: row, index: rowIndex }: { item: { item: QueryRow; }; index: number; }">
<div
@@ -119,6 +122,7 @@
:data-row-index="rowIndex"
:style="{
height: `${ROW_HEIGHT}px`,
+ minWidth: `${tableResize.effectiveWidth.value}px`,
}"
>
<!-- the index cell -->
@@ -228,6 +232,7 @@ import type {
SortDirection,
SortState,
} from "./common/types";
+import { getPlainValue } from "./common/utils";
import TableCell from "./TableCell.vue";
import useTableColumnWidthLogic from "./useTableResize";
@@ -271,11 +276,33 @@ const containerRef = ref<HTMLDivElement>();
const tableRef = ref<HTMLTableElement>();
const virtualListRef = ref<InstanceType<typeof NVirtualList>>();
+// Get row cell content for column width calculation
+const getRowCellContent = (columnIndex: number): string | undefined => {
+ // check at most 3 rows
+ for (let i = 0; i < Math.min(3, props.rows.length); i++) {
+ const firstRow = props.rows[i];
+ if (!firstRow) {
+ continue;
+ }
+ const cell = firstRow.item.values[columnIndex];
+ if (!cell) {
+ continue;
+ }
+ const columnType = props.columns[columnIndex]?.columnType ?? "";
+ const plainValue = getPlainValue(cell, columnType, "DEFAULT");
+ if (plainValue) {
+ return plainValue;
+ }
+ }
+ return undefined;
+};
+
const tableResize = useTableColumnWidthLogic({
tableRef,
containerRef,
minWidth: 64, // 4rem
maxWidth: 640, // 40rem
+ getRowCellContent,
});
const { getBinaryFormat, setBinaryFormat } = useBinaryFormatContext();
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/useTableResize.ts b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/useTableResize.ts
index 5ad5c0f3b9..5069dcb094 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/useTableResize.ts
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/useTableResize.ts
@@ -8,6 +8,9 @@ export type TableResizeOptions = {
containerRef: Ref<HTMLElement | null | undefined>;
minWidth: number;
maxWidth: number;
+ // Optional: provide row cell content for minimum width calculation
+ // Returns the display text for a given column index (0-based, excluding the index column)
+ getRowCellContent?: (columnIndex: number) => string | undefined;
};
type ColumnProps = {
@@ -62,21 +65,28 @@ const useTableResize = (options: TableResizeOptions) => {
nextTick(() => {
if (!table.value) return;
- const scale = containerWidth.value / table.value.scrollWidth;
const thList = Array.from(table.value.querySelectorAll("th"));
- const lastIndex = thList.length - 1;
- // First pass: calculate base widths for all columns except the last
+ // Font settings for measuring cell text width
+ // Cell font: text-sm (14px) monospace as defined in TableCell.vue
+ const cellFont = "14px ui-monospace, monospace";
+ // Cell padding: px-1 = 0.25rem * 2 = 8px
+ const cellPadding = 8;
+
const baseWidths = thList.map((th, index) => {
+ // Get header content width from the first child element (the content div)
+ // This includes text + icons and avoids inflation from auto-layout
+ const content = th.firstElementChild as HTMLElement | null;
+ const thStyle = getComputedStyle(th);
+ const paddingLeft = parseFloat(thStyle.paddingLeft) || 0;
+ const paddingRight = parseFloat(thStyle.paddingRight) || 0;
+ const borderLeft = parseFloat(thStyle.borderLeftWidth) || 0;
+ const borderRight = parseFloat(thStyle.borderRightWidth) || 0;
+
// For the first column (index column), use scrollWidth to get exact content width
if (index === 0) {
- const content = th.firstElementChild as HTMLElement | null;
if (content) {
- // scrollWidth gives the content width (includes padding but not margin)
- const thStyle = getComputedStyle(th);
const contentStyle = getComputedStyle(content);
- const borderLeft = parseFloat(thStyle.borderLeftWidth) || 0;
- const borderRight = parseFloat(thStyle.borderRightWidth) || 0;
const marginLeft = parseFloat(contentStyle.marginLeft) || 0;
const marginRight = parseFloat(contentStyle.marginRight) || 0;
return (
@@ -88,25 +98,41 @@ const useTableResize = (options: TableResizeOptions) => {
);
}
}
- const width = th.getBoundingClientRect().width;
- return Math.max(width, width * scale);
- });
- // For the last column, subtract any extra width that was added to fill container
- const containerW = options.containerRef?.value?.clientWidth || 0;
- const sumWithoutLast = baseWidths
- .slice(0, lastIndex)
- .reduce((a, b) => a + b, 0);
- const lastColWidth = baseWidths[lastIndex];
- const expectedTotal = sumWithoutLast + lastColWidth;
- // If table was expanded to fill container, remove that extra from last column
- if (expectedTotal > containerW && lastColWidth > options.minWidth) {
- const extraWidth = expectedTotal - containerW;
- baseWidths[lastIndex] = Math.max(
- options.minWidth,
- lastColWidth - extraWidth
+ // For data columns, measure header content scrollWidth (includes text + icons)
+ let headerWidth = options.minWidth;
+ if (content) {
+ headerWidth =
+ content.scrollWidth +
+ paddingLeft +
+ paddingRight +
+ borderLeft +
+ borderRight;
+ }
+
+ // Also consider first row content width if available
+ // index-1 because the first column (index 0) is the row index column
+ if (options.getRowCellContent && index > 0) {
+ const cellContent = options.getRowCellContent(index - 1);
+ // Only use cell content if it's non-empty
+ if (
+ cellContent !== undefined &&
+ cellContent !== null &&
+ cellContent.length > 0
+ ) {
+ const textWidth = measureTextWidth(String(cellContent), cellFont);
+ const cellWidth = textWidth + cellPadding;
+ // Use the larger of header width and cell content width
+ headerWidth = Math.max(headerWidth, cellWidth);
+ }
+ }
+
+ // Apply min/max constraints
+ return Math.min(
+ options.maxWidth,
+ Math.max(options.minWidth, headerWidth)
);
- }
+ });
state.columns = baseWidths.map((width) => ({ width }));
// After calculating the width, use fixed layout to keep the width stable.
@@ -248,6 +274,7 @@ const useTableResize = (options: TableResizeOptions) => {
getColumnWidth,
getTableProps,
totalWidth,
+ effectiveWidth,
startResizing,
};
};
@@ -274,3 +301,13 @@ const toggleDragStyle = (
document.body.classList.remove("cursor-col-resize");
}
};
+
+// Measure text width using canvas for accurate calculation
+// let measureCanvas: HTMLCanvasElement | null = null;
+const measureTextWidth = (text: string, font: string): number => {
+ const measureCanvas = document.createElement("canvas");
+ const context = measureCanvas.getContext("2d");
+ if (!context) return 0;
+ context.font = font;
+ return context.measureText(text).width;
+};