Skip to content

virtual-scroll: allow user to pass Observable<T[]> #10158

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

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 10 additions & 7 deletions src/cdk-experimental/scrolling/virtual-for-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {CollectionViewer, DataSource, Range, StaticArrayDataSource} from '@angular/cdk/collections';
import {ArrayDataSource, CollectionViewer, DataSource, Range} from '@angular/cdk/collections';
import {
Directive,
DoCheck,
Expand Down Expand Up @@ -35,7 +35,7 @@ import {CdkVirtualScrollViewport} from './virtual-scroll-viewport';
/** The context for an item rendered by `CdkVirtualForOf` */
export type CdkVirtualForOfContext<T> = {
$implicit: T;
cdkVirtualForOf: NgIterable<T> | DataSource<T>;
cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T>;
Copy link
Member

@jelbourn jelbourn Feb 28, 2018

Choose a reason for hiding this comment

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

I'm confused about this WRT your comment about needing the length (vs having NgIterable here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was originally going to support Observable<NgIterable<T>>, but decided there wasn't really much point. Still want to support plain NgIterable<T> so that you can pass all of the same stuff to *cdkVirtualFor that you would to *ngFor

index: number;
count: number;
first: boolean;
Expand All @@ -61,15 +61,18 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy

/** The DataSource to display. */
@Input()
get cdkVirtualForOf(): NgIterable<T> | DataSource<T> { return this._cdkVirtualForOf; }
set cdkVirtualForOf(value: NgIterable<T> | DataSource<T>) {
get cdkVirtualForOf(): DataSource<T> | Observable<T[]> | NgIterable<T> {
return this._cdkVirtualForOf;
}
set cdkVirtualForOf(value: DataSource<T> | Observable<T[]> | NgIterable<T>) {
this._cdkVirtualForOf = value;
const ds = value instanceof DataSource ? value :
// Slice the value since NgIterable may be array-like rather than an array.
new StaticArrayDataSource<T>(Array.prototype.slice.call(value));
// Slice the value if its an NgIterable to ensure we're working with an array.
new ArrayDataSource<T>(
value instanceof Observable ? value : Array.prototype.slice.call(value));
this._dataSourceChanges.next(ds);
}
_cdkVirtualForOf: NgIterable<T> | DataSource<T>;
_cdkVirtualForOf: DataSource<T> | Observable<T[]> | NgIterable<T>;

/**
* The `TrackByFunction` to use for tracking changes. The `TrackByFunction` takes the index and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {DataSource} from './data-source';


/** DataSource wrapper for a native array. */
export class StaticArrayDataSource<T> implements DataSource<T> {
constructor(private _data: T[]) {}
export class ArrayDataSource<T> implements DataSource<T> {
constructor(private _data: T[] | Observable<T[]>) {}

connect(): Observable<T[]> {
return observableOf(this._data);
return this._data instanceof Observable ? this._data : observableOf(this._data);
}

disconnect() {}
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/collections/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

export * from './array-data-source';
export * from './collection-viewer';
export * from './data-source';
export * from './selection';
export * from './static-array-data-source';
export {
UniqueSelectionDispatcher,
UniqueSelectionDispatcherListener,
Expand Down