@@ -3772,6 +3772,7 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
37723772 }
37733773
37743774 /**
3775+ * Reorder columns in the main columnList and _columns collections.
37753776 * @hidden
37763777 */
37773778 protected _moveColumns ( from : IgxColumnComponent , to : IgxColumnComponent , pos : DropPosition ) {
@@ -3801,44 +3802,44 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
38013802 }
38023803
38033804 /**
3805+ * Reorders columns inside the passed column collection.
3806+ * When reordering column group collection, the collection is not flattened.
3807+ * In all other cases, the columns collection is flattened, this is why adittional calculations on the dropIndex are done.
38043808 * @hidden
38053809 */
3806- protected _reorderColumns ( from : IgxColumnComponent , to : IgxColumnComponent , position : DropPosition , columnCollection : any [ ] ) {
3810+ protected _reorderColumns ( from : IgxColumnComponent , to : IgxColumnComponent , position : DropPosition , columnCollection : any [ ] ,
3811+ inGroup = false ) {
38073812 const fromIndex = columnCollection . indexOf ( from ) ;
3808- const childColumnsCount = from . allChildren . length ;
3809- // remove item(s) to be moved.
3810- const fromCollection = columnCollection . splice ( fromIndex , childColumnsCount + 1 ) ;
3811-
3813+ const childColumnsCount = inGroup ? 1 : from . allChildren . length + 1 ;
3814+ columnCollection . splice ( fromIndex , childColumnsCount ) ;
38123815 let dropIndex = columnCollection . indexOf ( to ) ;
3813-
38143816 if ( position === DropPosition . AfterDropTarget ) {
38153817 dropIndex ++ ;
3816- if ( to . columnGroup ) {
3818+ if ( ! inGroup && to . columnGroup ) {
38173819 dropIndex += to . allChildren . length ;
38183820 }
38193821 }
3820- columnCollection . splice ( dropIndex , 0 , ... fromCollection ) ;
3822+ columnCollection . splice ( dropIndex , 0 , from ) ;
38213823 }
3824+
38223825 /**
3826+ * Reorder column group collection.
38233827 * @hidden
38243828 */
38253829 protected _moveChildColumns ( parent : IgxColumnComponent , from : IgxColumnComponent , to : IgxColumnComponent , pos : DropPosition ) {
38263830 const buffer = parent . children . toArray ( ) ;
3827- this . _reorderColumns ( from , to , pos , buffer ) ;
3831+ this . _reorderColumns ( from , to , pos , buffer , true ) ;
38283832 parent . children . reset ( buffer ) ;
38293833 }
38303834 /**
3831- * Moves a column to the specified drop target.
3835+ * Places a column before or after the specified target column .
38323836 * @example
38333837 * ```typescript
3834- * grid.moveColumn(compName, persDetails );
3838+ * grid.moveColumn(column, target );
38353839 * ```
38363840 */
3837- public moveColumn ( column : IgxColumnComponent , dropTarget : IgxColumnComponent , pos : DropPosition = DropPosition . None ) {
3841+ public moveColumn ( column : IgxColumnComponent , target : IgxColumnComponent , pos : DropPosition = DropPosition . None ) {
38383842
3839- if ( column === dropTarget ) {
3840- return ;
3841- }
38423843 let position = pos ;
38433844 if ( position === DropPosition . None ) {
38443845 warningShown = showMessage (
@@ -3847,21 +3848,18 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
38473848 warningShown ) ;
38483849 position = DropPosition . AfterDropTarget ;
38493850 }
3850- if ( ( column . level !== dropTarget . level ) ||
3851- ( column . topLevelParent !== dropTarget . topLevelParent ) ) {
3851+
3852+ if ( column === target || ( column . level !== target . level ) ||
3853+ ( column . topLevelParent !== target . topLevelParent ) ) {
38523854 return ;
38533855 }
38543856
38553857 this . endEdit ( true ) ;
38563858 if ( column . level ) {
3857- this . _moveChildColumns ( column . parent , column , dropTarget , position ) ;
3858- }
3859-
3860- if ( dropTarget . pinned && column . pinned ) {
3861- this . _reorderColumns ( column , dropTarget , position , this . _pinnedColumns ) ;
3859+ this . _moveChildColumns ( column . parent , column , target , position ) ;
38623860 }
38633861
3864- if ( dropTarget . pinned && ! column . pinned ) {
3862+ if ( target . pinned && ! column . pinned ) {
38653863 column . pin ( ) ;
38663864 if ( ! this . isPinningToStart ) {
38673865 if ( pos === DropPosition . AfterDropTarget ) {
@@ -3870,36 +3868,30 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
38703868 position = DropPosition . None ;
38713869 }
38723870 }
3873- this . _reorderColumns ( column , dropTarget , position , this . _pinnedColumns ) ;
3871+ this . _reorderColumns ( column , target , position , this . _pinnedColumns ) ;
38743872 }
38753873
3876- if ( ! dropTarget . pinned && column . pinned ) {
3874+ if ( ! target . pinned && column . pinned ) {
38773875 column . unpin ( ) ;
3878- let list = [ ] ;
3879-
3880- if ( this . pinnedColumns . indexOf ( column ) === - 1 && this . pinnedColumns . indexOf ( dropTarget ) === - 1 ) {
3881- list = this . _unpinnedColumns ;
3882- } else {
3883- list = this . _pinnedColumns ;
3884- }
3885-
3886- const fi = list . indexOf ( column ) ;
3887- const ti = list . indexOf ( dropTarget ) ;
3876+ }
38883877
3889- if ( pos === DropPosition . BeforeDropTarget && fi < ti ) {
3890- position = DropPosition . BeforeDropTarget ;
3891- } else if ( pos === DropPosition . AfterDropTarget && fi > ti ) {
3892- position = DropPosition . AfterDropTarget ;
3893- } else {
3894- position = DropPosition . None ;
3895- }
3878+ if ( target . pinned && column . pinned ) {
3879+ this . _reorderColumns ( column , target , position , this . _pinnedColumns ) ;
38963880 }
38973881
3898- if ( ! dropTarget . pinned ) {
3899- this . _reorderColumns ( column , dropTarget , position , this . _unpinnedColumns ) ;
3882+ if ( ! target . pinned && ! column . pinned ) {
3883+ this . _reorderColumns ( column , target , position , this . _unpinnedColumns ) ;
39003884 }
39013885
3902- this . _moveColumns ( column , dropTarget , position ) ;
3886+ this . _moveColumns ( column , target , position ) ;
3887+ this . _columnsReordered ( column , target ) ;
3888+ }
3889+
3890+ /**
3891+ * Notiy changes, reset cache and populateVisibleIndexes.
3892+ * @hidden
3893+ */
3894+ private _columnsReordered ( column : IgxColumnComponent , target ) {
39033895 this . notifyChanges ( ) ;
39043896 if ( this . hasColumnLayouts ) {
39053897 this . columns . filter ( x => x . columnLayout ) . forEach ( x => x . populateVisibleIndexes ( ) ) ;
@@ -3910,7 +3902,7 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
39103902
39113903 const args = {
39123904 source : column ,
3913- target : dropTarget
3905+ target : target
39143906 } ;
39153907
39163908 this . onColumnMovingEnd . emit ( args ) ;
0 commit comments