Skip to content

Commit 84cdd9e

Browse files
committed
Fixed an issue where the expanded column width in the result grid of Query Tool or View/Edit Data was not retained after re-executing the query. #7057
1 parent 6aaf47d commit 84cdd9e

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

web/pgadmin/tools/sqleditor/static/js/components/QueryToolComponent.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
881881
eol: qtState.eol,
882882
connection_list: qtState.connection_list,
883883
current_file: qtState.current_file,
884+
qtPanelId: qtPanelId,
884885
toggleQueryTool: () => setQtStatePartial((prev)=>{
885886
return {
886887
...prev,

web/pgadmin/tools/sqleditor/static/js/components/QueryToolDataGrid/index.jsx

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function cellClassGetter(col, isSelected, dataChangeStore, rowKeyGetter){
256256
};
257257
}
258258

259-
function initialiseColumns(columns, rows, totalRowCount, columnWidthBy, maxColumnDataDisplayLength) {
259+
function initialiseColumns(columns, rows, totalRowCount, columnWidthBy, maxColumnDataDisplayLength, qtPanelId) {
260260
let retColumns = [
261261
...columns,
262262
];
@@ -265,7 +265,7 @@ function initialiseColumns(columns, rows, totalRowCount, columnWidthBy, maxColum
265265
canvasContext.font = '12px Roboto';
266266

267267
for(const col of retColumns) {
268-
col.width = getColumnWidth(col, rows, canvasContext, columnWidthBy, maxColumnDataDisplayLength);
268+
col.width = getColumnWidth(col, rows, canvasContext, columnWidthBy, maxColumnDataDisplayLength, qtPanelId);
269269
col.resizable = true;
270270
col.editorOptions = {
271271
commitOnOutsideClick: false,
@@ -336,8 +336,15 @@ function formatColumns(columns, dataChangeStore, selectedColumns, onColumnSelect
336336

337337
return retColumns;
338338
}
339+
// Helper to get or initialize the widths store on window
340+
function getWidthsStore() {
341+
if (!window.pgAdminGridColumnWidths) {
342+
window.pgAdminGridColumnWidths = {};
343+
}
344+
return window.pgAdminGridColumnWidths;
345+
}
339346

340-
function getColumnWidth(column, rows, canvasContext, columnWidthBy, maxColumnDataDisplayLength) {
347+
function getColumnWidth(column, rows, canvasContext, columnWidthBy, maxColumnDataDisplayLength, qtPanelId) {
341348
const dataWidthReducer = (longest, nextRow) => {
342349
let value = nextRow[column.key];
343350
if(_.isNull(value) || _.isUndefined(value)) {
@@ -369,14 +376,20 @@ function getColumnWidth(column, rows, canvasContext, columnWidthBy, maxColumnDat
369376
width = columnHeaderLen;
370377
}
371378
}
372-
return width;
379+
380+
// Use window object for storing widths
381+
const widthsStore = getWidthsStore();
382+
const savedWidths = widthsStore[qtPanelId] || {};
383+
384+
return savedWidths[column.display_name] || width;
373385
}
374386

375387
export default function QueryToolDataGrid({columns, rows, totalRowCount, dataChangeStore,
376388
onSelectedCellChange, selectedColumns, onSelectedColumnsChange, columnWidthBy, startRowNum, maxColumnDataDisplayLength, ...props}) {
377389
const [readyColumns, setReadyColumns] = useState([]);
378390
const [lastSelectedColumn, setLastSelectedColumn] = useState(null);
379391
const eventBus = useContext(QueryToolEventsContext);
392+
const qtPanelId = props.qtPanelId;
380393
const onColumnSelected = (columnIdx, isSelected, isShiftClick)=>{
381394
const newSelectedCols = new Set(selectedColumns);
382395
const start = isShiftClick && lastSelectedColumn ? Math.min(columnIdx, lastSelectedColumn) : columnIdx;
@@ -430,8 +443,17 @@ export default function QueryToolDataGrid({columns, rows, totalRowCount, dataCha
430443
onSelectedCellChange, handleShortcuts, startRowNum
431444
}), [onSelectedCellChange]);
432445

446+
// Save column width to window object on resize
447+
const handleColumnResize = (column, width) => {
448+
const widthsStore = getWidthsStore();
449+
if (!widthsStore[qtPanelId]) {
450+
widthsStore[qtPanelId] = {};
451+
}
452+
widthsStore[qtPanelId][column.display_name] = width;
453+
};
454+
433455
useEffect(()=>{
434-
let initCols = initialiseColumns(columns, rows, totalRowCount, columnWidthBy, maxColumnDataDisplayLength);
456+
let initCols = initialiseColumns(columns, rows, totalRowCount, columnWidthBy, maxColumnDataDisplayLength, qtPanelId);
435457
setReadyColumns(formatColumns(initCols, dataChangeStore, selectedColumns, onColumnSelected, onSelectedColumnsChangeWrapped, props.rowKeyGetter));
436458
}, [columns]);
437459

@@ -453,6 +475,7 @@ export default function QueryToolDataGrid({columns, rows, totalRowCount, dataCha
453475
enableCellSelect={true}
454476
onCopy={handleCopy}
455477
onMultiCopy={handleCopy}
478+
onColumnResize={handleColumnResize} // <-- Add this line
456479
renderers={{
457480
renderRow: renderCustomRow,
458481
}}
@@ -497,4 +520,5 @@ QueryToolDataGrid.propTypes = {
497520
columnWidthBy: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
498521
startRowNum: PropTypes.number,
499522
maxColumnDataDisplayLength: PropTypes.number,
523+
qtPanelId: PropTypes.string,
500524
};

web/pgadmin/tools/sqleditor/static/js/components/sections/ResultSet.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,7 @@ export function ResultSet() {
15491549
onSelectedCellChange={setSelectedCell}
15501550
onSelectedRangeChange={setSelectedRange}
15511551
stripedRows={queryToolCtx.preferences?.sqleditor?.striped_rows}
1552+
qtPanelId={queryToolCtx.qtPanelId}
15521553
/>
15531554
</Box>
15541555
</>}

0 commit comments

Comments
 (0)