primevue-mirror/doc/dropdown/LazyVirtualScrollDoc.vue

134 lines
4.1 KiB
Vue
Raw Normal View History

2023-02-28 08:29:30 +00:00
<template>
<DocSectionText v-bind="$attrs" />
<div class="card flex justify-content-center">
<Dropdown
v-model="selectedItem"
:options="items"
optionLabel="label"
optionValue="value"
:virtualScrollerOptions="{ lazy: true, onLazyLoad: onLazyLoad, itemSize: 38, showLoader: true, loading: loading, delay: 250 }"
placeholder="Select Item"
class="w-full md:w-14rem"
/>
</div>
<DocSectionCode :code="code" />
</template>
<script>
export default {
data() {
return {
selectedItem: null,
items: Array.from({ length: 100000 }),
loading: false,
code: {
2023-09-22 12:54:14 +00:00
basic: `
<Dropdown v-model="selectedItem" :options="items" optionLabel="label" optionValue="value" class="w-full md:w-14rem"
2023-02-28 08:29:30 +00:00
:virtualScrollerOptions="{ lazy: true, onLazyLoad: onLazyLoad, itemSize: 38, showLoader: true, loading: loading, delay: 250 }" placeholder="Select Item" />`,
2023-09-22 12:54:14 +00:00
options: `
<template>
2023-02-28 08:29:30 +00:00
<div class="card flex justify-content-center">
<Dropdown v-model="selectedItem" :options="items" optionLabel="label" optionValue="value" class="w-full md:w-14rem"
:virtualScrollerOptions="{ lazy: true, onLazyLoad: onLazyLoad, itemSize: 38, showLoader: true, loading: loading, delay: 250 }" placeholder="Select Item" />
</div>
</template>
<script>
export default {
data() {
return {
selectedItem: null,
items: Array.from({ length: 100000 }),
loading: false
};
},
loadLazyTimeout: null,
methods: {
onLazyLoad(event) {
this.loading = true;
if (this.loadLazyTimeout) {
clearTimeout(this.loadLazyTimeout);
}
//imitate delay of a backend call
this.loadLazyTimeout = setTimeout(() => {
const { first, last } = event;
const _items = [...this.items];
for (let i = first; i < last; i++) {
_items[i] = { label: \`Item #\${i}\`, value: i };
}
this.items = _items;
this.loading = false;
}, Math.random() * 1000 + 250);
}
}
};
<\/script>`,
2023-09-22 12:54:14 +00:00
composition: `
<template>
2023-02-28 08:29:30 +00:00
<div class="card flex justify-content-center">
<Dropdown v-model="selectedItem" :options="items" optionLabel="label" optionValue="value" class="w-full md:w-14rem"
:virtualScrollerOptions="{ lazy: true, onLazyLoad: onLazyLoad, itemSize: 38, showLoader: true, loading: loading, delay: 250 }" placeholder="Select Item" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedItem = ref();
const items = ref(Array.from({ length: 100000 }));
const loading = ref(false);
const loadLazyTimeout = ref();
const onLazyLoad = (event) => {
loading.value = true;
if (loadLazyTimeout.value) {
clearTimeout(loadLazyTimeout.value);
}
//imitate delay of a backend call
loadLazyTimeout.value = setTimeout(() => {
const { first, last } = event;
const _items = [...items.value];
for (let i = first; i < last; i++) {
_items[i] = { label: \`Item #\${i}\`, value: i };
}
items.value = _items;
loading.value = false;
}, Math.random() * 1000 + 250);
}
<\/script>`
}
};
},
loadLazyTimeout: null,
methods: {
onLazyLoad(event) {
this.loading = true;
if (this.loadLazyTimeout) {
clearTimeout(this.loadLazyTimeout);
}
//imitate delay of a backend call
this.loadLazyTimeout = setTimeout(() => {
const { first, last } = event;
const items = [...this.items];
for (let i = first; i < last; i++) {
items[i] = { label: `Item #${i}`, value: i };
}
this.items = items;
this.loading = false;
}, Math.random() * 1000 + 250);
}
}
};
</script>