Skip to content

Commit 3502e9f

Browse files
committed
feat(sort): adding sort using column and pivot key
1 parent 3863468 commit 3502e9f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/report_table.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const BBOX_Y_ADJUST = 10;
1616
const use_minicharts = false;
1717
let sortOrder = false;
1818
let columnKey = '';
19+
let pivotKey = '';
1920

2021
const removeStyles = async function () {
2122
const links = document.getElementsByTagName('link');
@@ -245,9 +246,12 @@ const buildReportTable = function (
245246
return (dropTarget = null);
246247
})
247248
.on('click', function (cell) {
248-
const key = cell.column.id;
249+
console.log('cell');
250+
console.log(cell);
251+
const column_key = cell.column.modelField.name;
252+
const pivot = cell.column.pivot_key;
249253
if (cell.type !== 'pivot0') {
250-
updateSorting(sortOrder, key);
254+
updateSorting(sortOrder, column_key, pivot);
251255
sortOrder = !sortOrder;
252256
}
253257
});
@@ -645,21 +649,32 @@ looker.plugins.visualizations.add({
645649
// Here goes the sorting then the vis is built again
646650
// If order = true => ascending
647651
// If order = false => descending
648-
const updateSorting = (order, key) => {
652+
const updateSorting = (order, key, pivot) => {
649653
columnKey = key;
654+
pivotKey = pivot;
650655
this.trigger('updateConfig', [
651656
{sorting: order ? 'ascending' : 'descending'},
652657
]);
653658
};
654659

655660
if (columnKey !== '') {
661+
console.log(`columnKey: ${columnKey}`);
662+
console.log(`pivotKey: ${pivotKey}`);
656663
if (config.sorting === 'ascending') {
657664
data.sort((a, b) => {
658-
return d3.ascending(a[columnKey].value, b[columnKey].value);
665+
const value_1 =
666+
pivotKey === '' ? a[columnKey].value : a[columnKey][pivotKey].value;
667+
const value_2 =
668+
pivotKey === '' ? b[columnKey].value : b[columnKey][pivotKey].value;
669+
return d3.ascending(value_1, value_2);
659670
});
660671
} else if (config.sorting === 'descending') {
661672
data.sort((a, b) => {
662-
return d3.descending(a[columnKey].value, b[columnKey].value);
673+
const value_1 =
674+
pivotKey === '' ? a[columnKey].value : a[columnKey][pivotKey].value;
675+
const value_2 =
676+
pivotKey === '' ? b[columnKey].value : b[columnKey][pivotKey].value;
677+
return d3.descending(value_1, value_2);
663678
});
664679
}
665680
}

webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22

33
module.exports = {
4+
mode: 'development',
45
entry: './src/report_table.js',
56
output: {
67
filename: 'bundle.js',
@@ -12,7 +13,7 @@ module.exports = {
1213
{
1314
test: /\.css$/i,
1415
use: [
15-
{ loader: 'style-loader', options: { injectType: 'lazyStyleTag' } },
16+
{loader: 'style-loader', options: {injectType: 'lazyStyleTag'}},
1617
'css-loader',
1718
],
1819
},

0 commit comments

Comments
 (0)