diff --git a/src/plugins/discover/public/application/components/data_grid/constants.ts b/src/plugins/discover/public/application/components/data_grid/constants.ts index c11f205aff1f..be96468a3a09 100644 --- a/src/plugins/discover/public/application/components/data_grid/constants.ts +++ b/src/plugins/discover/public/application/components/data_grid/constants.ts @@ -9,4 +9,5 @@ export const toolbarVisibility = { allowReorder: true, }, showStyleSelector: false, + showFullScreenSelector: false, }; diff --git a/src/plugins/discover/public/application/components/data_grid/data_grid_table.tsx b/src/plugins/discover/public/application/components/data_grid/data_grid_table.tsx index d304034d3760..f74288d41cea 100644 --- a/src/plugins/discover/public/application/components/data_grid/data_grid_table.tsx +++ b/src/plugins/discover/public/application/components/data_grid/data_grid_table.tsx @@ -17,6 +17,7 @@ import { DiscoverServices } from '../../../build_services'; import { OpenSearchSearchHit } from '../../doc_views/doc_views_types'; import { usePagination } from '../utils/use_pagination'; import { SortOrder } from '../../../saved_searches/types'; +import { buildColumns } from '../../utils/columns'; export interface DataGridTableProps { columns: string[]; @@ -50,6 +51,8 @@ export const DataGridTable = ({ const rowCount = useMemo(() => (rows ? rows.length : 0), [rows]); const pagination = usePagination(rowCount); + const adjustedColumns = buildColumns(columns, indexPattern); + const includeSourceInColumns = adjustedColumns.includes('_source'); const sortingColumns = useMemo(() => sort.map(([id, direction]) => ({ id, direction })), [sort]); const rowHeightsOptions = useMemo( () => ({ @@ -73,18 +76,28 @@ export const DataGridTable = ({ ]); const dataGridTableColumns = useMemo( - () => buildDataGridColumns(columns, indexPattern, displayTimeColumn), - [columns, indexPattern, displayTimeColumn] + () => + buildDataGridColumns( + adjustedColumns, + indexPattern, + displayTimeColumn, + includeSourceInColumns + ), + [adjustedColumns, indexPattern, displayTimeColumn, includeSourceInColumns] ); const dataGridTableColumnsVisibility = useMemo( () => ({ - visibleColumns: computeVisibleColumns(columns, indexPattern, displayTimeColumn) as string[], + visibleColumns: computeVisibleColumns( + adjustedColumns, + indexPattern, + displayTimeColumn + ) as string[], setVisibleColumns: (cols: string[]) => { onSetColumns(cols); }, }), - [columns, indexPattern, displayTimeColumn, onSetColumns] + [adjustedColumns, indexPattern, displayTimeColumn, onSetColumns] ); const sorting: EuiDataGridSorting = useMemo( @@ -152,7 +165,7 @@ export const DataGridTable = ({ generateDataGridTableColumn(colName, idxPattern)); + return columnsToUse.map((colName) => + generateDataGridTableColumn(colName, idxPattern, includeSourceInColumns) + ); } -export function generateDataGridTableColumn(colName: string, idxPattern: IndexPattern) { +export function generateDataGridTableColumn( + colName: string, + idxPattern: IndexPattern, + includeSourceInColumns: boolean +) { const timeLabel = i18n.translate('discover.timeLabel', { defaultMessage: 'Time', }); const idxPatternField = idxPattern.getFieldByName(colName); + const shouldHide = colName === '_source' || colName === idxPattern.timeFieldName; const dataGridCol: EuiDataGridColumn = { id: colName, schema: idxPatternField?.type, isSortable: idxPatternField?.sortable, display: idxPatternField?.displayName, actions: { - showHide: true, - showMoveLeft: false, - showMoveRight: false, + showHide: shouldHide + ? false + : { + label: i18n.translate('discover.deleteColumn.label', { + defaultMessage: 'Delete column', + }), + iconType: 'cross', + }, + showMoveLeft: !includeSourceInColumns, + showMoveRight: !includeSourceInColumns, }, cellActions: idxPatternField ? getCellActions(idxPatternField) : [], }; diff --git a/src/plugins/discover/public/application/utils/columns.ts b/src/plugins/discover/public/application/utils/columns.ts index 062ca24e3ba4..d51fa395d73b 100644 --- a/src/plugins/discover/public/application/utils/columns.ts +++ b/src/plugins/discover/public/application/utils/columns.ts @@ -28,14 +28,19 @@ * under the License. */ +import { IndexPattern } from '../../opensearch_dashboards_services'; /** * Helper function to provide a fallback to a single _source column if the given array of columns - * is empty, and removes _source if there are more than 1 columns given + * is empty, and removes _source if there are more than 1 columns given. If there is only one column + * and it is the time field, add _source to the columns. * @param columns + * @param indexPattern */ -export function buildColumns(columns: string[]) { +export function buildColumns(columns: string[], indexPattern?: IndexPattern) { if (columns.length > 1 && columns.indexOf('_source') !== -1) { return columns.filter((col) => col !== '_source'); + } else if (columns.length === 1 && indexPattern && columns[0] === indexPattern.timeFieldName) { + return [...columns, '_source']; } else if (columns.length !== 0) { return columns; }