|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <title>Virtualizer</title> |
| 8 | + <script type="module" src="./common.js"></script> |
| 9 | + </head> |
| 10 | + <body> |
| 11 | + <style> |
| 12 | + html, |
| 13 | + body { |
| 14 | + height: 100%; |
| 15 | + } |
| 16 | + </style> |
| 17 | + |
| 18 | + <script type="module"> |
| 19 | + import '@vaadin/button'; |
| 20 | + import { Virtualizer } from '@vaadin/component-base/src/virtualizer.js'; |
| 21 | + import '@vaadin/horizontal-layout'; |
| 22 | + import '@vaadin/integer-field'; |
| 23 | + import { Notification } from '@vaadin/notification'; |
| 24 | + import '@vaadin/split-layout'; |
| 25 | + import '@vaadin/vertical-layout'; |
| 26 | + |
| 27 | + // Set up the virtualizer |
| 28 | + const scrollTarget = document.querySelector('#scroll-target'); |
| 29 | + const scrollContainer = document.querySelector('#scroll-container'); |
| 30 | + |
| 31 | + const virtualizer = new Virtualizer({ |
| 32 | + createElements: (count) => [...Array(count)].map(() => document.createElement('div')), |
| 33 | + updateElement: (el, index) => (el.textContent = `Item ${index}`), |
| 34 | + scrollTarget, |
| 35 | + scrollContainer, |
| 36 | + reorderElements: true, |
| 37 | + }); |
| 38 | + |
| 39 | + virtualizer.size = 1000; |
| 40 | + |
| 41 | + // Set up the controls |
| 42 | + const sizeField = document.querySelector('#size'); |
| 43 | + sizeField.addEventListener('value-changed', (e) => (virtualizer.size = e.detail.value)); |
| 44 | + sizeField.value = virtualizer.size; |
| 45 | + |
| 46 | + const scrollToIndexField = document.querySelector('#scroll-to-index'); |
| 47 | + const scrollButton = document.querySelector('#scroll'); |
| 48 | + scrollButton.addEventListener('click', () => { |
| 49 | + virtualizer.scrollToIndex(parseInt(scrollToIndexField.value)); |
| 50 | + }); |
| 51 | + |
| 52 | + const firstVisibleIndexButton = document.querySelector('#first-last-visible-index'); |
| 53 | + firstVisibleIndexButton.addEventListener('click', () => { |
| 54 | + Notification.show( |
| 55 | + `First visible index: ${virtualizer.firstVisibleIndex}, last visible index: ${virtualizer.lastVisibleIndex}`, |
| 56 | + ); |
| 57 | + }); |
| 58 | + </script> |
| 59 | + |
| 60 | + <vaadin-split-layout orientation="vertical" style="height: 100%"> |
| 61 | + <!-- Virtualizer --> |
| 62 | + <div style="height: 30%; position: relative" id="scroll-target"> |
| 63 | + <div id="scroll-container"></div> |
| 64 | + </div> |
| 65 | + |
| 66 | + <!-- Controls --> |
| 67 | + <vaadin-vertical-layout style="height: 70%"> |
| 68 | + <vaadin-integer-field id="size" label="Size" min="0"></vaadin-integer-field> |
| 69 | + |
| 70 | + <vaadin-horizontal-layout style="align-items: flex-end" theme="spacing"> |
| 71 | + <vaadin-integer-field id="scroll-to-index" label="Scroll To Index" value="500" min="0"></vaadin-integer-field> |
| 72 | + |
| 73 | + <vaadin-button id="scroll">Scroll</vaadin-button> |
| 74 | + </vaadin-horizontal-layout> |
| 75 | + |
| 76 | + <vaadin-button id="first-last-visible-index" style="margin-top: 30px">First/last visible index</vaadin-button> |
| 77 | + </vaadin-vertical-layout> |
| 78 | + </vaadin-split-layout> |
| 79 | + </body> |
| 80 | +</html> |
0 commit comments