Skip to content

Commit 6136d59

Browse files
committed
[Lens] Retain column config (#90048)
1 parent 97f89e2 commit 6136d59

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

x-pack/plugins/lens/public/datatable_visualization/visualization.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,40 @@ describe('Datatable Visualization', () => {
108108
expect(suggestions.length).toBeGreaterThan(0);
109109
});
110110

111+
it('should retain width and hidden config from existing state', () => {
112+
const suggestions = datatableVisualization.getSuggestions({
113+
state: {
114+
layerId: 'first',
115+
columns: [
116+
{ columnId: 'col1', width: 123 },
117+
{ columnId: 'col2', hidden: true },
118+
],
119+
sorting: {
120+
columnId: 'col1',
121+
direction: 'asc',
122+
},
123+
},
124+
table: {
125+
isMultiRow: true,
126+
layerId: 'first',
127+
changeType: 'initial',
128+
columns: [numCol('col1'), strCol('col2'), strCol('col3')],
129+
},
130+
keptLayerIds: [],
131+
});
132+
133+
expect(suggestions.length).toBeGreaterThan(0);
134+
expect(suggestions[0].state.columns).toEqual([
135+
{ columnId: 'col1', width: 123 },
136+
{ columnId: 'col2', hidden: true },
137+
{ columnId: 'col3' },
138+
]);
139+
expect(suggestions[0].state.sorting).toEqual({
140+
columnId: 'col1',
141+
direction: 'asc',
142+
});
143+
});
144+
111145
it('should not make suggestions when the table is unchanged', () => {
112146
const suggestions = datatableVisualization.getSuggestions({
113147
state: {

x-pack/plugins/lens/public/datatable_visualization/visualization.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export const datatableVisualization: Visualization<DatatableVisualizationState>
9898
) {
9999
return [];
100100
}
101+
const oldColumnSettings: Record<string, ColumnState> = {};
102+
if (state) {
103+
state.columns.forEach((column) => {
104+
oldColumnSettings[column.columnId] = column;
105+
});
106+
}
101107
const title =
102108
table.changeType === 'unchanged'
103109
? i18n.translate('xpack.lens.datatable.suggestionLabel', {
@@ -126,8 +132,12 @@ export const datatableVisualization: Visualization<DatatableVisualizationState>
126132
// table with >= 10 columns will have a score of 0.4, fewer columns reduce score
127133
score: (Math.min(table.columns.length, 10) / 10) * 0.4,
128134
state: {
135+
...(state || {}),
129136
layerId: table.layerId,
130-
columns: table.columns.map((col) => ({ columnId: col.columnId })),
137+
columns: table.columns.map((col) => ({
138+
...(oldColumnSettings[col.columnId] || {}),
139+
columnId: col.columnId,
140+
})),
131141
},
132142
previewIcon: LensIconChartDatatable,
133143
// tables are hidden from suggestion bar, but used for drag & drop and chart switching

0 commit comments

Comments
 (0)