From 95a4047d9f9684a7cf211fbcb83fa072c763ed7f Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Wed, 3 May 2023 10:58:12 -0400 Subject: [PATCH] [v10.0.x] Maintain column widths when deleting columns (#67743) Maintain column widths when deleting columns (#67707) (cherry picked from commit cd8b6be41dea26f833a8f5d1c993dfe0bdeed2f5) Co-authored-by: Victor Marin <36818606+mdvictor@users.noreply.github.com> --- public/app/plugins/panel/datagrid/state.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/panel/datagrid/state.ts b/public/app/plugins/panel/datagrid/state.ts index 65840df7afe..fe414b124ca 100644 --- a/public/app/plugins/panel/datagrid/state.ts +++ b/public/app/plugins/panel/datagrid/state.ts @@ -220,9 +220,14 @@ export const datagridReducer = (state: DatagridState, action: DatagridAction): D columns = [ ...updateColumnsPayload.frame.fields.map((field: Field, index: number) => { const displayName = getFieldDisplayName(field, updateColumnsPayload.frame); + + // find column by field name and update width in new set. We cannot use index because + // if a column gets deleted we don't know the correct index anymore + const width = state.columns.find((column) => column.title === displayName)?.width; + return { title: displayName, - width: state.columns[index]?.width ?? getCellWidth(field), + width: width ?? getCellWidth(field), icon: typeToIconMap.get(field.type), hasMenu: isDatagridEnabled(), trailingRowOptions: { targetColumn: --index },