Skip to content

Commit

Permalink
🐛 filter out non visible columsn from datatable to export/inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Jan 4, 2022
1 parent 24f5be4 commit 5336fd2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/plugins/data/common/exports/export_csv.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { Datatable } from 'src/plugins/expressions';
import { FormatFactory } from '../../../field_formats/common';
import { createEscapeValue } from './escape_value';
import { getVisibleColumns } from './table_filter';

export const LINE_FEED_CHARACTER = '\r\n';
export const CSV_MIME_TYPE = 'text/plain;charset=utf-8';
Expand All @@ -28,10 +29,14 @@ export function datatableToCSV(
{ csvSeparator, quoteValues, formatFactory, raw, escapeFormulaValues }: CSVOptions
) {
const escapeValues = createEscapeValue(quoteValues, escapeFormulaValues);

// If columns have some meta information, use them to filter out hidden columns
const filteredColumns = getVisibleColumns(columns);
// const filteredColumns = columns;
// Build the header row by its names
const header = columns.map((col) => escapeValues(col.name));
const header = filteredColumns.map((col) => escapeValues(col.name));

const formatters = columns.reduce<Record<string, ReturnType<FormatFactory>>>(
const formatters = filteredColumns.reduce<Record<string, ReturnType<FormatFactory>>>(
(memo, { id, meta }) => {
memo[id] = formatFactory(meta?.params);
return memo;
Expand All @@ -41,7 +46,7 @@ export function datatableToCSV(

// Convert the array of row objects to an array of row arrays
const csvRows = rows.map((row) => {
return columns.map((column) =>
return filteredColumns.map((column) =>
escapeValues(raw ? row[column.id] : formatters[column.id].convert(row[column.id]))
);
});
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/data/common/exports/table_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { DatatableColumn } from 'src/plugins/expressions';

export function getVisibleColumns(columns: DatatableColumn[]) {
const columnsHaveMetaInfo = columns.some(({ meta }) => meta.dimensionName);
return columnsHaveMetaInfo ? columns.filter(({ meta }) => meta?.dimensionName) : columns;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { FormattedMessage } from '@kbn/i18n-react';
import { i18n } from '@kbn/i18n';

import { getVisibleColumns } from '../../../../common/exports/table_filter';
import { DataViewRow, DataViewColumn } from '../types';
import { IUiSettingsClient } from '../../../../../../core/public';
import { Datatable, DatatableColumn } from '../../../../../expressions/public';
Expand Down Expand Up @@ -156,7 +157,7 @@ export class DataTableFormat extends Component<DataTableFormatProps, DataTableFo
};
}

const columns = data.columns.map((dataColumn: any, index: number) => {
const columns = getVisibleColumns(data.columns).map((dataColumn: any, index: number) => {
const formatParams = { id: 'string', ...dataColumn.meta.params };
const fieldFormatter = fieldFormats.deserialize(formatParams);
const filterable = isFilterable(dataColumn);
Expand Down

0 comments on commit 5336fd2

Please sign in to comment.