Skip to content

Commit

Permalink
fix(VirtualScroll): initialise differ with trackByFn (#11492)
Browse files Browse the repository at this point in the history
* fix(VirtualScroll): initialise differ with trackByFn

* fix(VirtualScroll): DRY up differ initialization
  • Loading branch information
masimplo authored and manucorporat committed May 4, 2017
1 parent 646d736 commit 299a68b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/components/virtual-scroll/test/basic/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export class E2EPage {
window.location.reload(true);
}

trackByFn(index: number, item: any) {
return item.value;
}

}


Expand Down
9 changes: 5 additions & 4 deletions src/components/virtual-scroll/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
<button ion-button (click)="pushPage()">Push Virtual Scroll Page</button>
</div>

<ion-list [virtualScroll]="items"
[headerFn]="headerFn">

<ion-list [virtualScroll]="items" [headerFn]="headerFn" [virtualTrackBy]="trackByFn"
approxItemHeight="46px">

<ion-item-divider *virtualHeader="let header">
Header: {{header}}
Header: {{ header }}
</ion-item-divider>

<ion-item *virtualItem="let item">
Item: {{item.value}} {{item.someMethod()}}
Item: {{ item.value }} {{ item.someMethod() }}
</ion-item>

</ion-list>
Expand Down
20 changes: 15 additions & 5 deletions src/components/virtual-scroll/virtual-scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
};
_queue: number = SCROLL_QUEUE_NO_CHANGES;
_recordSize: number = 0;
_virtualTrackBy: TrackByFn;

@ContentChild(VirtualItem) _itmTmp: VirtualItem;
@ContentChild(VirtualHeader) _hdrTmp: VirtualHeader;
Expand All @@ -249,9 +250,7 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
@Input()
set virtualScroll(val: any) {
this._records = val;
if (isBlank(this._differ) && isPresent(val)) {
this._differ = this._iterableDiffers.find(val).create(this.virtualTrackBy);
}
this._updateDiffer();
}

/**
Expand Down Expand Up @@ -368,7 +367,12 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
/**
* @input {function} Same as `ngForTrackBy` which can be used on `ngFor`.
*/
@Input() virtualTrackBy: TrackByFn;
@Input()
set virtualTrackBy(val: TrackByFn) {
if (!isPresent(val)) return;
this._virtualTrackBy = val;
this._updateDiffer();
};


constructor(
Expand Down Expand Up @@ -481,6 +485,12 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
return null;
}

private _updateDiffer(): void {
if (isBlank(this._differ) && isPresent(this._records)) {
this._differ = this._iterableDiffers.find(this._records).create(this.virtualTrackBy);
}
}

/**
* @hidden
* DOM WRITE
Expand Down Expand Up @@ -632,7 +642,7 @@ export class VirtualScroll implements DoCheck, AfterContentInit, OnDestroy {
var stopAtHeight = (data.scrollTop + data.renderHeight);

processRecords(stopAtHeight, records, cells,
this._hdrFn, this._ftrFn, data);
this._hdrFn, this._ftrFn, data);
}

// ******** DOM READ ****************
Expand Down

0 comments on commit 299a68b

Please sign in to comment.