@@ -14,6 +14,8 @@ const BBOX_X_ADJUST = 10;
14
14
const BBOX_Y_ADJUST = 10 ;
15
15
16
16
const use_minicharts = false ;
17
+ let sortOrder = false ;
18
+ let columnKey = '' ;
17
19
18
20
const removeStyles = async function ( ) {
19
21
const links = document . getElementsByTagName ( 'link' ) ;
@@ -35,7 +37,8 @@ const buildReportTable = function (
35
37
config ,
36
38
dataTable ,
37
39
updateColumnOrder ,
38
- element
40
+ element ,
41
+ updateSorting
39
42
) {
40
43
var dropTarget = null ;
41
44
const bounds = element . getBoundingClientRect ( ) ;
@@ -194,6 +197,7 @@ const buildReportTable = function (
194
197
return '' ;
195
198
}
196
199
} ) ;
200
+
197
201
var header_rows = table
198
202
. append ( 'thead' )
199
203
. selectAll ( 'tr' )
@@ -228,17 +232,24 @@ const buildReportTable = function (
228
232
. style ( 'font-size' , config . headerFontSize + 'px' )
229
233
. attr ( 'draggable' , true )
230
234
. call ( drag )
231
- . on ( 'mouseover' , function ( cell ) {
235
+ . on ( 'mouseover' , function ( cell ) {
232
236
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 ) ;
236
240
d3 . select ( '#tooltip' ) . classed ( 'hidden' , false ) ;
237
- return dropTarget = cell
241
+ return ( dropTarget = cell ) ;
238
242
} )
239
- . on ( 'mouseout' , function ( cell ) {
243
+ . on ( 'mouseout' , function ( cell ) {
240
244
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
+ }
242
253
} ) ;
243
254
244
255
var table_rows = table
@@ -376,7 +387,7 @@ const buildReportTable = function (
376
387
// Looker applies padding based on the top of the viz when opening a drill field but
377
388
// if part of the viz container is hidden underneath the iframe, the drill menu opens off screen
378
389
// 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 ) {
380
391
let event = {
381
392
metaKey : d3 . event . metaKey ,
382
393
pageX : d3 . event . pageX ,
@@ -631,6 +642,28 @@ looker.plugins.visualizations.add({
631
642
) ;
632
643
}
633
644
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
+
634
667
// BUILD THE VIS
635
668
// 1. Create object
636
669
// 2. Register options
@@ -639,7 +672,13 @@ looker.plugins.visualizations.add({
639
672
// console.log(config)
640
673
var dataTable = new VisPluginTableModel ( data , queryResponse , config ) ;
641
674
this . trigger ( 'registerOptions' , dataTable . getConfigOptions ( ) ) ;
642
- buildReportTable ( config , dataTable , updateColumnOrder , element ) ;
675
+ buildReportTable (
676
+ config ,
677
+ dataTable ,
678
+ updateColumnOrder ,
679
+ element ,
680
+ updateSorting
681
+ ) ;
643
682
644
683
// DEBUG OUTPUT AND DONE
645
684
// console.log('dataTable', dataTable)
0 commit comments