Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(VirtualScroll): initialise differ with trackByFn #11492

Merged
merged 2 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 8 additions & 1 deletion 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 Down Expand Up @@ -368,7 +369,13 @@ 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;
if (isPresent(this._records)) {
this._differ = this._iterableDiffers.find(this._records).create(this.virtualTrackBy);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move the this._differ creation logic to a new function? maybe _updateDiffer. it is duplicated in here and virtualScroll.

Also, can you add a new-line after @input()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure if you prefer I overwrite last commit or add a new one, so I went with the safer choice.

}
};


constructor(
Expand Down