[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>
This commit is contained in:
Daniil 2020-11-19 18:02:40 +03:00 committed by GitHub
parent 5d05eeaab9
commit b263145ba2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 43 deletions

View file

@ -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;

View file

@ -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();