apps/automated/src/ui/repeater/repeater.md
Using a Repeater requires the repeater module. {%snippet article-require-repeater-module%} Other modules which will be used in the code samples in this article: {%snippet article-require-modules-repeater%}
<Page>
{%raw%}<Repeater items="{{ myItems }}" />{%endraw%}
</Page>
<Page>
{%raw%}<Repeater items="{{ myItems }}">
<Repeater.itemsLayout>
<StackLayout orientation="horizontal" />
</Repeater.itemsLayout>
</Repeater>{%endraw%}
</Page>
<Page>
{%raw%}<Repeater items="{{ myItems }}">
<Repeater.itemTemplate>
<Label text="{{ title || 'Downloading...' }}" textWrap="true" class="title" />
</Repeater.itemTemplate>
</Repeater>{%endraw%}
</Page>
The itemTemplateSelector can be an expression specified directly in XML. The context of the expression is the data item for each row.
<Page>
{%raw%}<Repeater items="{{ myItems }}" itemTemplateSelector="age > 18 ? 'green' : 'red'">
<Repeater.itemTemplates>
<template key="green">
<Label text="{{ age }}" style.backgroundColor="green" />
</template>
<template key="red">
<Label text="{{ age }}" style.backgroundColor="red" />
</template>
</Repeater.itemTemplates>
</Repeater>{%endraw%}
</Page>
In case your item template selector involves complicated logic which cannot be expressed with an expression, you can create an item template selector function in the code behind of the page in which the RepeaterRepeater resides. The function receives the respective data item, the row index and the entire Repeater items collection as parameters. It has to return the the key of the template to be used based on the supplied information.
<Page>
{%raw%}<Repeater items="{{ myItems }}" itemTemplateSelector="selectItemTemplate">
<Repeater.itemTemplates>
<template key="green">
<Label text="{{ age }}" style.backgroundColor="green" />
</template>
<template key="red">
<Label text="{{ age }}" style.backgroundColor="red" />
</template>
</Repeater.itemTemplates>
</Repeater>{%endraw%}
</Page>
{%snippet article-item-template-selector-function%}
You can use the special value '$index' in the item template selector expression which represents the row index.
<Page>
{%raw%}<Repeater items="{{ myItems }}" itemTemplateSelector="$index % 2 === 0 ? 'even' : 'odd'">
<Repeater.itemTemplates>
<template key="even">
<Label text="{{ age }}" style.backgroundColor="white" />
</template>
<template key="odd">
<Label text="{{ age }}" style.backgroundColor="gray" />
</template>
</Repeater.itemTemplates>
</Repeater>{%endraw%}
</Page>
<Page>
{%raw%}<ScrollView>
<Repeater items="{{ myItems }}">
<Repeater.itemsLayout>
<WrapLayout />
</Repeater.itemsLayout>
<Repeater.itemTemplate>
<Label text="{{ $value }}" margin="10" />
</Repeater.itemTemplate>
</Repeater>
</ScrollView>{%endraw%}
</Page>
{%snippet article-repeater-with-array%}
Note, that changing the array after the repeater is shown will not update the UI. You can force-update the UI using the refresh() method. {%snippet artcle-array-push-element%}
{%snippet article-repeater-layout%}
{%snippet article-repeater-observablearray%}
When using ObservableArray the repeater will be automatically updated when items are added or removed form the array. {%snippet article-push-to-observablearray%}