GUIDE / ADVANCED INTERFACE

Large lists

Show thousands of rows with <list>, understand how its rows live and die, and control scrolling.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 24 / 29~3 min read · try examples at your own pace
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

What you will learn

  • When to use <list> instead of <scroll>.
  • The rules a <list> follows and why typed text must live in state.
  • How to follow new rows, load older ones and scroll from code.

<scroll> creates everything; <list> creates what you see

<scroll> creates every row up front, which is simple and fine for dozens of rows. <list> is virtualized: it creates only the rows on screen plus a margin, and reuses that work as you scroll. Use it for hundreds or thousands of rows, such as a log, a chat or a table of records. With 10,000 rows, creating the list takes about 40 ms.

janel
<script lang="python">
from janelkit import state
rows = state([{"id": n, "title": f"Item {n}"} for n in range(1, 2001)])
</script>

<window title="Two thousand rows" width={440} height={360}>
  <list gap={4} padding={12}>
    {#each rows as row (row["id"])}
      <text>{row["title"]}</text>
    {/each}
  </list>
</window>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

How it works

  1. The state holds 2,000 dictionaries, but only the rows on screen get a view.
  2. The key row["id"] lets the list keep track of each row while you scroll.
  3. gap and padding are set on the <list> itself, like on a <column>.

What you should see

A scrollable window shows Item 1, Item 2 and so on, smoothly, up to Item 2000.

The rules of a <list>

A <list> contains exactly one {#each}, with a key. Inside the rows, everything works as usual: index, handlers, components and reactive collections. Rows take the full width of the list; the list accepts gap, padding, background, border and radius, and takes the free space of its container by default.

{:else} is not available inside a <list>. Show the empty state with an {#if} outside it.

janel
<!-- tasks is a list state of dictionaries with "id" and "title". -->
{#if not tasks}
  <text>No tasks yet.</text>
{/if}
<list>
  {#each tasks as task (task["id"])}
    <text>{task["title"]}</text>
  {/each}
</list>
Copy

Fragment · Add it to an existing component; it is not a complete file.

Rows lose their view when they scroll away

Each row is created, its Python part and its view, the first time it becomes visible. The Python part lives until the item leaves the list. The view is destroyed when the row scrolls off screen and rebuilt when it comes back. While a row has no view, what it shows is not recalculated; it catches up once when it returns. Its effects, timers and tasks keep running.

That is why anything the user types in a row must be stored in state with bind:, not only in the control: the control itself may be rebuilt.

Common mistakes

  • Text typed into a field without bind: is lost when its row scrolls away and back.
  • If every row reads the same state, such as the selected id, give each row a computed for its part (is this row selected?). A binding that reads only a computed is skipped when the answer does not change, so selecting another row updates only the two rows involved.

Follow new rows and scroll from code

follow makes the list start at the bottom and stay there as rows arrive, while the user is at the bottom; if they scroll up to read, it stays put. Inserting rows at the top keeps what was on screen in place. on:top and on:bottom fire when the user reaches either end; on:scroll reports the position, at most once per frame, as {"top": bool, "bottom": bool, "y": pixels, "height": content height}.

With a ref, code can scroll: scroll_to_end(), which with follow also resumes following, scroll_to_start(), and scroll_to(index), which scrolls just enough to show that row.

<scroll> accepts the same follow, on:top, on:bottom and on:scroll, for content that is not a list of equal rows: a short log, a long form, a document. With its ref, scroll_to(element) takes the ref of an element inside it and scrolls just enough to show it whole; if it is already visible, nothing moves.

janel
<!-- messages is a list state; load_older is a script function; feed = ref(). -->
<list follow ref={feed} on:top={load_older} on:scroll={at_bottom = event["bottom"]}>
  {#each messages as m (m["id"])}
    <text>{m["text"]}</text>
  {/each}
</list>
<button on:click={feed.scroll_to_end()} enabled={not at_bottom}>Jump to latest</button>
Copy

Fragment · Add it to an existing component; it is not a complete file.

Learn moreRefs: control an element from code ↗

How large changes are applied

Adding rows anywhere, in any amount, is applied in blocks and keeps the scroll position. Removing or moving a few rows is applied one by one, animated with <list animate>. From 64 removals or moves at once, such as sorting or replacing the list, the change is applied in one go: the visible rows are rebuilt and the row that was at the top stays at the top.

Key points

  • Use <list> for hundreds of rows or more; it creates only what is visible.
  • One keyed {#each}, no {:else}; keep typed text in state.
  • follow, on:top, on:scroll and a ref control scrolling.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.