0

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.

4

3 回答 3

1

我在这里为你创建了一个平台。您可以在 dataprovider 中传递唯一 ID,并且可以在 item 中访问。

PS中继器中有一个开放Feature-request支持索引,您也可以在那里投票。

于 2019-07-19T04:19:49.067 回答
1

Repeater 尚不支持访问项目索引,但如果它对您很重要,您可以扩展 Repeater 类以支持索引。

indexed-repeater.ts(针对 tns-core-modules v6.0.1 测试)

import { Repeater } from "tns-core-modules/ui/repeater";
import { CSSType } from "tns-core-modules/ui/layouts/layout-base";
import { parse } from "tns-core-modules/ui/builder";

export module knownTemplates {
    export const itemTemplate = "itemTemplate";
}

@CSSType("Repeater")
export class IndexedRepeater extends Repeater {

    public refresh() {
        if (this.itemsLayout) {
            this.itemsLayout.removeChildren();
        }

        if (!this.items) {
            return;
        }

        const length = this.items.length;
        for (let i = 0; i < length; i++) {
            const viewToAdd = this.itemTemplate ? parse(this.itemTemplate, this) : (<any>this)._getDefaultItemContent(i);
            const dataItem = (<any>this)._getDataItem(i);
            viewToAdd.bindingContext = {
                item: dataItem,
                index: i
            };
            this.itemsLayout.addChild(viewToAdd);
        }

        (<any>this)._isDirty = false;
    }
}

示例使用

<ScrollView xmlns:comps="components/indexed-repeater">
    <comps:IndexedRepeater items="{{ items }}">
        <comps:IndexedRepeater.itemTemplate>
            <Label text="{{ index + ' ' + item.name }}" class="m-10 h3" />
        </comps:IndexedRepeater.itemTemplate>
    </comps:IndexedRepeater>
</ScrollView>

游乐场样本

于 2019-07-19T06:22:11.260 回答
0

大家好,谢谢,它有帮助。我所做的,在不更改任何代码库的情况下,将本地生成的 ID 推送到 JSON 元素中并将它们用于索引。现在 json 有 { "my_index": "0" ..... } 在 fetch 结束时我在 for 循环中更改它。我使用这些索引只是为了滚动列表中某些元素的视图。

  http.getJSON("https://....")
  .then(function (response){ 
     for(var i = 0; i < response.length; i++) {            
       response[i].my_index = i; // change response on the fly.
       // do something with response....
     } 
    }, function(error) {
    console.error(JSON.stringify(error));
  })

我想这对某人也有帮助。

于 2019-07-19T23:18:48.247 回答