Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions changelogs/fragments/10754.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Fix fields order issue ([#10754](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/10754))
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useCallback, useEffect, useMemo, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { EuiDataGrid, EuiDataGridCellValueElementProps, EuiDataGridColumn } from '@elastic/eui';
import { VisColumn, VisFieldType } from '../types';
import { defaultTableChartStyles, CellTypeConfig, TableChartStyle } from './table_vis_config';
Expand All @@ -27,6 +27,7 @@ export const TableVis = React.memo(
({ rows, columns, styleOptions, pageSizeOptions, showStyleSelector }: TableVisProps) => {
const pageSize = styleOptions?.pageSize ?? defaultTableChartStyles.pageSize;
const [visibleColumns, setVisibleColumns] = useState(() => columns.map(({ column }) => column));
const prevFieldNamesRef = useRef<string[]>(columns.map(({ name }) => name));
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize });
const [filters, setFilters] = useState<Record<string, FilterConfig>>({});
const [popoverOpenColumnId, setPopoverOpenColumnId] = useState<string | null>(null);
Expand Down Expand Up @@ -100,6 +101,33 @@ export const TableVis = React.memo(
}
}, [styleOptions?.showColumnFilter]);

// Handle column visibility when columns change
// If there are no common fields between old and new columns, show all new columns
// If there are common fields, preserve existing visible columns and add any new ones
useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you leave a comment on this useEffect to describe the logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this just a replacement by new columns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lunalzm How do you reproduce the issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hailong-am @lunalzm I removed this useEffect, and pushed a commit that fix the column visibility issue from the root

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just read the comment for this method and didn't read the logic fully, so i think it just a replacement by new columns. There have different property in VisColumn interface, i see we are using id, name and column, not sure what's the difference.

const sortedColumns = useMemo(() => [...columns].sort((a, b) => a.id - b.id), [columns]);

const newColumnIds = sortedColumns.map(({ column }) => column);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue sometimes happens when I run a new query or refresh the page. Some columns get hidden by default.

Screen.Recording.2025-10-21.at.16.54.54.mov

const currentFieldNames = columns.map(({ name }) => name);
const prevFieldNames = prevFieldNamesRef.current;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to delete this ref, can we calculate the new columns based prevVisibleColumns in setVisibleColumns callback and sortedColumns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall implementation has changed, this useEffect no longer exists now

const newColumnIds = columns.map(({ column }) => column);

const hasCommonFields = currentFieldNames.some((name) => prevFieldNames.includes(name));

if (!hasCommonFields || prevFieldNames.length === 0) {
setVisibleColumns(newColumnIds);
} else {
setVisibleColumns((prevVisibleColumns) => {
const existingVisibleColumns = prevVisibleColumns.filter((colId) =>
newColumnIds.includes(colId)
);
const newColumns = newColumnIds.filter((colId) => !prevVisibleColumns.includes(colId));
const result = [...existingVisibleColumns, ...newColumns];

return result;
});
}

prevFieldNamesRef.current = currentFieldNames;
}, [columns]);

const filteredRows = useMemo(() => {
return rows.filter((row) =>
Object.entries(filters).every(([columnId, config]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ export const VisualizationRender = ({
}, [visualizationData?.transformedData]);

const columns = useMemo(() => {
return [
const allColumns = [
...(visualizationData?.numericalColumns ?? []),
...(visualizationData?.categoricalColumns ?? []),
...(visualizationData?.dateColumns ?? []),
];

return allColumns.sort((a, b) => a.id - b.id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also check explore_embeddable.tsx to see if the same logic should apply there? or perhaps move this logic to table_vis component?

}, [
visualizationData?.numericalColumns,
visualizationData?.categoricalColumns,
Expand Down
Loading