Skip to content

Commit 299a68b

Browse files
masimplomanucorporat
authored andcommitted
fix(VirtualScroll): initialise differ with trackByFn (#11492)
* fix(VirtualScroll): initialise differ with trackByFn * fix(VirtualScroll): DRY up differ initialization
1 parent 646d736 commit 299a68b

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/components/virtual-scroll/test/basic/app.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export class E2EPage {
5858
window.location.reload(true);
5959
}
6060

61+
trackByFn(index: number, item: any) {
62+
return item.value;
63+
}
64+
6165
}
6266

6367

src/components/virtual-scroll/test/basic/main.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
<button ion-button (click)="pushPage()">Push Virtual Scroll Page</button>
2020
</div>
2121

22-
<ion-list [virtualScroll]="items"
23-
[headerFn]="headerFn">
22+
23+
<ion-list [virtualScroll]="items" [headerFn]="headerFn" [virtualTrackBy]="trackByFn"
24+
approxItemHeight="46px">
2425

2526
<ion-item-divider *virtualHeader="let header">
26-
Header: {{header}}
27+
Header: {{ header }}
2728
</ion-item-divider>
2829

2930
<ion-item *virtualItem="let item">
30-
Item: {{item.value}} {{item.someMethod()}}
31+
Item: {{ item.value }} {{ item.someMethod() }}
3132
</ion-item>
3233

3334
</ion-list>

src/components/virtual-scroll/virtual-scroll.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
234234
};
235235
_queue: number = SCROLL_QUEUE_NO_CHANGES;
236236
_recordSize: number = 0;
237+
_virtualTrackBy: TrackByFn;
237238

238239
@ContentChild(VirtualItem) _itmTmp: VirtualItem;
239240
@ContentChild(VirtualHeader) _hdrTmp: VirtualHeader;
@@ -249,9 +250,7 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
249250
@Input()
250251
set virtualScroll(val: any) {
251252
this._records = val;
252-
if (isBlank(this._differ) && isPresent(val)) {
253-
this._differ = this._iterableDiffers.find(val).create(this.virtualTrackBy);
254-
}
253+
this._updateDiffer();
255254
}
256255

257256
/**
@@ -368,7 +367,12 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
368367
/**
369368
* @input {function} Same as `ngForTrackBy` which can be used on `ngFor`.
370369
*/
371-
@Input() virtualTrackBy: TrackByFn;
370+
@Input()
371+
set virtualTrackBy(val: TrackByFn) {
372+
if (!isPresent(val)) return;
373+
this._virtualTrackBy = val;
374+
this._updateDiffer();
375+
};
372376

373377

374378
constructor(
@@ -481,6 +485,12 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
481485
return null;
482486
}
483487

488+
private _updateDiffer(): void {
489+
if (isBlank(this._differ) && isPresent(this._records)) {
490+
this._differ = this._iterableDiffers.find(this._records).create(this.virtualTrackBy);
491+
}
492+
}
493+
484494
/**
485495
* @hidden
486496
* DOM WRITE
@@ -632,7 +642,7 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
632642
var stopAtHeight = (data.scrollTop + data.renderHeight);
633643

634644
processRecords(stopAtHeight, records, cells,
635-
this._hdrFn, this._ftrFn, data);
645+
this._hdrFn, this._ftrFn, data);
636646
}
637647

638648
// ******** DOM READ ****************

0 commit comments

Comments
 (0)