Open in a new tab

A list of 10 000 rows where only the visible window plus a small overscan exists in the DOM; jump to any row by index.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

Long tables, logs, feeds and pickers: rendering every row would cost thousands of nodes; the scroller keeps the DOM at a few dozen while the scrollbar still spans the whole list.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html, main.js and style.css, then open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.
  4. Keep ROW_HEIGHT equal to the CSS row height in fixed mode, or pass mode: 'variable' and let the scroller measure.

What it shows

  • virtualScroll.create({ container, itemHeight, total, overscan, renderItem }) with renderItem(index) returning one element per row on demand.
  • list.visibleRange(), list.scrollTo(index, { align }), list.refresh() and list.setTotal(n) for the driver code around it.
  • The status line counts the elements actually in the DOM against the 10 000 logical rows.

Code

main.js

javascriptApache-2.050 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { virtualScroll } from '@awacloud/fw/dom/rendering/virtualScroll.js';

fw.runtime.registerAllDeep([virtualScroll]);

export const TOTAL = 10000;
export const ROW_HEIGHT = 32; // must match .row in style.css (fixed mode)
export const OVERSCAN = 4;

/**
 * One row, built on demand: only the rows in the viewport (plus overscan)
 * exist at any time.
 * @param {number} index
 * @returns {HTMLElement}
 */
export function renderRow(index) {
    const row = document.createElement('div');
    row.className = 'row';
    row.setAttribute('role', 'listitem');
    row.textContent = `Row ${index + 1} of ${TOTAL}`;
    return row;
}

const container = /** @type {HTMLElement} */ (document.getElementById('list'));
const status = /** @type {HTMLElement} */ (document.getElementById('status'));
const goto = /** @type {HTMLInputElement} */ (document.getElementById('goto'));

/** The scroller: a spacer gives the full height, a layer holds the visible rows. */
export const list = fw.runtime.resolve('virtualScroll').create({
    container,
    itemHeight: ROW_HEIGHT,
    total: TOTAL,
    overscan: OVERSCAN,
    renderItem: renderRow,
});

export function updateStatus() {
    const { start, end } = list.visibleRange();
    const inDom = container.querySelectorAll('.row').length;
    status.textContent = `rows ${start + 1}–${end + 1} rendered (${inDom} elements in the DOM for ${TOTAL} rows)`;
}

container.addEventListener('scroll', updateStatus, { passive: true });
goto.addEventListener('change', () => {
    const index = Math.min(TOTAL - 1, Math.max(0, Number(goto.value) || 0));
    list.scrollTo(index, { align: 'start' });
    updateStatus();
});
updateStatus();

index.html

htmlApache-2.025 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · Ten thousand rows with a virtual scroller</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <link rel="stylesheet" href="./style.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>Ten thousand rows with a virtual scroller</h1>
    <label for="goto">Go to row</label>
    <input id="goto" type="number" min="0" max="9999" value="0">
    <p id="status" aria-live="polite"></p>
    <div id="list" role="list" aria-label="Rows" tabindex="0"></div>
  </main>
</body>
</html>

style.css

cssApache-2.09 lines

/* SPDX-License-Identifier: Apache-2.0 */
#list { block-size: 320px; overflow: auto; border: 1px solid var(--ex-border, #ccc); }
.row {
    block-size: 32px;
    line-height: 32px;
    padding-inline: 0.5rem;
    border-block-end: 1px solid var(--ex-border, #eee);
    box-sizing: border-box;
}