Skip to content

Commit 96322ac

Browse files
authored
Merge pull request #1853 from IgniteUI/mkirova/fix-1844
fix(igxFor): Ensure scroll offsets are updated when igxFor changes.
2 parents 57a9895 + f55e06a commit 96322ac

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,52 @@ describe('IgxVirtual directive - simple template', () => {
891891
chunkSize = firstRecChildren.length;
892892
expect(chunkSize).toEqual(10);
893893
});
894+
it('should update horizontal scroll offsets if igxForOf changes. ', () => {
895+
const fix = TestBed.createComponent(HorizontalVirtualComponent);
896+
fix.componentRef.hostView.detectChanges();
897+
fix.detectChanges();
898+
fix.componentInstance.width = '500px';
899+
fix.componentInstance.cols = [
900+
{ field: '1', width: 100 },
901+
{ field: '2', width: 200 },
902+
{ field: '3', width: 200 },
903+
{ field: '4', width: 200 },
904+
{ field: '5', width: 300 }
905+
];
906+
fix.componentRef.hostView.detectChanges();
907+
fix.detectChanges();
908+
const displayContainer = fix.nativeElement.querySelector('igx-display-container');
909+
910+
fix.componentInstance.scrollLeft(50);
911+
912+
fix.detectChanges();
913+
914+
expect(parseInt(displayContainer.style.left, 10)).toEqual(-50);
915+
916+
fix.componentInstance.cols = [
917+
{ field: '1', width: 100 }
918+
];
919+
fix.detectChanges();
920+
921+
expect(parseInt(displayContainer.style.left, 10)).toEqual(0);
922+
923+
});
924+
it('should update vertical scroll offsets if igxForOf changes. ', (done) => {
925+
const fix = TestBed.createComponent(VerticalVirtualComponent);
926+
fix.componentRef.hostView.detectChanges();
927+
fix.detectChanges();
928+
fix.componentInstance.scrollTop(5);
929+
fix.detectChanges();
930+
const displayContainer = fix.nativeElement.querySelector('igx-display-container');
931+
932+
expect(parseInt(displayContainer.style.top, 10)).toEqual(-5);
933+
fix.componentInstance.data = [{'1': 1, '2': 2, '3': 3, '4': 4}];
934+
fix.detectChanges();
935+
setTimeout(() => {
936+
expect(parseInt(displayContainer.style.top, 10)).toEqual(0);
937+
done();
938+
}, 10);
939+
});
894940
});
895941

896942
/** igxFor for testing */

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
240240
this._zone.run(() => {
241241
this._applyChanges(changes);
242242
this.cdr.markForCheck();
243+
this._updateScrollOffset();
243244
});
244245
}
245246
}
@@ -797,6 +798,22 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
797798
}
798799
}
799800
}
801+
802+
private _updateScrollOffset() {
803+
let scrollOffset = 0;
804+
if (this.igxForScrollOrientation === 'horizontal') {
805+
scrollOffset = this.hScroll ? this.hScroll.scrollLeft - this.hCache[this.state.startIndex] : 0;
806+
this.dc.instance._viewContainer.element.nativeElement.style.left = -scrollOffset + 'px';
807+
} else {
808+
const count = this.isRemote ?
809+
this.totalItemCount :
810+
this.igxForOf ? this.igxForOf.length : 0;
811+
const vScroll = this.vh.instance.elementRef.nativeElement;
812+
scrollOffset = vScroll ? vScroll.scrollTop - this.state.startIndex * (this._virtHeight / count) : 0;
813+
scrollOffset = scrollOffset !== parseInt(this.igxForItemSize, 10) ? scrollOffset : 0;
814+
this.dc.instance._viewContainer.element.nativeElement.style.top = -(scrollOffset) + 'px';
815+
}
816+
}
800817
}
801818

802819
export function getTypeNameForDebugging(type: any): string {

0 commit comments

Comments
 (0)