/*{"parent": "binding-to-the-dom", "order": 4, "title": "Array Bindings", "description": "List bindings: surgical updates via id-paths, virtualized rendering for million-row lists, sticky headers, filtering, and grids."}*/ /*# # Array Bindings and Virtual Lists The most likely source of complexity and performance issues in applications is displaying large lists or grids of objects. `tosijs` provides robust support for handling this efficiently. ## `.tosi.listBinding()` The simplest way to bind an array is with `.tosi.listBinding()`. It takes a template builder function that receives the `elements` proxy and a placeholder item for bindings: ```js import { elements, tosi } from 'tosijs' const { listBindingExample } = tosi({ listBindingExample: { array: ['this', 'is', 'an', 'example'] } }) const { h3, ul } = elements preview.append( h3('binding an array of strings'), ul( ...listBindingExample.array.tosi.listBinding(({li}, item) => li(item)) ) ) ``` And here is the same thing at scale: a few thousand emoji, in a **virtual** list, so only the rows on screen exist. [Virtualized Lists](#virtualized-lists) below explains the options. ```js import { elements, tosi, scrollListItemIntoView } from 'tosijs' const request = await fetch( 'https://raw.githubusercontent.com/tonioloewald/emoji-metadata/master/emoji-metadata.json' ) const emojiData = await request.json() const { emojiListExample } = tosi({ emojiListExample: { array: emojiData } }) const { div, button } = elements const emojiTable = div( { class: 'emoji-table' }, ...emojiListExample.array.tosi.listBinding(({div, span}, item) => div( { class: 'emoji-row', tabindex: 0, }, span({ textContent: item.chars, class: 'graphic' }), span({ textContent: item.name, class: 'no-overflow' }), span({ textContent: item.category, class: 'no-overflow' }), span({ textContent: item.subcategory, class: 'no-overflow' }) ), { value: emojiListExample.array, idPath: 'name', virtual: { height: 30, rowChunkSize: 3 }, } ) ) const scrollTo = (name) => { const item = emojiData.find(e => e.name === name) if (item) scrollListItemIntoView(emojiTable, item) } preview.append( emojiTable, div( { class: 'scroll-buttons' }, button('rocket', { onClick: () => scrollTo('rocket') }), button('flag: Finland', { onClick: () => scrollTo('flag: Finland') }), button('pile of poo', { onClick: () => scrollTo('pile of poo') }), ), ) ``` ```css .scroll-buttons { display: flex; justify-content: center; gap: 4px; padding: 4px; } .emoji-table { height: calc(100% - 36px); overflow: auto; } .emoji-row { display: grid; grid-template-columns: 50px 300px 200px 200px; align-items: center; height: 30px; overflow-x: hidden; } .emoji-row:nth-child(3n) { background: #f002; } .emoji-row:nth-child(3n+2) { background: #00f2; } .emoji-row > .graphic { font-size: 20px; justify-self: center; } .emoji-row > * { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } ``` ### listBinding(templateBuilder, options?) => [ElementProps, HTMLTemplateElement] type ListTemplateBuilder = (elements: ElementsProxy, item: U) => HTMLElement The template builder receives two arguments: the `elements` proxy (destructure the tags you need) and a placeholder proxy for the array item. Property access on the placeholder creates relative bindings (`^.name`, `^.score`, etc.) automatically. Spread the result into a container element — it returns an `[ElementProps, HTMLTemplateElement]` tuple. The props (containing `bindList`) are applied to the container and the `