Skip to content

Commit 5ce97ee

Browse files
authored
Merge pull request #8389 from IgniteUI/ddincheva/bugFixs8294
fix(Grid): newly added column should be at the last position
2 parents 8caac83 + 8e9df0e commit 5ce97ee

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5159,6 +5159,7 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
51595159
*/
51605160
protected onColumnsChanged(change: QueryList<IgxColumnComponent>) {
51615161
const diff = this.columnListDiffer.diff(change);
5162+
51625163
if (this.autoGenerate && this.columnList.length === 0 && this._autoGeneratedCols.length > 0) {
51635164
// In Ivy if there are nested conditional templates the content children are re-evaluated
51645165
// hence autogenerated columns are cleared and need to be reset.
@@ -5168,15 +5169,14 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
51685169
if (diff) {
51695170
let added = false;
51705171
let removed = false;
5171-
5172-
this.initColumns(this.columnList);
5173-
5174-
51755172
diff.forEachAddedItem((record: IterableChangeRecord<IgxColumnComponent>) => {
51765173
this.onColumnInit.emit(record.item);
51775174
added = true;
5175+
record.item.pinned ? this._pinnedColumns.push(record.item) : this._unpinnedColumns.push(record.item);
51785176
});
51795177

5178+
this.initColumns(this.columnList);
5179+
51805180
diff.forEachRemovedItem((record: IterableChangeRecord<IgxColumnComponent | IgxColumnGroupComponent>) => {
51815181
const isColumnGroup = record.item instanceof IgxColumnGroupComponent;
51825182
if (!isColumnGroup) {

projects/igniteui-angular/src/lib/grids/grid/column.spec.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IgxGridComponent } from './grid.component';
55
import { IgxGridModule } from './public_api';
66
import { GridTemplateStrings, ColumnDefinitions } from '../../test-utils/template-strings.spec';
77
import { SampleTestData } from '../../test-utils/sample-test-data.spec';
8-
import { ColumnHiddenFromMarkupComponent, ColumnCellFormatterComponent, DynamicColumnsComponent } from '../../test-utils/grid-samples.spec';
8+
import { ColumnHiddenFromMarkupComponent, ColumnCellFormatterComponent, DynamicColumnsComponent, GridAddColumnComponent } from '../../test-utils/grid-samples.spec';
99
import { configureTestSuite } from '../../test-utils/configure-suite';
1010
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
1111
import { IgxStringFilteringOperand } from '../../data-operations/filtering-condition';
@@ -27,7 +27,8 @@ describe('IgxGrid - Column properties #grid', () => {
2727
ColumnCellFormatterComponent,
2828
ColumnHaederClassesComponent,
2929
ColumnHiddenFromMarkupComponent,
30-
DynamicColumnsComponent
30+
DynamicColumnsComponent,
31+
GridAddColumnComponent
3132
],
3233
imports: [IgxGridModule, NoopAnimationsModule]
3334
})
@@ -167,6 +168,34 @@ describe('IgxGrid - Column properties #grid', () => {
167168
expect(grid.columnList.last.field).toMatch('Name');
168169
}));
169170

171+
it('should add new column at the correct visible index', fakeAsync(() => {
172+
const fix = TestBed.createComponent(GridAddColumnComponent);
173+
fix.detectChanges();
174+
const grid = fix.componentInstance.grid;
175+
const maxVindex = fix.componentInstance.columns.length - 1;
176+
177+
// add to unpinned area
178+
fix.componentInstance.columns.push({ field: 'City', width: 150, movable: true, type: 'string' });
179+
fix.detectChanges();
180+
181+
let cityCol = grid.getColumnByName('City');
182+
expect(cityCol.visibleIndex).toEqual(maxVindex + 1);
183+
184+
// remove the newly added column
185+
fix.componentInstance.columns.pop();
186+
fix.detectChanges();
187+
188+
cityCol = grid.getColumnByName('City');
189+
expect(cityCol).not.toBeDefined();
190+
191+
// add to pinned area
192+
fix.componentInstance.columns.push({ field: 'City', width: 150, movable: true, type: 'string', pinned: true });
193+
fix.detectChanges();
194+
195+
cityCol = grid.getColumnByName('City');
196+
expect(cityCol.visibleIndex).toEqual( 1);
197+
}));
198+
170199
it('should apply columnWidth on columns that don\'t have explicit width', () => {
171200
const fix = TestBed.createComponent(ColumnCellFormatterComponent);
172201
fix.componentInstance.grid.columnWidth = '200px';

projects/igniteui-angular/src/lib/test-utils/grid-samples.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,33 @@ export class ColumnHiddenFromMarkupComponent extends BasicGridComponent {
122122
data = SampleTestData.personIDNameData();
123123
}
124124

125+
@Component({
126+
template: `
127+
<igx-grid #grid1 [data]="data" [columnPinning]='true' [width]="'900px'" [height]="'600px'">
128+
<igx-column *ngFor="let c of columns" [field]="c.field"
129+
[header]="c.field"
130+
[movable]="c.movable"
131+
[width]="c.width"
132+
[editable]="true"
133+
[pinned]="c.pinned"
134+
[dataType]="c.type">
135+
</igx-column>
136+
</igx-grid>
137+
`
138+
})
139+
export class GridAddColumnComponent extends BasicGridComponent implements OnInit {
140+
public columns: Array<any>;
141+
data = SampleTestData.contactInfoDataFull();
142+
public ngOnInit(): void {
143+
this.columns = [
144+
{ field: 'ID', width: 150, movable: true, type: 'string', pinned: true },
145+
{ field: 'CompanyName', width: 150, movable: true, type: 'string'},
146+
{ field: 'ContactName', width: 150, movable: true, type: 'string' },
147+
{ field: 'ContactTitle', width: 150, movable: true, type: 'string' },
148+
{ field: 'Address', width: 150, movable: true, type: 'string' }];
149+
}
150+
}
151+
125152
@Component({
126153
template: GridTemplateStrings.declareGrid('', '',
127154
ColumnDefinitions.idNameFormatter)

0 commit comments

Comments
 (0)