简体   繁体   English

在Nativescript布局中的嵌套转发器中访问父级列表视图的索引

[英]Access the index of a parent listview in a nested repeater in a Nativescript layout

I have an array that contains multiple arrays of objects with in it that is returned from a web service, lets call it var groups . 我有一个数组,其中包含从Web服务返回的多个对象数组,可以将其称为var groups In my view model it's an observable array. 在我的视图模型中,这是一个可观察的数组。

var groups = [
              [{year:"1986", key2: "foo2"}, {year:"1986", key2:"blah"}],
              [{year:"1987", key2: "baz"}, {year:"1987", key2:"beek"}],
              [{year:"1988", key2: "baz"}, {year:"1988", key2:"beek"}]
             ]

In my xml I'm trying to nest a <Repeater> with in a <Listview> such that each array in the groups array will be a list item, and each object in the inner arrays will be a repeater item. 在我的xml中,我试图将<Repeater><Listview>嵌套在一起,以使groups数组中的每个数组都成为一个列表项,而内部数组中的每个对象都成为一个重复项。 Problem is I have no clue how to access the index of the parent listview items so that I can reference the correct array in the repeater, and I can't find in the documentation how to access the list index from with in the xml. 问题是我不知道如何访问父级listview项的索引,以便可以在转发器中引用正确的数组,而且在文档中找不到如何从xml中访问with的列表索引。 Does anyone know if this is possible? 有人知道这是否可能吗? Here's an example of the xml note the ??? 这是xml注释???的示例。 in the repeaters iterator....What can I actually put there? 在转发器迭代器中。...我实际上可以放在那里?

<Listview items="{{ Groups }}">
    <ListView.itemTemplate>
       /* Some other layout content */
      <Repeater items="{{ Groups[parent.index] ??? }}">
        <Repeater.itemTemplate>
          / * the contents of each groups array should display here */
        </Repeater.itemTemplate>
      </Repeater>
    </ListView.itemTemplate> 
</Listview>

EDIT: Just to make it more clear, I basically just want to know how to access the Parent list views index from the repeaters item attribute. 编辑:只是为了更清楚一点,我基本上只想知道如何从重复器item属性访问父级列表视图索引。 That would allow me to display the correct data in the repeater element. 这将使我能够在转发器元素中显示正确的数据。

I'm trying to achieve a separated list such as this below. 我正在尝试实现一个单独的列表,如下所示。 Each array in the groups array is a chunk of data with the same date. groups数组中的每个数组都是具有相同日期的数据块。 So the list view rows (dates) are based on the number of arrays contained in the groups array and the data sections below each date are populated from the data within each of those arrays. 因此,列表视图行(日期)基于groups数组中包含的数组数,并且每个日期以下的数据部分是从每个数组中的数据中填充的。

我正在尝试实现一个单独的列表,如下所示。 <code> groups </ code>数组中的每个数组都是具有相同日期的数据块。因此,列表视图行(日期)基于<code> groups </ code>数组中包含的数组数,并且每个日期以下的数据部分都是从每个数组中的数据中填充的。

In NativeScript is not possible to get index of the selected item in Repeater . NativeScript中,不可能在Repeater获取所选item索引。 However one possible decision could be to set unique id for example to Label and to attach tap even to it. 但是,一个可能的决定是将唯一id设置为Label ,甚至将tap附加到id When one of the Label has been clicked, you could get its unique id . 单击Label之一后,您可以获得其唯一id I am attaching example for this option. 我为此示例附加了示例。

main-page.xml 主page.xml

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="onPageLoaded">
        <ListView id="listview" items="{{ myItems }}" itemTap="listViewItemTap">
            <ListView.itemTemplate>
                <StackLayout>

                  <Repeater items="{{ myItem2 }}" >
                    <Repeater.itemsLayout>
                      <WrapLayout />
                    </Repeater.itemsLayout>
                    <Repeater.itemTemplate>
                      <Label text="{{ $value.item }}" id="{{$value.key}}" margin="10" onTap="test"/>
                    </Repeater.itemTemplate>
                  </Repeater>
                </StackLayout>
            </ListView.itemTemplate>
        </ListView>
</Page>

main-page.js 主page.js

var observable_array_1 = require("data/observable-array");
function onPageLoaded(args) {
    var page = args.object;
    var array = new observable_array_1.ObservableArray();
    var listView = page.getViewById('listview');
    array.push({ myItem2: [{ item: "test1", key: "1" }, { item: "test1", key: 'j' }, { item: "test1", key: "3" }] });
    array.push({ myItem2: [{ item: "test6", key: "6" }, { item: "test4", key: "4" }, { item: "test5", key: "5" }] });
    page.bindingContext = { myItems: array };

}
exports.onPageLoaded = onPageLoaded;
function listViewItemTap(args) {
    var itemIndex = args.index;
    var object = args.object;
    console.log(itemIndex);
}
exports.listViewItemTap = listViewItemTap;
function test(args) {
    console.log(args.object.get('id'));
}
exports.test = test;

As NativeScript indeed can not provide indexing inside the nested repeater and based on the example of Nikolay Tsonev it will be a lot easier to just convert your jagged array to array with JSON objects. 由于NativeScript确实无法在嵌套的转发器内部提供索引,并且基于Nikolay Tsonev示例,将锯齿状数组转换为带有JSON对象的数组会容易得多 Here is what you need to do for this code to work with your array. 您需要执行以下操作才能使此代码与您的数组一起使用。

var groups = [
      [{year:"1986", key2: "foo2"}, {year:"1986", key2:"blah"}],
      [{year:"1987", key2: "baz"}, {year:"1987", key2:"beek"}],
      [{year:"1988", key2: "baz"}, {year:"1988", key2:"beek"}]
];

var resultArray = new ObservableArray();

groups.forEach(subgroup => {
    var jsonArg1 = new Object();
    jsonArg1["myItem2"] = subgroup;
    resultArray.push(jsonArg1);
});

However you have also another solution - you can provide totally diffferent binding source to your nested repeater via $parents[Page].someDataModel For example: 但是,您还有另一种解决方案-您可以通过$ parents [Page] .someDataModel为嵌套转发器提供完全不同的绑定源,例如:

// page.xml
<ListView items="{{ items }}">
    <ListView.itemTemplate>
        <StackLayout>
            <Label text="{{ $value }}" />
            <TextField text="{{ $parents['ListView'].test, $parents['ListView'].test }}" />
        </StackLayout>
    </ListView.itemTemplate>
</ListView>

// page.js
viewModel.set("items", [1, 2, 3]);
viewModel.set("test", "Test for parent binding!");

This as well will require for you to manually convert your two-dimensional array into something with more suitable form. 这也需要您手动将二维数组转换为更合适的形式。 More about $parents this here 更多关于这里的 父母

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM