0

I'm trying to implement a virtual scroller (initially I wanted a Select component with virtual scrolling lazy but I simplified it to try to make it works) to display a list of project (id, name, customer). I have a Project endpoint which is working fine with DataTable with lazy pagination but I can't figure out how to correctly implement the virtual scroller.

Here is my code:

<script setup lang="ts">
import type { Project } from '@/core/interfaces'
import { projectService } from '@/core/services'
import type { VirtualScrollerLazyEvent } from 'primevue'
import { ref, type Ref } from 'vue'

const items: Ref<Project[]> = ref(Array.from({ length: 1000 }))
const loading: Ref<boolean> = ref(false)

const onLazyLoad = (event: VirtualScrollerLazyEvent) => {
  console.log('onLazyLoad', event, items.value.length)
  fetchItems(event.first, event.last)
}

const fetchItems = async (first: number, last: number) => {
  loading.value = true
  const skip: string = first.toString()
  const limit: string = last.toString()
  const params = new URLSearchParams({ skip, limit })
  const res = await projectService.getLazyList(params)
  items.value = res.projects
  loading.value = false
}
</script>

<template>
  <VirtualScroller :items="items" :itemSize="20" :loading="loading" :onLazyLoad="onLazyLoad" lazy showLoader
    :delay="250" :appendOnly="true" :autoSize="true" :style="{ width: '200px', height: '400px' }">
    <template v-slot:item="{ item }">
      <div style="height: 40px">{{ item?.id }}</div>
    </template>
  </VirtualScroller>
</template>

It's only works when limit = last + first but it result to load more and more data in each call and it's not a good thing. I can't figure out what is badly implemented...

2
  • "but it result to load more and more data in each call and it's not a good thing" what does this mean exactly? It's described very vaguely and the issue is not clear. Is the data returned by the endpoint correct? If it only works with certain limit value, and the limit value is sent to getLazyList service, then you probably should look at correcting getLazyList (or its endpoint), which you've not included in your question. Only you know how this works or what actual values are even being used. We see black box function call and variables with unknown values. Please enlighten us further. Commented Jan 21 at 14:41
  • hi @yoduh ! Sorry, my issue was so unclear to me ... The fact is that the itemSize property wasn't really obvious to understand IMO. I finally fixed my code by setting it to the line height value and to setting AutoSize=true. I also had some difficult to understand the importance to define the lenght of the array of values and to splice the array of value. I'll send the full code when totally fixed to help ! PS: the getLazyList was just a fetch with axios to my endpoint Commented Jan 21 at 14:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.