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

[Feature Request] Fire events in v-virtual-scroll #12094

Closed
dschreij opened this issue Aug 21, 2020 · 6 comments · Fixed by #17265
Closed

[Feature Request] Fire events in v-virtual-scroll #12094

dschreij opened this issue Aug 21, 2020 · 6 comments · Fixed by #17265
Assignees
Labels
Milestone

Comments

@dschreij
Copy link
Contributor

Problem to solve

According to the docs, v-virtual-scroll doesn't fire any events, but it would be nice if it would fire one if it (almost) reaches the end of its items stream. You could hook this into a fetching function that retrieves more items and easily implement 'infinite scrolling' this way.

Proposed solution

When v-virtual-scroll reaches the end of its items stream, fire an event that designates this (for instance $emit('end-of-stream')). One could even make it configurable at how many items before the end of the stream the event should be fired?

@ghost ghost added the S: triage label Aug 21, 2020
@KaelWD
Copy link
Member

KaelWD commented Aug 21, 2020

It emits scroll, you could compare e.currentTarget.scrollHeight and e.currentTarget.scrollTop yourself.

@dschreij
Copy link
Contributor Author

Thanks @KaelWD. Even though I could have found this out myself using the Vue devtools, maybe it's a good idea to mention this scroll event in the doc page of v-virtual-scroll ?

@dschreij
Copy link
Contributor Author

Actually, I checked this in the vue devtools, and I see no scroll event emitted. Or is this a native JS event you are talking about?

@Mert75 Mert75 added has workaround T: feature A new feature and removed S: triage labels Aug 22, 2020
@dschreij
Copy link
Contributor Author

Alright @KaelWD, your suggestion lead me into the right direction. I indeed had to listen to the native scroll event of v-virtual-scroll, e.g.

<v-virtual-scroll
  :items="items"
  @scroll.native="scrolling"
>

Then the script to fire an event at the end of the stream is as follows:

import {debounce} from 'lodash'
export default {
    created () {
        this.scrolling = debounce(this.scrolling, 200)
    },
    scrolling (event) {
        const element = event.currentTarget || event.target
        if (element && element.scrollHeight - element.scrollTop === element.clientHeight) {
           this.$emit('scroll-end')
        }
    }
}

which did the trick. One could add a margin to the end area, so that items can already be fetched before the end of the current stream is reached, e.g.:

const margin = 50 // Get from somewhere else e.g. a prop
if (element && element.scrollHeight - element.scrollTop === element.clientHeight - margin) {

All in all, this was indeed relatively easy to achieve, but I still think this would be a worthwhile addition to the library 😄

@trizotti

This comment has been minimized.

@KaelWD
Copy link
Member

KaelWD commented May 25, 2023

#17265 adds a renderless prop so you could implement this by putting v-virtual-scroll inside v-infinite-scroll

@KaelWD KaelWD self-assigned this Jul 2, 2023
@KaelWD KaelWD added this to the v3.3.x milestone Jul 2, 2023
KaelWD added a commit that referenced this issue Jul 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants