Repeater 项目列表中的 NativeScript 索引

NativeScript index in the list of Repeater items

我正在尝试获取并使用 NativeScript 中的列表索引,在本例中使用的是 Repeater。

这是我的代码:

    <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>

带有myIndex="{{ $index }}"的部分不起作用,因为无法在Repeater中计算索引。 有没有办法为中继器中的每个项目获取索引? - ListView 有效,但在这种情况下我不能使用 listview。

我已经为你创建了一个 plyground here。您可以在您的数据提供者中传递唯一ID,并可以在项目中访问。

P.S。转发器中有一个开放的Feature-request支持索引,你也可以在那里投票

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

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>

Playground Sample

大家好,谢谢,这很有帮助。我所做的是在不更改任何代码库的情况下将本地生成的 ID 推入 JSON 元素并将它们用于索引。 现在 json 有 { "my_index": "0" ..... } 当获取结束时我在 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));
  })

我想这对某些人也有帮助。