@@ -3848,6 +3848,7 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
38483848 }
38493849
38503850 /**
3851+ * Reorder columns in the main columnList and _columns collections.
38513852 * @hidden
38523853 */
38533854 protected _moveColumns ( from : IgxColumnComponent , to : IgxColumnComponent , pos : DropPosition ) {
@@ -3877,94 +3878,79 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
38773878 }
38783879
38793880 /**
3881+ * Reorders columns inside the passed column collection.
3882+ * When reordering column group collection, the collection is not flattened.
3883+ * In all other cases, the columns collection is flattened, this is why adittional calculations on the dropIndex are done.
38803884 * @hidden
38813885 */
3882- protected _reorderColumns ( from : IgxColumnComponent , to : IgxColumnComponent , position : DropPosition , columnCollection : any [ ] ) {
3886+ protected _reorderColumns ( from : IgxColumnComponent , to : IgxColumnComponent , position : DropPosition , columnCollection : any [ ] ,
3887+ inGroup = false ) {
38833888 const fromIndex = columnCollection . indexOf ( from ) ;
3884- const childColumnsCount = from . allChildren . length ;
3885- // remove item(s) to be moved.
3886- const fromCollection = columnCollection . splice ( fromIndex , childColumnsCount + 1 ) ;
3887-
3889+ const childColumnsCount = inGroup ? 1 : from . allChildren . length + 1 ;
3890+ columnCollection . splice ( fromIndex , childColumnsCount ) ;
38883891 let dropIndex = columnCollection . indexOf ( to ) ;
3889-
38903892 if ( position === DropPosition . AfterDropTarget ) {
38913893 dropIndex ++ ;
3892- if ( to . columnGroup ) {
3894+ if ( ! inGroup && to . columnGroup ) {
38933895 dropIndex += to . allChildren . length ;
38943896 }
38953897 }
3896- columnCollection . splice ( dropIndex , 0 , ... fromCollection ) ;
3898+ columnCollection . splice ( dropIndex , 0 , from ) ;
38973899 }
3900+
38983901 /**
3902+ * Reorder column group collection.
38993903 * @hidden
39003904 */
39013905 protected _moveChildColumns ( parent : IgxColumnComponent , from : IgxColumnComponent , to : IgxColumnComponent , pos : DropPosition ) {
39023906 const buffer = parent . children . toArray ( ) ;
3903- this . _reorderColumns ( from , to , pos , buffer ) ;
3907+ this . _reorderColumns ( from , to , pos , buffer , true ) ;
39043908 parent . children . reset ( buffer ) ;
39053909 }
39063910 /**
3907- * Moves a column to the specified drop target.
3911+ * Places a column before or after the specified target column .
39083912 * @example
39093913 * ```typescript
3910- * grid.moveColumn(column, dropTarget );
3914+ * grid.moveColumn(column, target );
39113915 * ```
39123916 */
3913- public moveColumn ( column : IgxColumnComponent , dropTarget : IgxColumnComponent , pos : DropPosition = DropPosition . AfterDropTarget ) {
3917+ public moveColumn ( column : IgxColumnComponent , target : IgxColumnComponent , pos : DropPosition = DropPosition . AfterDropTarget ) {
39143918
3915- if ( column === dropTarget ) {
3916- return ;
3917- }
3918- let position = pos ;
3919- if ( ( column . level !== dropTarget . level ) ||
3920- ( column . topLevelParent !== dropTarget . topLevelParent ) ) {
3919+ if ( column === target || ( column . level !== target . level ) ||
3920+ ( column . topLevelParent !== target . topLevelParent ) ) {
39213921 return ;
39223922 }
39233923
39243924 this . endEdit ( true ) ;
39253925 if ( column . level ) {
3926- this . _moveChildColumns ( column . parent , column , dropTarget , position ) ;
3926+ this . _moveChildColumns ( column . parent , column , target , pos ) ;
39273927 }
39283928
3929- if ( dropTarget . pinned && column . pinned ) {
3930- this . _reorderColumns ( column , dropTarget , position , this . _pinnedColumns ) ;
3931- }
3932-
3933- if ( dropTarget . pinned && ! column . pinned ) {
3929+ if ( target . pinned && ! column . pinned ) {
39343930 column . pin ( ) ;
3935- if ( ! this . isPinningToStart ) {
3936- if ( pos === DropPosition . AfterDropTarget ) {
3937- position = DropPosition . AfterDropTarget ;
3938- }
3939- }
3940- this . _reorderColumns ( column , dropTarget , position , this . _pinnedColumns ) ;
39413931 }
39423932
3943- if ( ! dropTarget . pinned && column . pinned ) {
3933+ if ( ! target . pinned && column . pinned ) {
39443934 column . unpin ( ) ;
3945- let list = [ ] ;
3946-
3947- if ( this . pinnedColumns . indexOf ( column ) === - 1 && this . pinnedColumns . indexOf ( dropTarget ) === - 1 ) {
3948- list = this . _unpinnedColumns ;
3949- } else {
3950- list = this . _pinnedColumns ;
3951- }
3952-
3953- const fi = list . indexOf ( column ) ;
3954- const ti = list . indexOf ( dropTarget ) ;
3935+ }
39553936
3956- if ( pos === DropPosition . BeforeDropTarget && fi < ti ) {
3957- position = DropPosition . BeforeDropTarget ;
3958- } else if ( pos === DropPosition . AfterDropTarget && fi > ti ) {
3959- position = DropPosition . AfterDropTarget ;
3960- }
3937+ if ( target . pinned && column . pinned ) {
3938+ this . _reorderColumns ( column , target , pos , this . _pinnedColumns ) ;
39613939 }
39623940
3963- if ( ! dropTarget . pinned ) {
3964- this . _reorderColumns ( column , dropTarget , position , this . _unpinnedColumns ) ;
3941+ if ( ! target . pinned && ! column . pinned ) {
3942+ this . _reorderColumns ( column , target , pos , this . _unpinnedColumns ) ;
39653943 }
39663944
3967- this . _moveColumns ( column , dropTarget , position ) ;
3945+ this . _moveColumns ( column , target , pos ) ;
3946+ this . _columnsReordered ( column , target ) ;
3947+ }
3948+
3949+ /**
3950+ * Notiy changes, reset cache and populateVisibleIndexes.
3951+ * @hidden
3952+ */
3953+ private _columnsReordered ( column : IgxColumnComponent , target ) {
39683954 this . notifyChanges ( ) ;
39693955 if ( this . hasColumnLayouts ) {
39703956 this . columns . filter ( x => x . columnLayout ) . forEach ( x => x . populateVisibleIndexes ( ) ) ;
@@ -3975,7 +3961,7 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
39753961
39763962 const args = {
39773963 source : column ,
3978- target : dropTarget
3964+ target : target
39793965 } ;
39803966
39813967 this . onColumnMovingEnd . emit ( args ) ;
0 commit comments