nativescriptrepeateritemtemplateselector

NativeScript index in the list of Repeater items


I am trying to get and use index of the list in NativeScript, in this case using Repeater.

Here is my code:

    <ScrollView>                 
        <Repeater items="{{ sviArtikli }}" itemTemplateSelector="$index">
            <Repeater.itemsLayout>
                    <WrapLayout orientation="horizontal"/>                        
            </Repeater.itemsLayout>
            <Repeater.itemTemplate>
                        <Card:CardView margin="10" width="45%" height="250" item="{{ id }}"  myIndex="{{ $index }}" tap="detaljiArtikla">
                            <grid-layout rows="250, *" columns="*, 50">
                                <Image loaded="slikaUcitana" class="lista-katalozi-slika" id="slicice" opacity="1" stretch="aspectFill" colSpan="3" row="0" src="{{ 'https://url_location/' + slika_image }}" /> 
                            </grid-layout>
                        </Card:CardView>   
            </Repeater.itemTemplate>
        </Repeater> 
    </ScrollView>

The part with myIndex="{{ $index }}" does not work as index can not be calculated in Repeater. Is there a way I can get index for every item in repeater? - ListView works, but I can not use listview in this case.


Solution

  • I have created a plyground for you here. You can pass the unique ID in your dataprovider and can access in item.

    P.S. There is an open Feature-request to support index in repeater, you can also cast your vote there.