From b263145ba2fe94fd0adfeeaed21aba10792b23db Mon Sep 17 00:00:00 2001 From: Daniil Date: Thu, 19 Nov 2020 18:02:40 +0300 Subject: [PATCH] [Data Table] Remove extra column in split mode (#83193) * Fix extra column in split table * Update table exports Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../public/legacy/agg_table/agg_table.js | 34 ++++--------------- .../public/legacy/agg_table/agg_table.test.js | 28 +++++++-------- 2 files changed, 19 insertions(+), 43 deletions(-) diff --git a/src/plugins/vis_type_table/public/legacy/agg_table/agg_table.js b/src/plugins/vis_type_table/public/legacy/agg_table/agg_table.js index a9ec431e9d940..d3eac891c81f4 100644 --- a/src/plugins/vis_type_table/public/legacy/agg_table/agg_table.js +++ b/src/plugins/vis_type_table/public/legacy/agg_table/agg_table.js @@ -58,12 +58,8 @@ export function KbnAggTable(config, RecursionHelper) { }; self.toCsv = function (formatted) { - const rows = formatted ? $scope.rows : $scope.table.rows; - const columns = formatted ? [...$scope.formattedColumns] : [...$scope.table.columns]; - - if ($scope.splitRow && formatted) { - columns.unshift($scope.splitRow); - } + const rows = $scope.rows; + const columns = $scope.formattedColumns; const nonAlphaNumRE = /[^a-zA-Z0-9]/; const allDoubleQuoteRE = /"/g; @@ -77,7 +73,7 @@ export function KbnAggTable(config, RecursionHelper) { return val; } - let csvRows = []; + const csvRows = []; for (const row of rows) { const rowArray = []; for (const col of columns) { @@ -86,15 +82,11 @@ export function KbnAggTable(config, RecursionHelper) { formatted && col.formatter ? escape(col.formatter.convert(value)) : escape(value); rowArray.push(formattedValue); } - csvRows = [...csvRows, rowArray]; + csvRows.push(rowArray); } // add the columns to the rows - csvRows.unshift( - columns.map(function (col) { - return escape(formatted ? col.title : col.name); - }) - ); + csvRows.unshift(columns.map(({ title }) => escape(title))); return csvRows .map(function (row) { @@ -112,7 +104,6 @@ export function KbnAggTable(config, RecursionHelper) { if (!table) { $scope.rows = null; $scope.formattedColumns = null; - $scope.splitRow = null; return; } @@ -122,19 +113,12 @@ export function KbnAggTable(config, RecursionHelper) { if (typeof $scope.dimensions === 'undefined') return; - const { buckets, metrics, splitColumn, splitRow } = $scope.dimensions; + const { buckets, metrics } = $scope.dimensions; $scope.formattedColumns = table.columns .map(function (col, i) { const isBucket = buckets.find((bucket) => bucket.accessor === i); - const isSplitColumn = splitColumn - ? splitColumn.find((splitColumn) => splitColumn.accessor === i) - : undefined; - const isSplitRow = splitRow - ? splitRow.find((splitRow) => splitRow.accessor === i) - : undefined; - const dimension = - isBucket || isSplitColumn || metrics.find((metric) => metric.accessor === i); + const dimension = isBucket || metrics.find((metric) => metric.accessor === i); const formatter = dimension ? getFormatService().deserialize(dimension.format) @@ -147,10 +131,6 @@ export function KbnAggTable(config, RecursionHelper) { filterable: !!isBucket, }; - if (isSplitRow) { - $scope.splitRow = formattedColumn; - } - if (!dimension) return; const last = i === table.columns.length - 1; diff --git a/src/plugins/vis_type_table/public/legacy/agg_table/agg_table.test.js b/src/plugins/vis_type_table/public/legacy/agg_table/agg_table.test.js index c93fb4f8bd568..d97ef374def93 100644 --- a/src/plugins/vis_type_table/public/legacy/agg_table/agg_table.test.js +++ b/src/plugins/vis_type_table/public/legacy/agg_table/agg_table.test.js @@ -262,14 +262,12 @@ describe('Table Vis - AggTable Directive', function () { const $tableScope = $el.isolateScope(); const aggTable = $tableScope.aggTable; - $tableScope.table = { - columns: [ - { id: 'a', name: 'one' }, - { id: 'b', name: 'two' }, - { id: 'c', name: 'with double-quotes(")' }, - ], - rows: [{ a: 1, b: 2, c: '"foobar"' }], - }; + $tableScope.rows = [{ a: 1, b: 2, c: '"foobar"' }]; + $tableScope.formattedColumns = [ + { id: 'a', title: 'one' }, + { id: 'b', title: 'two' }, + { id: 'c', title: 'with double-quotes(")' }, + ]; expect(aggTable.toCsv()).toBe( 'one,two,"with double-quotes("")"' + '\r\n' + '1,2,"""foobar"""' + '\r\n' @@ -455,14 +453,12 @@ describe('Table Vis - AggTable Directive', function () { const aggTable = $tableScope.aggTable; const saveAs = sinon.stub(aggTable, '_saveAs'); - $tableScope.table = { - columns: [ - { id: 'a', name: 'one' }, - { id: 'b', name: 'two' }, - { id: 'c', name: 'with double-quotes(")' }, - ], - rows: [{ a: 1, b: 2, c: '"foobar"' }], - }; + $tableScope.rows = [{ a: 1, b: 2, c: '"foobar"' }]; + $tableScope.formattedColumns = [ + { id: 'a', title: 'one' }, + { id: 'b', title: 'two' }, + { id: 'c', title: 'with double-quotes(")' }, + ]; aggTable.csv.filename = 'somefilename.csv'; aggTable.exportAsCsv();