Skip to content

Commit e664ff0

Browse files
MKirovaMKirova
authored andcommitted
feat(igxGrid): Add onScroll event for grid.
1 parent 06d691f commit e664ff0

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

projects/igniteui-angular/src/lib/grids/common/events.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,15 @@ export interface IPinRowEventArgs extends IBaseEventArgs {
150150
/** Whether or noy the row is pinned or unpinned. */
151151
readonly isPinned: boolean;
152152
}
153+
154+
/**
155+
* Event emitted when a grid is scrolled.
156+
*/
157+
export interface IGridScrollEventArgs extends IBaseEventArgs {
158+
/** The scroll direction - vertical or horizontal. */
159+
direction: string;
160+
/** The original browser scroll event. */
161+
event: Event;
162+
/** The new scroll position */
163+
scrollPosition: number;
164+
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ import {
138138
ICellPosition,
139139
IRowToggleEventArgs,
140140
IColumnSelectionEventArgs,
141-
IPinRowEventArgs
141+
IPinRowEventArgs,
142+
IGridScrollEventArgs
142143
} from './common/events';
143144
import { IgxAdvancedFilteringDialogComponent } from './filtering/advanced-filtering/advanced-filtering-dialog.component';
144145
import { GridType } from './common/grid.interface';
@@ -361,6 +362,17 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
361362
@Output()
362363
public advancedFilteringExpressionsTreeChange = new EventEmitter<IFilteringExpressionsTree>();
363364

365+
/**
366+
* Emitted when grid is scrolled horizontally/vertically.
367+
* @example
368+
* ```html
369+
* <igx-grid #grid [data]="localData" [height]="'305px'" [autoGenerate]="true"
370+
* (onScroll)="onScroll($event)"></igx-grid>
371+
* ```
372+
*/
373+
@Output()
374+
public onScroll = new EventEmitter<IGridScrollEventArgs>();
375+
364376
/**
365377
* Gets/Sets the advanced filtering state.
366378
* @example
@@ -2654,6 +2666,12 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
26542666
this.disableTransitions = false;
26552667

26562668
this.hideOverlays();
2669+
const args: IGridScrollEventArgs = {
2670+
direction: 'vertical',
2671+
event: event,
2672+
scrollPosition: this.verticalScrollContainer.scrollPosition
2673+
};
2674+
this.onScroll.emit(args);
26572675
}
26582676

26592677
private horizontalScrollHandler = (event) => {
@@ -2669,6 +2687,8 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
26692687
});
26702688

26712689
this.hideOverlays();
2690+
const args: IGridScrollEventArgs = { direction: 'horizontal', event: event, scrollPosition: this.headerContainer.scrollPosition };
2691+
this.onScroll.emit(args);
26722692
}
26732693

26742694
/**

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,48 @@ describe('IgxGrid Component Tests #grid', () => {
15971597

15981598
expect(grid.verticalScrollContainer.getScroll().scrollTop).toEqual(200);
15991599
});
1600+
1601+
it('should emit onScroll event when scrolling horizontally/vertically', async() => {
1602+
const fix = TestBed.createComponent(IgxGridDefaultRenderingComponent);
1603+
fix.componentInstance.initColumnsRows(30, 10);
1604+
fix.detectChanges();
1605+
1606+
const grid = fix.componentInstance.grid;
1607+
grid.height = '300px';
1608+
grid.width = '300px';
1609+
fix.detectChanges();
1610+
1611+
spyOn(grid.onScroll, 'emit').and.callThrough();
1612+
let verticalScrollEvent;
1613+
let horizontalScrollEvent;
1614+
grid.verticalScrollContainer.getScroll().addEventListener('scroll', (evt) => verticalScrollEvent = evt);
1615+
grid.headerContainer.getScroll().addEventListener('scroll', (evt) => horizontalScrollEvent = evt);
1616+
1617+
grid.navigateTo(20, 0);
1618+
fix.detectChanges();
1619+
await wait(100);
1620+
fix.detectChanges();
1621+
1622+
expect(grid.onScroll.emit).toHaveBeenCalledTimes(1);
1623+
expect(grid.onScroll.emit).toHaveBeenCalledWith({
1624+
direction: 'vertical',
1625+
scrollPosition: grid.verticalScrollContainer.getScrollForIndex(20, true),
1626+
event: verticalScrollEvent
1627+
});
1628+
1629+
grid.navigateTo(20, 6);
1630+
fix.detectChanges();
1631+
await wait(100);
1632+
fix.detectChanges();
1633+
1634+
expect(grid.onScroll.emit).toHaveBeenCalledTimes(2);
1635+
expect(grid.onScroll.emit).toHaveBeenCalledWith({
1636+
direction: 'horizontal',
1637+
scrollPosition: grid.headerContainer.getScrollForIndex(6, true),
1638+
event: horizontalScrollEvent
1639+
});
1640+
1641+
});
16001642
});
16011643

16021644
describe('IgxGrid - Integration with other Igx Controls', () => {

0 commit comments

Comments
 (0)