Fixed #692 - VirtualScroller Component

pull/1375/head
Tuğçe Küçükoğlu 2021-07-16 12:39:45 +03:00
parent 519e9d70bf
commit a5365d8292
10 changed files with 1786 additions and 0 deletions

View File

@ -0,0 +1,124 @@
const VirtualScrollerProps = [
{
name: "items",
type: "array",
default: "null",
description: "An array of objects to display."
},
{
name: "itemSize",
type: "number|array",
default: "null",
description: "The height/width of item according to orientation."
},
{
name: "scrollHeight",
type: "string",
default: "null",
description: "Height of the scroll viewport."
},
{
name: "scrollWidth",
type: "string",
default: "null",
description: "Width of the scroll viewport."
},
{
name: "orientation",
type: "string",
default: "vertical",
description: "The orientation of scrollbar, valid values are 'vertical', 'horizontal' and 'both'."
},
{
name: "numToleratedItems",
type: "number",
default: "null",
description: "Determines how many additional elements to add to the DOM outside of the view. According to the scrolls made up and down, extra items are added in a certain algorithm in the form of multiples of this number. Default value is half the number of items shown in the view."
},
{
name: "delay",
type: "number",
default: "0",
description: "Delay in scroll before new data is loaded."
},
{
name: "lazy",
type: "boolean",
default: "false",
description: "Defines if data is loaded and interacted with in lazy manner."
},
{
name: "showLoader",
type: "boolean",
default: "false",
description: "Whether to show loader."
},
{
name: "style",
type: "any",
default: "null",
description: "Inline style of the component."
},
{
name: "class",
type: "string",
default: "null",
description: "Style class of the component."
}
];
const VirtualScrollerEvents = [
{
name: "scroll-index-change",
description: "Callback to invoke when scroll position and item's range in view changes.",
arguments: [
{
name: "event.first",
type: "number",
description: "First index of the new data range to be loaded."
},
{
name: "event.last",
type: "number",
description: "Last index of the new data range to be loaded."
}
]
},
{
name: "lazy-load",
description: "Callback to invoke in lazy mode to load new data.",
arguments: [
{
name: "event.first",
type: "number",
description: "First index of the new data range to be loaded."
},
{
name: "event.last",
type: "number",
description: "Last index of the new data range to be loaded."
}
]
}
];
const VirtualScrollerSlots = [
{
name: "item",
description: "Content for the item"
},
{
name: "loader",
description: "Custom content for the loader items"
}
];
module.exports = {
virtualscroller: {
name: "VirtualScroller",
description: "VirtualScroller is a performant approach to handle huge data efficiently.",
props: VirtualScrollerProps,
events: VirtualScrollerEvents,
slots: VirtualScrollerSlots
}
};

View File

@ -406,6 +406,11 @@
"name": "DataView",
"to": "/dataview"
},
{
"name": "VirtualScroller",
"to": "/virtualscroller",
"badge": "New"
},
{
"name": "FilterService",
"to": "/filterservice"

View File

@ -0,0 +1,27 @@
import { VNode } from 'vue';
interface VirtualScrollerProps {
items?: any[]|any[][];
itemSize?: number|any[];
scrollHeight?: string;
scrollWidth?: string;
orientation?: string;
numToleratedItems?: number;
delay?: number;
lazy?: boolean;
showLoader?: boolean;
loading?: boolean;
style?: any;
class?: string;
}
declare class VirtualScroller {
$props: VirtualScrollerProps;
$emit(eventName: 'update:numToleratedItems', value: number): this;
$emit(eventName: 'scroll-index-change', value: { first: number, last: number }): this;
$emit(eventName: 'lazy-load', value: { first: number, last: number }): this;
$slots: {
items: VNode[];
loader: VNode[];
}
}

View File

@ -0,0 +1,545 @@
<template>
<div :ref="elementRef" :class="containerClass" :style="style" @scroll="onScroll">
<div :ref="contentRef" class="p-virtualscroller-content">
<template v-for="(item, index) of renderItems" :key="index">
<slot name="item" :item="item" :options="getOptions(index)"></slot>
</template>
</div>
<div :ref="spacerRef" class="p-virtualscroller-spacer"></div>
<div :class="loaderClass" v-if="d_loading">
<template v-for="(loadItem, index) of loaderArr" :key="index">
<slot name="loader" :options="getLoaderOptions(index)">
<i class="p-virtualscroller-loading-icon pi pi-spinner pi-spin"></i>
</slot>
</template>
</div>
</div>
</template>
<script>
export default {
name: 'VirtualScroller',
emits: ['update:numToleratedItems', 'scroll-index-change', 'lazy-load'],
props: {
items: null,
itemSize: {
type: [Number,Array],
default: null
},
scrollHeight: null,
scrollWidth: null,
orientation: {
type: String,
default: 'vertical'
},
numToleratedItems: {
type: Number,
default: null
},
delay: {
type: Number,
default: 0
},
lazy: {
type: Boolean,
default: false
},
showLoader: {
type: Boolean,
default: false
},
loading: {
type: Boolean,
default: false
},
style: null,
class: null
},
data() {
return {
first: this.isBoth() ? { rows: 0, cols: 0 } : 0,
last: this.isBoth() ? { rows: 0, cols: 0 } : 0,
numItemsInViewport: this.isBoth() ? { rows: 0, cols: 0 } : 0,
lastScrollPos: this.isBoth() ? { top: 0, left: 0 } : 0,
d_numToleratedItems: this.numToleratedItems,
d_loading: this.loading,
loaderArr: null
}
},
element: null,
content: null,
spacer: null,
scrollTimeout: null,
mounted() {
this.$nextTick(function () {
this.init();
});
},
watch: {
numToleratedItems(newValue) {
this.d_numToleratedItems = newValue;
},
loading(newValue) {
this.d_loading = newValue;
}
},
methods: {
init() {
this.setSize();
this.calculateOptions();
this.setSpacerSize();
},
getLast(last, isCols) {
return this.items ? Math.min((isCols ? this.items[0].length : this.items.length), last) : 0;
},
calculateOptions() {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const first = this.first;
const itemSize = this.itemSize;
const contentPadding = this.getContentPadding();
const contentWidth = this.element ? this.element.offsetWidth - contentPadding.left : 0;
const contentHeight = this.element ? this.element.offsetHeight - contentPadding.top : 0;
const calculateNumItemsInViewport = (_contentSize, _itemSize) => Math.ceil(_contentSize / (_itemSize || _contentSize));
const numItemsInViewport = isBoth ?
{ rows: calculateNumItemsInViewport(contentHeight, itemSize[0]), cols: calculateNumItemsInViewport(contentWidth, itemSize[1]) } :
calculateNumItemsInViewport((isHorizontal ? contentWidth : contentHeight), itemSize);
let numToleratedItems = this.d_numToleratedItems || Math.ceil((isBoth ? numItemsInViewport.rows : numItemsInViewport) / 2);
const calculateLast = (_first, _num, _isCols) => this.getLast(_first + _num + ((_first < numToleratedItems ? 2 : 3) * numToleratedItems), _isCols);
const last = isBoth ?
{ rows: calculateLast(first.rows, numItemsInViewport.rows), cols: calculateLast(first.cols, numItemsInViewport.cols, true) } :
calculateLast(first, numItemsInViewport);
this.d_numToleratedItems = numToleratedItems;
this.$emit('update:numToleratedItems', this.d_numToleratedItems);
this.last = last;
this.numItemsInViewport = numItemsInViewport;
if (this.showLoader) {
if (this.$slots && this.$slots.loader) {
this.loaderArr = Array.from({ length: (isBoth ? numItemsInViewport.rows : numItemsInViewport) });
}
else {
this.loaderArr = Array.from({ length: 1});
}
}
if (this.lazy) {
this.$emit('lazy-load', { first, last });
}
},
getContentPadding() {
if (this.content) {
const style = getComputedStyle(this.content);
const left = parseInt(parseFloat(style.paddingLeft.slice(0, -2)), 10);
const right = parseInt(parseFloat(style.paddingRight.slice(0, -2)), 10);
const top = parseInt(parseFloat(style.paddingTop.slice(0, -2)), 10);
const bottom = parseInt(parseFloat(style.paddingBottom.slice(0, -2)), 10);
return { left, right, top, bottom, x: left + right, y: top + bottom };
}
return { left: 0, right: 0, top: 0, bottom: 0, x: 0, y: 0 };
},
setSize() {
if (this.element) {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const parentElement = this.element.parentElement;
const width = this.scrollWidth || `${(this.element.offsetWidth || parentElement.offsetWidth)}px`;
const height = this.scrollHeight || `${(this.element.offsetHeight || parentElement.offsetHeight)}px`;
const setProp = (_name, _value) => this.element.style[_name] = _value;
if (isBoth) {
setProp('height', height);
setProp('width', width);
}
else {
isHorizontal ? setProp('width', width) : setProp('height', height);
}
}
},
setSpacerSize() {
const items = this.items;
if (this.spacer && items) {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const itemSize = this.itemSize;
const contentPadding = this.getContentPadding();
const setProp = (_name, _value, _size, _padding = 0) => this.spacer.style[_name] = (((_value || []).length * _size) + _padding) + 'px';
if (isBoth) {
setProp('height', items[0], itemSize[0], contentPadding.y);
setProp('width', items[1], itemSize[1], contentPadding.x);
}
else {
isHorizontal ? setProp('width', items, itemSize, contentPadding.x) : setProp('height', items, itemSize, contentPadding.y);
}
}
},
setContentPosition(pos) {
if (this.content) {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const content = this.content;
const first = pos ? pos.first : this.first;
const itemSize = this.itemSize;
const calculateTranslateVal = (_first, _size) => (_first * _size);
const setTransform = (_x = 0, _y = 0) => content.style.transform = `translate3d(${_x}px, ${_y}px, 0)`;
if (isBoth) {
setTransform(calculateTranslateVal(first.cols, itemSize[1]), calculateTranslateVal(first.rows, itemSize[0]));
}
else {
const translateVal = calculateTranslateVal(first, itemSize);
isHorizontal ? setTransform(translateVal, 0) : setTransform(0, translateVal);
}
}
},
onScrollPositionChange(event) {
const target = event.target;
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const itemSize = this.itemSize;
const contentPadding = this.getContentPadding();
const calculateScrollPos = (_pos, _padding) => _pos ? (_pos > _padding ? _pos - _padding : _pos) : 0;
const calculateCurrentIndex = (_pos, _size) => Math.floor(_pos / (_size || _pos));
const calculateTriggerIndex = (_currentIndex, _first, _last, _num, _isScrollDownOrRight) => {
return (_currentIndex <= this.d_numToleratedItems ? this.d_numToleratedItems : (_isScrollDownOrRight ? (_last - _num - this.d_numToleratedItems) : (_first + this.d_numToleratedItems - 1)))
};
const calculateFirst = (_currentIndex, _triggerIndex, _first, _last, _num, _isScrollDownOrRight) => {
if (_currentIndex <= this.d_numToleratedItems)
return 0;
else
return _isScrollDownOrRight ?
(_currentIndex < _triggerIndex ? _first : _currentIndex - this.d_numToleratedItems) :
(_currentIndex > _triggerIndex ? _first : _currentIndex - (2 * this.d_numToleratedItems));
};
const calculateLast = (_currentIndex, _first, _last, _num, _isCols) => {
let lastValue = _first + _num + (2 * this.d_numToleratedItems);
if (_currentIndex >= this.d_numToleratedItems) {
lastValue += (this.d_numToleratedItems + 1);
}
return this.getLast(lastValue, _isCols);
};
const scrollTop = calculateScrollPos(target.scrollTop, contentPadding.top);
const scrollLeft = calculateScrollPos(target.scrollLeft, contentPadding.left);
let newFirst = 0;
let newLast = this.last;
let isRangeChanged = false;
if (isBoth) {
const isScrollDown = this.lastScrollPos.top <= scrollTop;
const isScrollRight = this.lastScrollPos.left <= scrollLeft;
const currentIndex = { rows: calculateCurrentIndex(scrollTop, itemSize[0]), cols: calculateCurrentIndex(scrollLeft, itemSize[1]) };
const triggerIndex = {
rows: calculateTriggerIndex(currentIndex.rows, this.first.rows, this.last.rows, this.numItemsInViewport.rows, isScrollDown),
cols: calculateTriggerIndex(currentIndex.cols, this.first.cols, this.last.cols, this.numItemsInViewport.cols, isScrollRight)
};
newFirst = {
rows: calculateFirst(currentIndex.rows, triggerIndex.rows, this.first.rows, this.last.rows, this.numItemsInViewport.rows, isScrollDown),
cols: calculateFirst(currentIndex.cols, triggerIndex.cols, this.first.cols, this.last.cols, this.numItemsInViewport.cols, isScrollRight)
};
newLast = {
rows: calculateLast(currentIndex.rows, newFirst.rows, this.last.rows, this.numItemsInViewport.rows),
cols: calculateLast(currentIndex.cols, newFirst.cols, this.last.cols, this.numItemsInViewport.cols, true)
};
isRangeChanged = (newFirst.rows !== this.first.rows || newFirst.cols !== this.first.cols) || (newLast.rows !== this.last.rows || newLast.cols !== this.last.cols);
this.lastScrollPos = { top: scrollTop, left: scrollLeft };
}
else {
const scrollPos = isHorizontal ? scrollLeft : scrollTop;
const isScrollDownOrRight = this.lastScrollPos <= scrollPos;
const currentIndex = calculateCurrentIndex(scrollPos, itemSize);
const triggerIndex = calculateTriggerIndex(currentIndex, this.first, this.last, this.numItemsInViewport, isScrollDownOrRight);
newFirst = calculateFirst(currentIndex, triggerIndex, this.first, this.last, this.numItemsInViewport, isScrollDownOrRight);
newLast = calculateLast(currentIndex, newFirst, this.last, this.numItemsInViewport);
isRangeChanged = newFirst !== this.first || newLast !== this.last;
this.lastScrollPos = scrollPos;
}
return {
first: newFirst,
last: newLast,
isRangeChanged
}
},
onScrollChange(event) {
const { first, last, isRangeChanged } = this.onScrollPositionChange(event);
if (isRangeChanged) {
const newState = { first, last };
this.setContentPosition(newState);
if (this.lazy) {
this.$emit('lazy-load', { first, last });
}
this.first = first;
this.last = last;
this.$emit('scroll-index-change', { first, last });
}
},
onScroll(event) {
if (this.delay && !this.lazy) {
if (this.scrollTimeout) {
clearTimeout(this.scrollTimeout);
}
if (!this.d_loading && this.showLoader) {
const { isRangeChanged: changed } = this.onScrollPositionChange(event);
if (changed) {
this.d_loading = true;
}
}
this.scrollTimeout = setTimeout(() => {
this.onScrollChange(event);
if (this.d_loading && this.showLoader && !this.lazy) {
this.d_loading = false;
}
}, this.delay);
}
else {
this.onScrollChange(event);
}
},
getOptions(renderedIndex) {
let count = this.items.length;
let index = this.isBoth() ? this.first.rows + renderedIndex : this.first + renderedIndex;
return {
index,
count,
first: index === 0,
last: index === (count - 1),
even: index % 2 === 0,
odd: index % 2 !== 0
};
},
getLoaderOptions(index) {
let count = this.loaderArr.length;
return {
loading: this.d_loading,
first: index === 0,
last: index === (count - 1),
even: index % 2 === 0,
odd: index % 2 !== 0
}
},
isHorizontal() {
return this.orientation === 'horizontal';
},
isBoth() {
return this.orientation === 'both';
},
scrollTo(options) {
if (this.element) {
this.element.scrollTo(options);
}
},
scrollToIndex(index, behavior = 'auto') {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const itemSize = this.itemSize;
const contentPadding = this.getContentPadding();
const calculateFirst = (_index = 0) => (_index <= this.d_numToleratedItems ? 0 : _index);
const calculateCoord = (_first, _size, _padding) => (_first * _size) + _padding;
const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });
if (isBoth) {
const newFirst = { rows: calculateFirst(index[0]), cols: calculateFirst(index[1]) };
if (newFirst.rows !== this.first.rows || newFirst.cols !== this.first.cols)
scrollTo(calculateCoord(newFirst.cols, itemSize[1], contentPadding.left),calculateCoord(newFirst.rows, itemSize[0], contentPadding.top));
}
else {
const newFirst = calculateFirst(index);
if (newFirst !== this.first) {
isHorizontal ? scrollTo(calculateCoord(newFirst, itemSize, contentPadding.left), 0) : scrollTo(0, calculateCoord(newFirst, itemSize, contentPadding.top));
}
this.first = newFirst.index;
}
},
scrollInView(index, to, behavior = 'auto') {
if (to) {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const { first, viewport } = this.getRenderedRange();
const itemSize = this.itemSize;
const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });
const isToStart = to === 'to-start';
const isToEnd = to === 'to-end';
if (isToStart) {
if (isBoth) {
if (viewport.first.rows - first.rows > index[0]) {
scrollTo(viewport.first.cols * itemSize[1], (viewport.first.rows - 1) * itemSize[0]);
}
else if (viewport.first.cols - first.cols > index[1]) {
scrollTo((viewport.first.cols - 1) * itemSize[1], viewport.first.rows * itemSize[0]);
}
}
else {
if (viewport.first - first > index) {
const pos = (viewport.first - 1) * itemSize;
isHorizontal ? scrollTo(pos, 0) : scrollTo(0, pos);
}
}
}
else if (isToEnd) {
if (isBoth) {
if (viewport.last.rows - first.rows <= index[0] + 1) {
scrollTo(viewport.first.cols * itemSize[1], (viewport.first.rows + 1) * itemSize[0]);
}
else if (viewport.last.cols - first.cols <= index[1] + 1) {
scrollTo((viewport.first.cols + 1) * itemSize[1], viewport.first.rows * itemSize[0]);
}
}
else {
if (viewport.last - first <= index + 1) {
const pos = (viewport.first + 1) * itemSize;
isHorizontal ? scrollTo(pos, 0) : scrollTo(0, pos);
}
}
}
}
else {
this.scrollToIndex(index, behavior);
}
},
getRenderedRange() {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const itemSize = this.itemSize;
const calculateFirstInViewport = (_pos, _size) => Math.floor(_pos / (_size || _pos));
let firstInViewport = this.first;
let lastInViewport = 0;
if (this.element) {
const scrollTop = this.element.scrollTop;
const scrollLeft = this.element.scrollLeft;
if (isBoth) {
firstInViewport = { rows: calculateFirstInViewport(scrollTop, itemSize[0]), cols: calculateFirstInViewport(scrollLeft, itemSize[1]) };
lastInViewport = { rows: firstInViewport.rows + this.numItemsInViewport.rows, cols: firstInViewport.cols + this.numItemsInViewport.cols };
}
else {
const scrollPos = isHorizontal ? scrollLeft : scrollTop;
firstInViewport = calculateFirstInViewport(scrollPos, itemSize);
lastInViewport = firstInViewport + this.numItemsInViewport;
}
}
return {
first: this.first,
last: this.last,
viewport: {
first: firstInViewport,
last: lastInViewport
}
};
},
elementRef(el) {
this.element = el;
},
contentRef(el) {
this.content = el;
},
spacerRef(el) {
this.spacer = el;
},
},
computed: {
containerClass() {
return ['p-virtualscroller', {
'p-both-scroll': this.isBoth(),
'p-horizontal-scroll': this.isHorizontal()
}, this.class];
},
loaderClass() {
return ['p-virtualscroller-loader', {
'p-component-overlay': !this.$slots.loader
}];
},
renderItems() {
const items = this.items;
if (items && !this.d_loading) {
const isBoth = this.isBoth();
if (isBoth) {
return items.slice(this.first.rows, this.last.rows).map((item) => {
const items = item.slice(this.first.cols, this.last.cols);
return items;
});
}
else {
return items.slice(this.first, this.last).map((item) => {
return item;
});
}
}
return null;
}
}
}
</script>
<style>
.p-virtualscroller {
position: relative;
overflow: auto;
contain: strict;
transform: translateZ(0);
will-change: scroll-position;
}
.p-virtualscroller-content {
position: absolute;
top: 0;
left: 0;
contain: content;
min-height: 100%;
min-width: 100%;
will-change: transform;
}
.p-virtualscroller-spacer {
position: absolute;
top: 0;
left: 0;
height: 1px;
width: 1px;
transform-origin: 0 0;
pointer-events: none;
}
.p-virtualscroller .p-virtualscroller-loader {
position: sticky;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.p-virtualscroller-loader.p-component-overlay {
display: flex;
align-items: center;
justify-content: center;
}
</style>

View File

@ -0,0 +1,9 @@
{
"main": "./virtualscroller.cjs.js",
"module": "./virtualscroller.esm.js",
"unpkg": "./virtualscroller.min.js",
"types": "./VirtualScroller.d.ts",
"browser": {
"./sfc": "./VirtualScroller.vue"
}
}

View File

@ -94,6 +94,7 @@ import Toolbar from './components/toolbar/Toolbar';
import Tooltip from './components/tooltip/Tooltip';
import TriStateCheckbox from './components/tristatecheckbox/TriStateCheckbox';
import Galleria from './components/galleria/Galleria';
import VirtualScroller from './components/virtualscroller/VirtualScroller';
import AppInputStyleSwitch from './AppInputStyleSwitch';
import AppDemoActions from './AppDemoActions';
@ -210,6 +211,7 @@ app.component('TreeSelect', TreeSelect);
app.component('TreeTable', TreeTable);
app.component('TriStateCheckbox', TriStateCheckbox);
app.component('Galleria', Galleria);
app.component('VirtualScroller', VirtualScroller);
app.component('AppDemoActions', AppDemoActions);
app.component('AppInputStyleSwitch', AppInputStyleSwitch);

View File

@ -831,6 +831,11 @@ const routes = [
name: 'galleriaadvvanced',
component: () => import('../views/galleria/GalleriaAdvancedDemo.vue')
},
{
path: '/virtualscroller',
name: 'virtualscroller',
component: () => import('../views/virtualscroller/VirtualScrollerDemo.vue')
},
{
path: '/vuelidate',
name: 'vuelidateform',

View File

@ -490,6 +490,7 @@ import Tree from 'primevue/tree';
import TreeSelect from 'primevue/treeselect';
import TreeTable from 'primevue/treetable';
import TriStateCheckbox from 'primevue/tristatecheckbox';
import VirtualScroller from 'primevue/virtualscroller';
${extImport}
const app = createApp(${name});
@ -586,6 +587,7 @@ app.component('Tree', Tree);
app.component('TreeSelect', TreeSelect);
app.component('TreeTable', TreeTable);
app.component('TriStateCheckbox', TriStateCheckbox);
app.component('VirtualScroller', VirtualScroller);
${extElement}
app.mount("#app");

View File

@ -0,0 +1,243 @@
<template>
<div>
<div class="content-section introduction">
<div class="feature-intro">
<h1>VirtualScroller</h1>
<p>VirtualScroller is a performant approach to handle huge data efficiently.</p>
</div>
<AppDemoActions />
</div>
<div class="content-section implementation virtualscroller-demo">
<div class="card">
<h5 class="p-mb-0">Basic</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Vertical</h6>
<VirtualScroller :items="basicItems" :itemSize="50">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Horizontal</h6>
<VirtualScroller :items="basicItems" :itemSize="50" orientation="horizontal">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="width: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Both</h6>
<VirtualScroller :items="multiItems" :itemSize="[50, 100]" orientation="both">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px" >
<template v-for="(el, index) of item" :key="index">
<div style="width: 100px">{{ el }}</div>
</template>
</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Scroll Delay</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>0ms Delay</h6>
<VirtualScroller :items="basicItems" :itemSize="50">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>150ms Delay</h6>
<VirtualScroller :items="basicItems" :itemSize="50" :delay="150">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>250ms Delay</h6>
<VirtualScroller :items="basicItems" :itemSize="50" :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Loading</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Basic</h6>
<VirtualScroller :items="basicItems" :itemSize="50" showLoader :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Templating</h6>
<VirtualScroller class="custom-loading" :items="basicItems" :itemSize="50" showLoader :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
<template v-slot:loader="{ options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px" >
<Skeleton :width="options.even ? '60%' : '50%'" height="1.3rem" />
</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Lazy</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<VirtualScroller :items="lazyItems" :itemSize="50" showLoader :delay="250" :loading="lazyLoading" :lazy=true @lazy-load="onLazyLoad">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Template</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<VirtualScroller class="custom-loading" :items="basicItems" :itemSize="25 * 7" showLoader :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['custom-scroll-item scroll-item', {'odd': options.odd}]">
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{`Item: ${item}`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{`Index: ${options.index}`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{`Count: ${options.count}`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{`First: ${options.first}`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{`Last: ${options.last}`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{`Even: ${options.even}`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{`Odd: ${options.odd}`}}</div>
</div>
</template>
<template v-slot:loader="{ options }">
<div :class="['custom-scroll-item scroll-item', {'odd': options.odd}]">
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="50%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="50%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="50%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
</div>
<VirtualScrollerDoc />
</div>
</template>
<script>
import VirtualScrollerDoc from './VirtualScrollerDoc.vue';
export default {
data() {
return {
basicItems: null,
multiItems: null,
lazyItems: null,
lazyLoading: false,
loadLazyTimeout: null,
}
},
mounted() {
this.basicItems = Array.from({ length: 100000 }).map((_, i) => `Item #${i}`);
this.multiItems = Array.from({ length: 1000 }).map((_, i) => Array.from({ length: 1000 }).map((_j, j) => `Item #${i}_${j}`));
this.lazyItems = Array.from({ length: 100000 });
},
methods: {
onLazyLoad(event) {
this.lazyLoading = true;
if (this.loadLazyTimeout) {
clearTimeout(this.loadLazyTimeout);
}
//imitate delay of a backend call
this.loadLazyTimeout = setTimeout(() => {
const { first, last } = event;
const lazyItems = [...this.lazyItems];
for (let i = first; i < last; i++) {
lazyItems[i] = `Item #${i}`;
}
this.lazyItems = lazyItems;
this.lazyLoading = false;
}, Math.random() * 1000 + 250);
}
},
components: {
VirtualScrollerDoc
}
}
</script>
<style lang="scss" scoped>
.virtualscroller-demo {
::v-deep(.p-virtualscroller) {
height: 200px;
width: 200px;
border: 1px solid var(--surface-d);
.scroll-item {
background-color: var(--surface-a);
display: flex;
align-items: center;
}
.custom-scroll-item {
flex-direction: column;
align-items: stretch;
}
.odd {
background-color: var(--surface-b);
}
}
::v-deep(.p-horizontal-scroll) {
.p-virtualscroller-content {
display: flex;
flex-direction: row;
}
.scroll-item {
writing-mode: vertical-lr;
}
}
::v-deep(.custom-loading > .p-virtualscroller-loader) {
display: block;
}
}
</style>

View File

@ -0,0 +1,824 @@
<template>
<AppDoc name="VirtualScrollerDemo" :sources="sources" github="virtualscroller/VirtualScrollerDemo.vue">
<h5>Imports</h5>
<pre v-code.script><code>
import VirtualScroller from 'primevue/virtualscroller';
</code></pre>
<h5>Getting Started</h5>
<p>VirtualScroller is used to display huge data. It periodically adds special elements defined according to the scroll's position to the DOM.
The <i>item</i> and <i>itemSize</i> properties and <i>item</i> template are required on component. In addition, an initial array is required based on the total number of items to display.<br />
VirtualScroller automatically calculates how many items will be displayed in the view according to <i>itemSize</i> using a specified scroll height. Its scroll height can be adjusted with <i>scrollHeight</i>
property or height property of CSS.</p>
<pre v-code><code><template v-pre>
&lt;VirtualScroller :items="basicItems" :itemSize="50"&gt;
&lt;template v-slot:item="{ item, options }"&gt;
&lt;div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px"&gt;{{ item }}&lt;/div&gt;
&lt;/template&gt;
&lt;/VirtualScroller&gt;
</template>
</code></pre>
<pre v-code.script><code>
export default {
data() {
return {
basicItems: null
}
},
mounted() {
this.basicItems = Array.from({ length: 100000 }).map((_, i) => `Item #${i}`);
}
}
</code></pre>
<h5>Loader</h5>
<p>VirtualScroller has a special loader. It can be activated with the <i>showLoader</i> property.
In addition, <i>loader</i> template can be used to add custom loaders to item elements.</p>
<pre v-code><code><template v-pre>
&lt;VirtualScroller :items="basicItems" :itemSize="50" showLoader :delay="250"&gt;
&lt;template v-slot:item="{ item, options }"&gt;
&lt;div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px"&gt;{{ item }}&lt;/div&gt;
&lt;/template&gt;
&lt;template v-slot:loader="{ options }"&gt;
&lt;div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px" &gt;
&lt;Skeleton :width="options.even ? '60%' : '50%'" height="1.3rem" /&gt;
&lt;/div&gt;
&lt;/template&gt;
&lt;/VirtualScroller&gt;
</template>
</code></pre>
<h5>Lazy</h5>
<p>Lazy mode is handy to deal with large datasets, instead of loading the entire data, small chunks of data is loaded by invoking <i>lazy-load</i> event.</p>
<pre v-code><code><template v-pre>
&lt;VirtualScroller :items="lazyItems" :itemSize="50" showLoader :delay="250" :loading="lazyLoading" :lazy=true @lazy-load="onLazyLoad"&gt;
&lt;template v-slot:item="{ item, options }"&gt;
&lt;div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px"&gt;{{ item }}&lt;/div&gt;
&lt;/template&gt;
&lt;/VirtualScroller&gt;
</template>
</code></pre>
<pre v-code.script><code>
export default {
data() {
return {
lazyItems: null,
lazyLoading: false,
loadLazyTimeout: null,
}
},
mounted() {
this.lazyItems = Array.from({ length: 100000 });
},
methods: {
onLazyLoad(event) {
this.lazyLoading = true;
if (this.loadLazyTimeout) {
clearTimeout(this.loadLazyTimeout);
}
//imitate delay of a backend call
this.loadLazyTimeout = setTimeout(() => {
const { first, last } = event;
const lazyItems = [...this.lazyItems];
for (let i = first; i &lt; last; i++) {
lazyItems[i] = `Item #${i}`;
}
this.lazyItems = lazyItems;
this.lazyLoading = false;
}, Math.random() * 1000 + 250);
}
}
}
</code></pre>
<h5>Properties</h5>
<div class="doc-tablewrapper">
<table class="doc-table">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>items</td>
<td>array</td>
<td>null</td>
<td>An array of objects to display.</td>
</tr>
<tr>
<td>itemSize</td>
<td>number / [number, number]</td>
<td>null</td>
<td>The height/width of item according to orientation.</td>
</tr>
<tr>
<td>scrollHeight</td>
<td>string</td>
<td>null</td>
<td>Height of the scroll viewport.</td>
</tr>
<tr>
<td>scrollWidth</td>
<td>string</td>
<td>null</td>
<td>Width of the scroll viewport.</td>
</tr>
<tr>
<td>orientation</td>
<td>string</td>
<td>'vertical'</td>
<td>The orientation of scrollbar, valid values are 'vertical', 'horizontal' and 'both'.</td>
</tr>
<tr>
<td>numToleratedItems</td>
<td>number</td>
<td>null</td>
<td>Determines how many additional elements to add to the DOM outside of the view. <br />
According to the scrolls made up and down, extra items are added in a certain algorithm in the form of multiples of this number. <br />
Default value is half the number of items shown in the view.</td>
</tr>
<tr>
<td>delay</td>
<td>number</td>
<td>0</td>
<td>Delay in scroll before new data is loaded.</td>
</tr>
<tr>
<td>lazy</td>
<td>boolean</td>
<td>false</td>
<td>Defines if data is loaded and interacted with in lazy manner.</td>
</tr>
<tr>
<td>showLoader</td>
<td>boolean</td>
<td>false</td>
<td>Whether to show loader.</td>
</tr>
<tr>
<td>style</td>
<td>any</td>
<td>null</td>
<td>Inline style of the component.</td>
</tr>
<tr>
<td>class</td>
<td>string</td>
<td>null</td>
<td>Style class of the component.</td>
</tr>
</tbody>
</table>
</div>
<h5>Events</h5>
<div class="doc-tablewrapper">
<table class="doc-table">
<thead>
<tr>
<th>Name</th>
<th>Parameters</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>scroll</td>
<td>event: Browser event</td>
<td>Callback to invoke when scroll position changes.</td>
</tr>
<tr>
<td>scroll-index-change</td>
<td>event.first: First index of the new data range to be loaded.<br/>
event.last: Last index of the new data range to be loaded.
</td>
<td>Callback to invoke when scroll position and item's range in view changes.</td>
</tr>
<tr>
<td>lazy-load</td>
<td>event.first: First index of the new data range to be loaded.<br/>
event.last: Last index of the new data range to be loaded.
</td>
<td>Callback to invoke in lazy mode to load new data.</td>
</tr>
</tbody>
</table>
</div>
<h5>Methods</h5>
<div class="doc-tablewrapper">
<table class="doc-table">
<thead>
<tr>
<th>Name</th>
<th>Parameters</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>scrollTo</td>
<td>
left: Left position of scroll. <br />
top: Top position of scroll <br />
behavior: Behavior of scroll, valid values are 'auto' and 'smooth'
</td>
<td>Scroll to move to a specific position.</td>
</tr>
<tr>
<td>scrollToIndex</td>
<td>
index: Index of item according to orientation mode. <br />
behavior: Behavior of scroll, valid values are 'auto' and 'smooth'
</td>
<td>Scroll to move to a specific item.</td>
</tr>
<tr>
<td>scrollInView</td>
<td>
index: Index of item according to orientation mode. <br />
to: Defines the location of the item in view, valid values are 'to-start' and 'to-end'. <br />
behavior: Behavior of scroll, valid values are 'auto' and 'smooth'
</td>
<td>It is used to move the specified index into the view. It is a method that will usually be needed when keyboard support is added to the virtualScroller component.</td>
</tr>
<tr>
<td>getRenderedRange</td>
<td>-</td>
<td>Returns the range of items added to the DOM.</td>
</tr>
</tbody>
</table>
</div>
<h5>Slots</h5>
<div class="doc-tablewrapper">
<table class="doc-table">
<thead>
<tr>
<th>Name</th>
<th>Parameters</th>
</tr>
</thead>
<tbody>
<tr>
<td>item</td>
<td>item: Item instance<br />
options: Options of the item instance</td>
</tr>
<tr>
<td>loader</td>
<td>options: Options of the loader items</td>
</tr>
</tbody>
</table>
</div>
<h5>Styling</h5>
<p>Following is the list of structural style classes, for theming classes visit <router-link to="/theming">theming</router-link> page.</p>
<div class="doc-tablewrapper">
<table class="doc-table">
<thead>
<tr>
<th>Name</th>
<th>Element</th>
</tr>
</thead>
<tbody>
<tr>
<td>p-virtualscroller</td>
<td>Container element.</td>
</tr>
<tr>
<td>p-virtualscroller-content</td>
<td>Content element.</td>
</tr>
<tr>
<td>p-virtualscroller-loader</td>
<td>Loader element.</td>
</tr>
</tbody>
</table>
</div>
<h5>Dependencies</h5>
<p>None.</p>
</AppDoc>
</template>
<script>
export default {
data() {
return {
sources: {
'options-api': {
tabName: 'Options API Source',
content: `
<template>
<div>
<div class="content-section introduction">
<div class="feature-intro">
<h1>VirtualScroller</h1>
<p>VirtualScroller is a performant approach to handle huge data efficiently.</p>
</div>
<AppDemoActions />
</div>
<div class="content-section implementation virtualscroller-demo">
<div class="card">
<h5 class="p-mb-0">Basic</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Vertical</h6>
<VirtualScroller :items="basicItems" :itemSize="50">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Horizontal</h6>
<VirtualScroller :items="basicItems" :itemSize="50" orientation="horizontal">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="width: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Both</h6>
<VirtualScroller :items="multiItems" :itemSize="[50, 100]" orientation="both">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px" >
<template v-for="(el, index) of item" :key="index">
<div style="width: 100px">{{ el }}</div>
</template>
</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Scroll Delay</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>0ms Delay</h6>
<VirtualScroller :items="basicItems" :itemSize="50">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>150ms Delay</h6>
<VirtualScroller :items="basicItems" :itemSize="50" :delay="150">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>250ms Delay</h6>
<VirtualScroller :items="basicItems" :itemSize="50" :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Loading</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Basic</h6>
<VirtualScroller :items="basicItems" :itemSize="50" showLoader :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Templating</h6>
<VirtualScroller class="custom-loading" :items="basicItems" :itemSize="50" showLoader :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
<template v-slot:loader="{ options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px" >
<Skeleton :width="options.even ? '60%' : '50%'" height="1.3rem" />
</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Lazy</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<VirtualScroller :items="lazyItems" :itemSize="50" showLoader :delay="250" :loading="lazyLoading" :lazy=true @lazy-load="onLazyLoad">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Template</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<VirtualScroller class="custom-loading" :items="basicItems" :itemSize="25 * 7" showLoader :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['custom-scroll-item scroll-item', {'odd': options.odd}]">
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Item: \${item}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Index: \${options.index}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Count: \${options.count}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`First: \${options.first}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Last: \${options.last}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Even: \${options.even}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Odd: \${options.odd}\`}}</div>
</div>
</template>
<template v-slot:loader="{ options }">
<div :class="['custom-scroll-item scroll-item', {'odd': options.odd}]">
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="50%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="50%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="50%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
basicItems: null,
multiItems: null,
lazyItems: null,
lazyLoading: false,
loadLazyTimeout: null,
}
},
mounted() {
this.basicItems = Array.from({ length: 100000 }).map((_, i) => \`Item #\${i}\`);
this.multiItems = Array.from({ length: 1000 }).map((_, i) => Array.from({ length: 1000 }).map((_j, j) => \`Item #\${i}_\${j}\`));
this.lazyItems = Array.from({ length: 100000 });
},
methods: {
onLazyLoad(event) {
this.lazyLoading = true;
if (this.loadLazyTimeout) {
clearTimeout(this.loadLazyTimeout);
}
//imitate delay of a backend call
this.loadLazyTimeout = setTimeout(() => {
const { first, last } = event;
const lazyItems = [...this.lazyItems];
for (let i = first; i < last; i++) {
lazyItems[i] = \`Item #\${i}\`;
}
this.lazyItems = lazyItems;
this.lazyLoading = false;
}, Math.random() * 1000 + 250);
}
}
}
<\\/script>
<style lang="scss" scoped>
.virtualscroller-demo {
::v-deep(.p-virtualscroller) {
height: 200px;
width: 200px;
border: 1px solid var(--surface-d);
.scroll-item {
background-color: var(--surface-a);
display: flex;
align-items: center;
}
.custom-scroll-item {
flex-direction: column;
align-items: stretch;
}
.odd {
background-color: var(--surface-b);
}
}
::v-deep(.p-horizontal-scroll) {
.p-virtualscroller-content {
display: flex;
flex-direction: row;
}
.scroll-item {
writing-mode: vertical-lr;
}
}
::v-deep(.custom-loading > .p-virtualscroller-loader) {
display: block;
}
}
</style>
`
},
'composition-api': {
tabName: 'Composition API Source',
content: `
<template>
<div>
<div class="content-section introduction">
<div class="feature-intro">
<h1>VirtualScroller</h1>
<p>VirtualScroller is a performant approach to handle huge data efficiently.</p>
</div>
<AppDemoActions />
</div>
<div class="content-section implementation virtualscroller-demo">
<div class="card">
<h5 class="p-mb-0">Basic</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Vertical</h6>
<VirtualScroller :items="basicItems" :itemSize="50">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Horizontal</h6>
<VirtualScroller :items="basicItems" :itemSize="50" orientation="horizontal">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="width: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Both</h6>
<VirtualScroller :items="multiItems" :itemSize="[50, 100]" orientation="both">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px" >
<template v-for="(el, index) of item" :key="index">
<div style="width: 100px">{{ el }}</div>
</template>
</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Scroll Delay</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>0ms Delay</h6>
<VirtualScroller :items="basicItems" :itemSize="50">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>150ms Delay</h6>
<VirtualScroller :items="basicItems" :itemSize="50" :delay="150">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>250ms Delay</h6>
<VirtualScroller :items="basicItems" :itemSize="50" :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Loading</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Basic</h6>
<VirtualScroller :items="basicItems" :itemSize="50" showLoader :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<h6>Templating</h6>
<VirtualScroller class="custom-loading" :items="basicItems" :itemSize="50" showLoader :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
<template v-slot:loader="{ options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px" >
<Skeleton :width="options.even ? '60%' : '50%'" height="1.3rem" />
</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Lazy</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<VirtualScroller :items="lazyItems" :itemSize="50" showLoader :delay="250" :loading="lazyLoading" :lazy=true @lazy-load="onLazyLoad">
<template v-slot:item="{ item, options }">
<div :class="['scroll-item p-p-2', {'odd': options.odd}]" style="height: 50px">{{ item }}</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
<div class="card">
<h5 class="p-mb-0">Template</h5>
<div class="p-d-flex p-ai-center p-flex-wrap">
<div class="p-d-flex p-dir-col p-mr-3 p-mt-3">
<VirtualScroller class="custom-loading" :items="basicItems" :itemSize="25 * 7" showLoader :delay="250">
<template v-slot:item="{ item, options }">
<div :class="['custom-scroll-item scroll-item', {'odd': options.odd}]">
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Item: \${item}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Index: \${options.index}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Count: \${options.count}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`First: \${options.first}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Last: \${options.last}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Even: \${options.even}\`}}</div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px">{{\`Odd: \${options.odd}\`}}</div>
</div>
</template>
<template v-slot:loader="{ options }">
<div :class="['custom-scroll-item scroll-item', {'odd': options.odd}]">
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="50%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="50%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="50%" height="1.2rem" /></div>
<div class="p-d-flex p-ai-center p-px-2" style="height: 25px"><Skeleton width="60%" height="1.2rem" /></div>
</div>
</template>
</VirtualScroller>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import { ref, onMounted } from 'vue';
export default {
setup() {
onMounted(() => {
basicItems.value = Array.from({ length: 100000 }).map((_, i) => \`Item #\${i}\`);
multiItems.value = Array.from({ length: 1000 }).map((_, i) => Array.from({ length: 1000 }).map((_j, j) => \`Item #\${i}_\${j}\`));
lazyItems.value = Array.from({ length: 100000 });
})
const basicItems = ref(null);
const multiItems = ref(null);
const lazyItems = ref(null);
const lazyLoading = ref(false);
const loadLazyTimeout = ref(null);
const onLazyLoad = (event) => {
lazyLoading.value = true;
if (loadLazyTimeout.value) {
clearTimeout(loadLazyTimeout.value);
}
//imitate delay of a backend call
loadLazyTimeout.value = setTimeout(() => {
const { first, last } = event;
const lazyItems = [...lazyItems.value];
for (let i = first; i < last; i++) {
lazyItems[i] = \`Item #\${i}\`;
}
lazyItems.value = lazyItems;
lazyLoading.value = false;
}, Math.random() * 1000 + 250);
}
return { basicItems, multiItems, lazyItems, lazyLoading, onLazyLoad }
},
methods: {
}
}
<\\/script>
<style lang="scss" scoped>
.virtualscroller-demo {
::v-deep(.p-virtualscroller) {
height: 200px;
width: 200px;
border: 1px solid var(--surface-d);
.scroll-item {
background-color: var(--surface-a);
display: flex;
align-items: center;
}
.custom-scroll-item {
flex-direction: column;
align-items: stretch;
}
.odd {
background-color: var(--surface-b);
}
}
::v-deep(.p-horizontal-scroll) {
.p-virtualscroller-content {
display: flex;
flex-direction: row;
}
.scroll-item {
writing-mode: vertical-lr;
}
}
::v-deep(.custom-loading > .p-virtualscroller-loader) {
display: block;
}
}
</style>
`
}
}
}
}
}
</script>