@@ -139,12 +139,16 @@ define([
139
139
} = state ;
140
140
141
141
$ ( this . _wrapSelector ( '#vp_gbVariable' ) ) . val ( variable ) ;
142
- $ ( this . _wrapSelector ( '#vp_gbBy' ) ) . val ( groupby . join ( ',' ) ) ;
142
+ $ ( this . _wrapSelector ( '#vp_gbBy' ) ) . val ( groupby . map ( col => col . code ) . join ( ',' ) ) ;
143
143
$ ( this . _wrapSelector ( '#vp_gbBy' ) ) . data ( 'list' , groupby ) ;
144
- $ ( this . _wrapSelector ( '#vp_gbByGrouper' ) ) . prop ( 'checked' , useGrouper ) ;
145
- $ ( this . _wrapSelector ( '#vp_gbByGrouperNumber' ) ) . val ( grouperNumber ) ;
146
- $ ( this . _wrapSelector ( '#vp_gbByGrouperPeriod' ) ) . val ( grouperPeriod ) ;
147
- $ ( this . _wrapSelector ( '#vp_gbDisplay' ) ) . val ( display . join ( ',' ) ) ;
144
+ if ( useGrouper ) {
145
+ $ ( this . _wrapSelector ( '#vp_gbByGrouper' ) ) . removeAttr ( 'disabled' ) ;
146
+ $ ( this . _wrapSelector ( '#vp_gbByGrouper' ) ) . prop ( 'checked' , useGrouper ) ;
147
+ $ ( this . _wrapSelector ( '#vp_gbByGrouperNumber' ) ) . val ( grouperNumber ) ;
148
+ $ ( this . _wrapSelector ( '#vp_gbByGrouperPeriod' ) ) . val ( grouperPeriod ) ;
149
+ $ ( this . _wrapSelector ( '.vp-gb-by-grouper-box' ) ) . show ( ) ;
150
+ }
151
+ $ ( this . _wrapSelector ( '#vp_gbDisplay' ) ) . val ( display . map ( col => col . code ) . join ( ',' ) ) ;
148
152
$ ( this . _wrapSelector ( '#vp_gbDisplay' ) ) . data ( 'list' , display ) ;
149
153
$ ( this . _wrapSelector ( '#vp_gbMethod' ) ) . val ( method ) ;
150
154
$ ( this . _wrapSelector ( '#vp_gbMethodSelector' ) ) . val ( method ) ;
@@ -303,7 +307,7 @@ define([
303
307
page . appendFormatLine ( '<label for="{0}" class="{1}">{2}</label>' , 'vp_gbBy' , 'vp-orange-text wp80' , 'Groupby' ) ;
304
308
page . appendFormatLine ( '<input type="text" id="{0}" placeholder="{1}" disabled/>' , 'vp_gbBy' , 'Groupby coluns' ) ;
305
309
page . appendFormatLine ( '<button id="{0}" class="{1}">{2}</button>' , 'vp_gbBySelect' , 'vp-button wp50' , 'Edit' ) ;
306
- page . appendFormatLine ( '<label style="display: none;" ><input type="checkbox" id="{0}"/><span>{1}</span></label>' , 'vp_gbByGrouper' , 'Grouper' ) ;
310
+ page . appendFormatLine ( '<label><input type="checkbox" id="{0}" disabled /><span>{1}</span></label>' , 'vp_gbByGrouper' , 'Grouper' ) ;
307
311
page . appendFormatLine ( '<div class="{0}" style="display:none;">' , 'vp-gb-by-grouper-box' ) ;
308
312
page . appendFormatLine ( '<input type="number" id="{0}" class="{1}"/>' , 'vp_gbByGrouperNumber' , 'vp-gb-by-number' ) ;
309
313
page . appendFormatLine ( '<select id="{0}">' , 'vp_gbByGrouperPeriod' ) ;
@@ -531,7 +535,11 @@ define([
531
535
openInnerPopup ( targetSelector , title = 'Select columns' , includeList = [ ] ) {
532
536
this . popup . type = 'column' ;
533
537
this . popup . targetSelector = targetSelector ;
534
- this . renderColumnSelector ( this . popup . targetSelector . data ( 'list' ) , includeList ) ;
538
+ var previousList = this . popup . targetSelector . data ( 'list' ) ;
539
+ if ( previousList ) {
540
+ previousList = previousList . map ( col => col . code )
541
+ }
542
+ this . renderColumnSelector ( previousList , includeList ) ;
535
543
536
544
// set title
537
545
$ ( this . _wrapSelector ( '.' + APP_POPUP_BOX + ' .' + APP_TITLE ) ) . text ( title ) ;
@@ -664,11 +672,12 @@ define([
664
672
$ ( document ) . on ( 'change' , this . _wrapSelector ( '#vp_gbBy' ) , function ( event ) {
665
673
var colList = event . colList ;
666
674
that . state . groupby = colList ;
667
-
668
- if ( colList && colList . length == 1 ) {
669
- $ ( that . _wrapSelector ( '#vp_gbByGrouper' ) ) . parent ( ) . show ( ) ;
675
+
676
+ if ( colList && colList . length == 1
677
+ && colList [ 0 ] . dtype . includes ( 'datetime' ) ) {
678
+ $ ( that . _wrapSelector ( '#vp_gbByGrouper' ) ) . removeAttr ( 'disabled' ) ;
670
679
} else {
671
- $ ( that . _wrapSelector ( '#vp_gbByGrouper' ) ) . parent ( ) . hide ( ) ;
680
+ $ ( that . _wrapSelector ( '#vp_gbByGrouper' ) ) . attr ( 'disabled' , true ) ;
672
681
}
673
682
} ) ;
674
683
@@ -763,20 +772,38 @@ define([
763
772
var idx = $ ( that . _wrapSelector ( '.vp-gb-adv-col' ) ) . index ( this ) ;
764
773
765
774
// if there's change, reset display namings
766
- var previousList = that . state . advColList [ idx ] ;
767
- if ( ! previousList || colList . length !== previousList . length
768
- || ! colList . slice ( ) . sort ( ) . every ( ( val , idx ) => { return val === previousList . slice ( ) . sort ( ) [ idx ] } ) ) {
769
- that . state . advNamingList = [ ]
770
- $ ( that . _wrapSelector ( '.vp-gb-adv-naming' ) ) . val ( '' ) ;
771
- $ ( that . _wrapSelector ( '.vp-gb-adv-naming' ) ) . data ( 'dict' , { } ) ;
775
+ // var previousList = that.state.advColList[idx];
776
+ // if (!previousList || colList.length !== previousList.length
777
+ // || !colList.map(col=>col.code).slice().sort().every((val, idx) => {
778
+ // return val === previousList.map(col=>col.code).slice().sort()[idx]
779
+ // })) {
780
+ // that.state.advNamingList = []
781
+ // $(this).parent().find('.vp-gb-adv-naming').val('');
782
+ // $(this).parent().find('.vp-gb-adv-naming').data('dict', {});
783
+ // }
784
+ var namingDict = that . state . advNamingList [ idx ] ;
785
+ if ( namingDict ) {
786
+ // namingDict = namingDict.filter(key => colList.map(col=>col.code).includes(key));
787
+ Object . keys ( namingDict ) . forEach ( key => {
788
+ if ( ! colList . map ( col => col . code ) . includes ( key ) ) {
789
+ delete namingDict [ key ] ;
790
+ }
791
+ } ) ;
792
+ that . state . advNamingList [ idx ] = namingDict ;
793
+ $ ( this ) . parent ( ) . find ( '.vp-gb-adv-naming' ) . val ( Object . values ( namingDict ) . map ( val => "'" + val + "'" ) . join ( ',' ) ) ;
794
+ $ ( this ) . parent ( ) . find ( '.vp-gb-adv-naming' ) . data ( 'dict' , namingDict ) ;
772
795
}
773
796
774
797
that . state . advColList [ idx ] = colList ;
775
798
} ) ;
776
799
777
800
// edit target columns
778
801
$ ( document ) . on ( 'click' , this . _wrapSelector ( '.vp-gb-adv-col-selector' ) , function ( ) {
779
- that . openInnerPopup ( $ ( this ) . parent ( ) . find ( '.vp-gb-adv-col' ) , 'Select columns' , that . state . display ) ;
802
+ var includeList = that . state . display ;
803
+ if ( includeList && includeList . length > 0 ) {
804
+ includeList = includeList . map ( col => col . code ) ;
805
+ }
806
+ that . openInnerPopup ( $ ( this ) . parent ( ) . find ( '.vp-gb-adv-col' ) , 'Select columns' , includeList ) ;
780
807
} ) ;
781
808
782
809
// select method
@@ -815,6 +842,9 @@ define([
815
842
$ ( document ) . on ( 'click' , this . _wrapSelector ( '.vp-gb-adv-naming-selector' ) , function ( ) {
816
843
var parentDiv = $ ( this ) . parent ( ) ;
817
844
var columns = $ ( parentDiv ) . find ( '.vp-gb-adv-col' ) . data ( 'list' ) ;
845
+ if ( columns && columns . length > 0 ) {
846
+ columns = columns . map ( col => col . code ) ;
847
+ }
818
848
var method = $ ( parentDiv ) . find ( '.vp-gb-adv-method' ) . val ( ) ;
819
849
if ( ! method || method == '' || method == "''" ) {
820
850
// set focus on selecting method tag
@@ -899,7 +929,7 @@ define([
899
929
if ( that . popup . type == 'column' ) {
900
930
var colList = that . popup . ColSelector . getColumnList ( ) ;
901
931
902
- $ ( that . popup . targetSelector ) . val ( colList . join ( ',' ) ) ;
932
+ $ ( that . popup . targetSelector ) . val ( colList . map ( col => { return col . code } ) . join ( ',' ) ) ;
903
933
$ ( that . popup . targetSelector ) . data ( 'list' , colList ) ;
904
934
$ ( that . popup . targetSelector ) . trigger ( { type : 'change' , colList : colList } ) ;
905
935
that . closeInnerPopup ( ) ;
@@ -911,13 +941,11 @@ define([
911
941
var key = $ ( tags [ i ] ) . data ( 'code' ) ;
912
942
var val = $ ( tags [ i ] ) . val ( ) ;
913
943
if ( val && val != '' ) {
914
- dict [ key ] = "'" + val + "'" ;
944
+ dict [ key ] = val ;
915
945
}
916
946
}
917
947
918
- console . log ( dict ) ;
919
-
920
- $ ( that . popup . targetSelector ) . val ( Object . values ( dict ) . join ( ',' ) ) ;
948
+ $ ( that . popup . targetSelector ) . val ( Object . values ( dict ) . map ( val => "'" + val + "'" ) . join ( ',' ) ) ;
921
949
$ ( that . popup . targetSelector ) . data ( 'dict' , dict ) ;
922
950
$ ( that . popup . targetSelector ) . trigger ( { type : 'change' , namingDict : dict } ) ;
923
951
that . closeInnerPopup ( ) ;
@@ -980,6 +1008,10 @@ define([
980
1008
display, method, advanced, allocateTo, resetIndex
981
1009
} = this . state ;
982
1010
1011
+ // mapping colList states
1012
+ groupby = groupby . map ( col => col . code ) ;
1013
+ display = display . map ( col => col . code ) ;
1014
+
983
1015
//====================================================================
984
1016
// Allocation
985
1017
//====================================================================
@@ -1000,9 +1032,8 @@ define([
1000
1032
// Grouper
1001
1033
if ( useGrouper ) {
1002
1034
byStr = vpCommon . formatString ( "pd.Grouper(key={0}, freq='{1}')" , byStr , grouperNumber + grouperPeriod ) ;
1003
- }
1004
-
1005
- if ( resetIndex == true ) {
1035
+ } else if ( resetIndex == true ) {
1036
+ // as_index option cannot use with Grouper -> use .reset_index() at the end
1006
1037
byStr += ', as_index=False' ;
1007
1038
}
1008
1039
// variable & groupby columns & option
@@ -1039,6 +1070,9 @@ define([
1039
1070
}
1040
1071
for ( var i = 0 ; i < advItemTags . length ; i ++ ) {
1041
1072
var advColumns = $ ( advItemTags [ i ] ) . find ( '.vp-gb-adv-col' ) . data ( 'list' ) ;
1073
+ if ( advColumns && advColumns . length > 0 ) {
1074
+ advColumns = advColumns . map ( col => col . code ) ;
1075
+ }
1042
1076
var advMethod = $ ( advItemTags [ i ] ) . find ( '.vp-gb-adv-method' ) . val ( ) ;
1043
1077
var advNaming = $ ( advItemTags [ i ] ) . find ( '.vp-gb-adv-naming' ) . data ( 'dict' ) ;
1044
1078
if ( ! advMethod || advMethod == '' || advMethod == "''" ) {
@@ -1047,6 +1081,9 @@ define([
1047
1081
if ( advColumns && advColumns . length > 0 ) {
1048
1082
advColumns . forEach ( col => {
1049
1083
var naming = advNaming [ col ] ;
1084
+ if ( naming && naming != '' ) {
1085
+ naming = "'" + naming + "'" ;
1086
+ }
1050
1087
if ( Object . keys ( advColumnDict ) . includes ( col ) ) {
1051
1088
advColumnDict [ col ] . push ( { method : advMethod , naming : naming } )
1052
1089
} else {
@@ -1055,7 +1092,11 @@ define([
1055
1092
} ) ;
1056
1093
1057
1094
} else {
1058
- advColumnDict [ 'nothing' ] . push ( { method : advMethod , naming : advNaming [ advMethod ] } ) ;
1095
+ var naming = advNaming [ advMethod ] ;
1096
+ if ( naming && naming != '' ) {
1097
+ naming = "'" + naming + "'" ;
1098
+ }
1099
+ advColumnDict [ 'nothing' ] . push ( { method : advMethod , naming : naming } ) ;
1059
1100
}
1060
1101
}
1061
1102
@@ -1065,7 +1106,7 @@ define([
1065
1106
var noColList = advColumnDict [ 'nothing' ] ;
1066
1107
if ( noColList . length == 1 ) {
1067
1108
// 1 method
1068
- if ( noColList [ 0 ] . naming && noColList [ 0 ] . naming != undefined ) {
1109
+ if ( noColList [ 0 ] . naming && noColList [ 0 ] . naming != '' ) {
1069
1110
methodStr . appendFormat ( "[({0}, {1})]" , noColList [ 0 ] . naming , noColList [ 0 ] . method ) ;
1070
1111
} else {
1071
1112
methodStr . appendFormat ( "{0}" , noColList [ 0 ] . method ) ;
@@ -1074,7 +1115,7 @@ define([
1074
1115
// more than 1 method
1075
1116
var tmpList = [ ] ;
1076
1117
noColList . forEach ( obj => {
1077
- if ( obj . naming && obj . naming != undefined ) {
1118
+ if ( obj . naming && obj . naming != '' ) {
1078
1119
tmpList . push ( vpCommon . formatString ( "({0}, {1})" , obj . naming , obj . method ) ) ;
1079
1120
} else {
1080
1121
tmpList . push ( obj . method ) ;
@@ -1106,7 +1147,7 @@ define([
1106
1147
var tmpList2 = [ ] ;
1107
1148
var useTuple = false ;
1108
1149
colList . forEach ( obj => {
1109
- if ( obj . naming && obj . naming != undefined ) {
1150
+ if ( obj . naming && obj . naming != '' ) {
1110
1151
tmpList2 . push ( vpCommon . formatString ( "({0}, {1})" , obj . naming , obj . method ) ) ;
1111
1152
useTuple = true ;
1112
1153
} else {
@@ -1129,6 +1170,11 @@ define([
1129
1170
//================================================================
1130
1171
methodStr . appendFormat ( '{0}()' , method ) ;
1131
1172
}
1173
+
1174
+ // when using as_index option with Grouper, use .reset_index()
1175
+ if ( useGrouper && resetIndex ) {
1176
+ methodStr . append ( '.reset_index()' ) ;
1177
+ }
1132
1178
// display columns
1133
1179
code . appendFormat ( '{0}.{1}' , colStr , methodStr . toString ( ) ) ;
1134
1180
0 commit comments