Skip to content

virtual-scroll: add logic to correct the scroll position as user move… #11137

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 3 commits into from
May 7, 2018

Conversation

mmalerba
Copy link
Contributor

@mmalerba mmalerba commented May 3, 2018

…s toward the top

@mmalerba mmalerba requested review from amcdnl, jelbourn and crisbeto May 3, 2018 19:22
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label May 3, 2018
@mmalerba
Copy link
Contributor Author

mmalerba commented May 3, 2018

For some reason the changes from #11085 are showing up here, I have no idea why since that PR is shown as merged already.

Edit: seems to be fixed now, must've been some sort of glitch

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

LGTM, left a couple of nits.

// If the rendered content offset was specified as an offset to the end of the content,
// rewrite it as an offset to the start of the content.
if (this._renderedContentOffsetNeedsRewrite) {
this._ngZone.onStable.pipe(take(1)).subscribe(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Since you're subscribing to this._ngZone.onStable.pipe(take(1)) in both the if and the else clauses, it might make sense to combine them and do the check inside the subscription.

@@ -144,6 +144,13 @@ export class AutoSizeVirtualScrollStrategy implements VirtualScrollStrategy {
}
}

/** Implemented as part of VirtualScrollStrategy. */
Copy link
Member

Choose a reason for hiding this comment

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

nit: should this be @docs-private?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, should mark all of these hook funcions as @docs-private

sortBy(prop: 'name' | 'capital') {
this.statesObservable.next(this.states.map(s => ({...s})).sort((a, b) => {
const aProp = a[prop], bProp = b[prop];
return aProp < bProp ? -1 : (aProp > bProp ? 1 : 0);
Copy link
Member

Choose a reason for hiding this comment

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

nit: consider breaking up the nested ternary.

@@ -147,8 +147,15 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
private _differs: IterableDiffers,
/** The virtual scrolling viewport that these items are being rendered in. */
@SkipSelf() private _viewport: CdkVirtualScrollViewport) {
this.dataStream.subscribe(data => this._data = data);
this._viewport.renderedRangeStream.subscribe(range => this._onRenderedRangeChange(range));
this.dataStream.subscribe(data => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you ever need to unsubscribe from there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this one it should be ok, since the underlying Subject is completed in the destroy function, but renderedRangeStream I probably should, since that one belongs to the viewport

Copy link
Contributor

@amcdnl amcdnl left a comment

Choose a reason for hiding this comment

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

Looks good overall.

this._lastRenderedContentSize = viewport.measureRenderedContentSize();
this._averager.addSample(viewport.getRenderedRange(), this._lastRenderedContentSize);
this._updateTotalContentSize(this._lastRenderedContentSize);
}

private _checkRenderedContentOffset() {
Copy link
Member

Choose a reason for hiding this comment

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

Add method description?

@amcdnl
Copy link
Contributor

amcdnl commented May 6, 2018

Just tested this code in real world app, looks good there.

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn
Copy link
Member

jelbourn commented May 7, 2018

Looks like build file needs to be updated

@mmalerba mmalerba merged commit 604f743 into angular:virtual-scroll May 7, 2018
mmalerba added a commit that referenced this pull request May 22, 2018
* feat(virtual-scroll): fixed size virtual scroll (#9316)

* feat(virtual-scroll): fixed size virtual scroll

* address some comments

* change VirtualScrollStrategy interface a bit

* address some more comments

* fix lint & build

* chore: move virtual-scroll to cdk-experimental (#9974)

* virtual-scroll: simplify scroll listener logic (#10102)

* virtual-scroll: only move views that need to be moved (#10099)

* virtual-scroll: only move views that need to be moved

* address comments

* virtual-scroll: switch `throttleTime` to `sampleTime` (#10179)

* virtual-scroll: switch throttleTime to sampleTime

* add comment

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

* virtual-scroll: rename `Range` to `ListRange` to avoid confusion with native `Range` (#10220)

* virtual-scroll: add autosize scroll strategy (#10219)

* rename fixed size virtual scroll directive

* add autosize virtual scroll strategy

* add item size estimator class

* add logic for jumping rendered content based on scroll position

* address comments

* virtual-scroll: add `onContentRendered` hook to `VirtualScrollStrategy` (#10290)

* virtual-scroll: add `onContentRendered` hook to `VirtualScrollStrategy`

* address comemnts

* virtual-scroll: add incremental scroll logic in `AutosizeVirtualScrollStrategy` (#10504)

* virtual-scroll: add incremental scroll logic in `AutosizeVirtualScrollStrategy`.

This still has a couple issues that need to be ironed out and it doesn't
have the code for correcting the error between the predicted and actual
scroll position. (See various TODOs for additional things that need
work).

* fix lint

* address comments

* address comments

* fix bazel

* fix devapp

* fix lint

* cleanup

* virtual-scroll: rewrite offset in terms of "to-top" and fix a bug where items were removed too soon (#10986)

* rewrite offsets to the end of the rendered content as offsets to the
start

* add some more autosize demos for testing

* make sure not to remove too many items

* address comments

* virtual-scroll: address amcdnl's feedback (#10988)

* rewrite offsets to the end of the rendered content as offsets to the
start

* add some more autosize demos for testing

* make sure not to remove too many items

* virtual-scroll: address amcdnl's feedback

* virtual-scroll: fix updating when data changes and add trackBy demos (#11085)

* virtual-scroll: add logic to correct the scroll position as user move… (#11137)

* virtual-scroll: add logic to correct the scroll position as user moves toward the top

* address comments

* fix(scrolling): adds right to fix pushed content (#11192)

* fix(scrolling): adds right to fix pushed content

* chore: address feedback

* chore: address pr feedback

* virtual-scroll: add tests for fixed size items (#11255)

* virtual-scroll: add tests for fixed size items

* address comments

* fix bug in fixed size strategy

* fix bazel build

* virtual-scroll: add tests for `cdkVirtualFor` logic (#11275)

* merge fixed size test components into one

* add tests for cdkVirtualFor logic

* allow undefined to be explicitly passed as trackBy

* fix bazel build

* address comments

* add some basic tests (#11295)

* fix lint

* virtual-scroll: add e2e tests for autosize scroll strategy (#11345)

* set up virtual scroll page in e2e app

* add gulp task for e2e:watch

* add e2e tests for autosize

* address comments

* address comments

* fix lint and tests

* fix bad rebase

* fix(scrolling): scrollbar jump when data change on scroll (#11193)

* fix(scrolling): data change not updating size

* chore: remove todo

* chore: more performant fix for jump
@naveedahmed1
Copy link

I am using virtual-scroll in a component, when user clicks an item I get the current scroll position
which I get through '_renderedContentOffset' and store it in a singleton service.:

@ViewChild('virtualViewport') public virtualViewport: ElementRef;
.....
this._myService.scrollToOffset = this.virtualViewport['_renderedContentOffset'];

and when user goes back to the list page I set the scroll position of the div.

ngAfterViewInit() {
          let thisRef = this;
          setTimeout(function () {
                    let e = document.getElementById('list');
                    e.scrollTop = thisRef._as.scrollToOffset;
        }, 10);
}

I thought this would work, but it doesn't seem to work since it doesn't set the scroll position to where it was before clicking the item.

Does this indicate that there is some issue with the calculations of scroll position?

@mmalerba
Copy link
Contributor Author

@naveedahmed1 for autosize strategy, the currently displayed data is only an estimation of what should be shown at the current scroll position. Setting it back to the same scroll position later is not guaranteed to show the same data. Once the autosize strategy supports the scrollToIndex method you can use that to go to a specific index in the list.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants