Skip to content

Commit a79d608

Browse files
authored
Merge pull request halfnelson#88 from changeweb/master
Add Multiple Templates Selector Function
2 parents d95f47c + 35fdd03 commit a79d608

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs_src/content/docs/40-components.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,34 @@ function onItemTap(event) {
330330
}
331331
```
332332
333+
**Multiple Templates Selector Function**
334+
335+
Multiple templates can be used for different layouts of list items, which is similar to [NativeScript Multiple Templates Selector Function](https://docs.nativescript.org/ui/components/list-view#multiple-templates-selector-function). `itemTemplateSelector` is used to select template using `key` which is returned based on your logic.
336+
337+
> **Note:** Use _multiple templates_ approach instead of `{#if}` block, because the _view recycling_ concept does not work as intended since `<listView>` cannot reuse the same template view if off-screen items require different type of template.
338+
339+
```html
340+
<listView bind:this="{test_subject}" items={items} itemTemplateSelector={selectTemplate}>
341+
<Template let:item key="odd">
342+
<label text="Odd {item}" />
343+
</Template>
344+
<Template let:item key="even">
345+
<label text="Even {item}" />
346+
</Template>
347+
</listView>
348+
```
349+
```js
350+
<script>
351+
import { Template } from 'svelte-native/components'
352+
export let test_subject;
353+
let items = ["item 0", "item 1"]
354+
function selectTemplate(item, index, items) {
355+
// Your logic here
356+
return (index % 2 == 0) ? "even" : "odd";
357+
}
358+
</script>
359+
```
360+
333361
> **IMPORTANT NOTE**: unlike Svelte expressions, The `<listView>` will **not** update the item list if you assign the same value to lists (eg `listOfItems.push('four'); listOfItems = listOfItems`). It **will** update if you assign a _new list reference_ (eg `listOfItems = listOfItems.concat('four')` or `listOfItems = [...listOfItems, 'four']`).
334362
335363
#### Props

0 commit comments

Comments
 (0)