Skip to content

Commit

Permalink
[Data Table] Remove extra column in split mode (#83193)
Browse files Browse the repository at this point in the history
* Fix extra column in split table

* Update table exports

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
Daniil and kibanamachine authored Nov 19, 2020
1 parent 5d05eea commit b263145
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 43 deletions.
34 changes: 7 additions & 27 deletions src/plugins/vis_type_table/public/legacy/agg_table/agg_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -112,7 +104,6 @@ export function KbnAggTable(config, RecursionHelper) {
if (!table) {
$scope.rows = null;
$scope.formattedColumns = null;
$scope.splitRow = null;
return;
}

Expand All @@ -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)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b263145

Please sign in to comment.