Skip to content

Commit 3863468

Browse files
committed
feat(sort): add first attempt to sort basic table
1 parent 21075af commit 3863468

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

src/d3loader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
import {select, selectAll, event} from 'd3-selection';
44
import {transition} from 'd3-transition';
55
import {drag} from 'd3-drag';
6+
import {ascending, descending} from 'd3-array';
67

7-
export {select, selectAll, event, transition, drag};
8+
export {select, selectAll, event, transition, drag, ascending, descending};

src/report_table.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const BBOX_X_ADJUST = 10;
1414
const BBOX_Y_ADJUST = 10;
1515

1616
const use_minicharts = false;
17+
let sortOrder = false;
18+
let columnKey = '';
1719

1820
const removeStyles = async function () {
1921
const links = document.getElementsByTagName('link');
@@ -35,7 +37,8 @@ const buildReportTable = function (
3537
config,
3638
dataTable,
3739
updateColumnOrder,
38-
element
40+
element,
41+
updateSorting
3942
) {
4043
var dropTarget = null;
4144
const bounds = element.getBoundingClientRect();
@@ -194,6 +197,7 @@ const buildReportTable = function (
194197
return '';
195198
}
196199
});
200+
197201
var header_rows = table
198202
.append('thead')
199203
.selectAll('tr')
@@ -228,17 +232,24 @@ const buildReportTable = function (
228232
.style('font-size', config.headerFontSize + 'px')
229233
.attr('draggable', true)
230234
.call(drag)
231-
.on('mouseover', function(cell) {
235+
.on('mouseover', function (cell) {
232236
d3.select('#tooltip')
233-
.style('left', d3.event.x + 'px')
234-
.style('top', d3.event.y + 'px')
235-
.html(cell.label);
237+
.style('left', d3.event.x + 'px')
238+
.style('top', d3.event.y + 'px')
239+
.html(cell.label);
236240
d3.select('#tooltip').classed('hidden', false);
237-
return dropTarget = cell
241+
return (dropTarget = cell);
238242
})
239-
.on('mouseout', function(cell) {
243+
.on('mouseout', function (cell) {
240244
d3.select('#tooltip').classed('hidden', true);
241-
return dropTarget = null
245+
return (dropTarget = null);
246+
})
247+
.on('click', function (cell) {
248+
const key = cell.column.id;
249+
if (cell.type !== 'pivot0') {
250+
updateSorting(sortOrder, key);
251+
sortOrder = !sortOrder;
252+
}
242253
});
243254

244255
var table_rows = table
@@ -376,7 +387,7 @@ const buildReportTable = function (
376387
// Looker applies padding based on the top of the viz when opening a drill field but
377388
// if part of the viz container is hidden underneath the iframe, the drill menu opens off screen
378389
// We make a simple copy of the d3.event and account for pageYOffser as MouseEvent attributes are read only.
379-
if (d.links !== [] && d.links[0].url) {
390+
if (Array.isArray(d.links) && d.links.length !== 0 && d.links[0].url) {
380391
let event = {
381392
metaKey: d3.event.metaKey,
382393
pageX: d3.event.pageX,
@@ -631,6 +642,28 @@ looker.plugins.visualizations.add({
631642
);
632643
}
633644

645+
// Here goes the sorting then the vis is built again
646+
// If order = true => ascending
647+
// If order = false => descending
648+
const updateSorting = (order, key) => {
649+
columnKey = key;
650+
this.trigger('updateConfig', [
651+
{sorting: order ? 'ascending' : 'descending'},
652+
]);
653+
};
654+
655+
if (columnKey !== '') {
656+
if (config.sorting === 'ascending') {
657+
data.sort((a, b) => {
658+
return d3.ascending(a[columnKey].value, b[columnKey].value);
659+
});
660+
} else if (config.sorting === 'descending') {
661+
data.sort((a, b) => {
662+
return d3.descending(a[columnKey].value, b[columnKey].value);
663+
});
664+
}
665+
}
666+
634667
// BUILD THE VIS
635668
// 1. Create object
636669
// 2. Register options
@@ -639,7 +672,13 @@ looker.plugins.visualizations.add({
639672
// console.log(config)
640673
var dataTable = new VisPluginTableModel(data, queryResponse, config);
641674
this.trigger('registerOptions', dataTable.getConfigOptions());
642-
buildReportTable(config, dataTable, updateColumnOrder, element);
675+
buildReportTable(
676+
config,
677+
dataTable,
678+
updateColumnOrder,
679+
element,
680+
updateSorting
681+
);
643682

644683
// DEBUG OUTPUT AND DONE
645684
// console.log('dataTable', dataTable)

src/vis_table_plugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ const tableModelCoreOptions = {
190190
default: false,
191191
order: 100,
192192
},
193+
sorting: {
194+
type: 'string',
195+
default: 'ascending',
196+
},
193197
};
194198
/**
195199
* Represents an "enriched data object" with additional methods and properties for data vis

0 commit comments

Comments
 (0)