@@ -12,11 +12,7 @@ export class GroupSort extends Module {
1212 initialize ( ) {
1313 // @ts -expect-error groupSort is a custom propoerty see registerTableOption above
1414 if ( this . table . options . groupSort ) {
15- this . table . on ( 'dataSorting' , ( ) => {
16- this . table . blockRedraw ( ) ;
17- this . _sortGroups ( ) ;
18- this . table . restoreRedraw ( ) ;
19- } ) ;
15+ this . subscribe ( 'sort-changed' , this . _sortGroups . bind ( this ) ) ;
2016 }
2117 }
2218
@@ -34,89 +30,111 @@ export class GroupSort extends Module {
3430 }
3531 }
3632
37- _areGroupsEqual ( oldGroups : unknown [ ] , newGroups : unknown [ ] ) {
38- return (
39- oldGroups &&
40- newGroups . length === oldGroups . length &&
41- newGroups . every ( ( value , index ) => value === oldGroups [ index ] )
42- ) ;
43- }
44-
4533 _sortGroups ( ) {
4634 const grpArray = Array . isArray ( this . table . options . groupBy )
4735 ? this . table . options . groupBy
4836 : [ this . table . options . groupBy ] ;
4937 const { options } = this . table ;
50- options . groupValues = [ ] ;
5138
5239 const validGrps = grpArray . filter ( Boolean ) . length > 0 ;
53- if ( this . table && this . table . options . sortMode !== 'remote' && validGrps ) {
54- const { modules } = this . table ;
40+ if ( this . table && options . sortMode !== 'remote' && validGrps ) {
41+ let groupFunc = grpArray [ 0 ] ;
42+ const grpField = groupFunc as string ;
43+ if ( typeof groupFunc === 'string' ) {
44+ groupFunc = function ( data ) {
45+ return data [ grpField ] ;
46+ } ;
47+ }
5548
56- const groupRows = modules . groupRows ;
57- const rows = this . table . rowManager . rows ;
58- groupRows . configureGroupSetup ( ) ;
59- groupRows . generateGroups ( rows ) ;
49+ const groupsByKey : { [ key : string ] : unknown [ ] } = { } ;
50+ if ( groupFunc ) {
51+ const rows = this . table . rowManager . rows ;
52+ rows . forEach ( ( row : InternalColumnTotal ) => {
53+ const grpVal = groupFunc ( row . data ) ;
54+ let groupRows = groupsByKey [ grpVal ] ;
55+ if ( ! groupRows ) {
56+ groupRows = [ ] ;
57+ groupsByKey [ grpVal ] = groupRows ;
58+ }
59+ groupRows . push ( row ) ;
60+ } ) ;
61+ }
6062
61- const groupTotalsRows : InternalColumnTotal [ ] = [ ] ;
62- const columnCalcs = modules . columnCalcs ;
63+ let groupTotalsRows : InternalColumnTotal [ ] = [ ] ;
64+ const columnCalcs = this . table . modules . columnCalcs ;
6365 const field = columnCalcs . botCalcs [ 0 ] . field ;
64- groupRows . groupList . forEach ( ( group : { key : string ; rows : { data : unknown } [ ] } ) => {
65- const row = columnCalcs . generateBottomRow ( group . rows ) ;
66- row . data [ field ] = group . key ;
67- row . key = group . key ;
68- row . rows = group . rows ;
66+ for ( const [ key , rows ] of Object . entries ( groupsByKey ) ) {
67+ const row = columnCalcs . generateBottomRow ( rows ) ;
68+ row . data [ field ] = key ;
69+ row . key = key ;
70+ row . rows = rows ;
6971 row . generateCells ( ) ;
7072 groupTotalsRows . push ( row ) ;
71- } ) ;
72-
73- const sortListActual : unknown [ ] = [ ] ;
74- //build list of valid sorters and trigger column specific callbacks before sort begins
75- const sorter = modules . sort ;
76- const sortList : InternalSortItem [ ] = options . sortOrderReverse
77- ? sorter . sortList . slice ( ) . reverse ( )
78- : sorter . sortList ;
79- sortList . forEach ( ( item ) => {
80- let sortObj ;
81-
82- if ( item . column ) {
83- sortObj = item . column . modules . sort ;
84-
85- if ( sortObj ) {
86- //if no sorter has been defined, take a guess
87- if ( ! sortObj . sorter ) {
88- sortObj . sorter = sorter . findSorter ( item . column ) ;
89- }
90-
91- item . params =
92- typeof sortObj . params === 'function'
93- ? sortObj . params ( item . column . getComponent ( ) , item . dir )
94- : sortObj . params ;
95-
96- sortListActual . push ( item ) ;
97- }
98- }
99- } ) ;
100-
101- //sort data
102- if ( sortListActual . length ) {
103- sorter . _sortItems ( groupTotalsRows , sortListActual ) ;
104- } else {
105- groupTotalsRows . sort ( ( a , b ) => {
106- const index = b . rows . length - a . rows . length ;
107- if ( index === 0 ) {
108- return a . key . localeCompare ( b . key ) ;
109- }
110- return index ;
111- } ) ;
11273 }
74+
75+ groupTotalsRows = this . _sortGroupTotals ( groupTotalsRows ) ;
11376 const groupValues : string [ ] = [ ] ;
11477 groupTotalsRows . forEach ( ( colTotals ) => {
11578 groupValues . push ( colTotals . data [ field ] as string ) ;
11679 } ) ;
11780
118- this . table ?. setGroupValues ( [ groupValues ] ) ;
81+ const originalGroupVals = ( options . groupValues ?? [ [ ] ] ) [ 0 ] ?? [ ] ;
82+ if ( ! this . _areGroupsEqual ( groupValues , originalGroupVals ) ) {
83+ this . table ?. setGroupValues ( [ groupValues ] ) ;
84+ }
85+ } else {
86+ this . table ?. setGroupValues ( [ ] ) ;
87+ }
88+ }
89+
90+ _areGroupsEqual ( oldGroups : unknown [ ] , newGroups : unknown [ ] ) {
91+ return (
92+ oldGroups &&
93+ newGroups . length === oldGroups . length &&
94+ newGroups . every ( ( value , index ) => value === oldGroups [ index ] )
95+ ) ;
96+ }
97+
98+ _sortGroupTotals ( groupTotalsRows : InternalColumnTotal [ ] ) {
99+ const sortListActual : unknown [ ] = [ ] ;
100+ const { modules, options } = this . table ;
101+
102+ const sorter = modules . sort ;
103+ const sortList : InternalSortItem [ ] = options . sortOrderReverse
104+ ? sorter . sortList . slice ( ) . reverse ( )
105+ : sorter . sortList ;
106+ sortList . forEach ( ( item ) => {
107+ if ( item . column ) {
108+ const sortObj = item . column . modules . sort ;
109+ if ( sortObj ) {
110+ //if no sorter has been defined, take a guess
111+ if ( ! sortObj . sorter ) {
112+ sortObj . sorter = sorter . findSorter ( item . column ) ;
113+ }
114+
115+ item . params =
116+ typeof sortObj . params === 'function'
117+ ? sortObj . params ( item . column . getComponent ( ) , item . dir )
118+ : sortObj . params ;
119+
120+ sortListActual . push ( item ) ;
121+ }
122+ }
123+ } ) ;
124+
125+ //sort data
126+ if ( sortListActual . length ) {
127+ sorter . _sortItems ( groupTotalsRows , sortListActual ) ;
128+ } else {
129+ groupTotalsRows . sort ( ( a , b ) => {
130+ const index = b . rows . length - a . rows . length ;
131+ if ( index === 0 ) {
132+ return a . key . localeCompare ( b . key ) ;
133+ }
134+ return index ;
135+ } ) ;
119136 }
137+ return groupTotalsRows ;
120138 }
121139}
122140
0 commit comments