-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Explore Vis] Fix table visualiztion issues #10754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
ce11838
baf9b73
3a9c07d
23f1ebb
f685e22
55e1791
3a80e2d
55fd811
9e01a75
6f42a06
ad6135e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
|
|
@@ -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'; | ||
|
|
@@ -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); | ||
|
|
@@ -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(() => { | ||
| const currentFieldNames = columns.map(({ name }) => name); | ||
| const prevFieldNames = prevFieldNamesRef.current; | ||
|
||
| 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]) => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
||
| }, [ | ||
| visualizationData?.numericalColumns, | ||
| visualizationData?.categoricalColumns, | ||
|
|
||
There was a problem hiding this comment.
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
useEffectto describe the logic?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 rootThere was a problem hiding this comment.
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
VisColumninterface, i see we are usingid,nameandcolumn, not sure what's the difference.const sortedColumns = useMemo(() => [...columns].sort((a, b) => a.id - b.id), [columns]);const newColumnIds = sortedColumns.map(({ column }) => column);There was a problem hiding this comment.
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