我有一个数组,其中包含从Web服务返回的多个对象数组,我们称之为 var groups
. 在我的视图模型中,它是一个可观察的数组 .
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"}]
]
在我的xml中,我试图在 <Listview>
中嵌套 <Repeater>
,这样 groups
数组中的每个数组都是一个列表项,内部数组中的每个对象都将是一个转发项 . 问题是我不知道如何访问父listview项的索引,以便我可以在转发器中引用正确的数组,我可以't find in the documentation how to access the list index from with in the xml. Does anyone know if this is possible? Here'一个xml注释的例子???在中继器迭代器中......我能在那里放置什么?
<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>
编辑:只是为了使它更清楚,我基本上只是想知道如何从转发器 item
属性访问父列表视图索引 . 这将允许我在转发器元素中显示正确的数据 .
我正在尝试实现如下所示的单独列表 . groups
数组中的每个数组都是一个具有相同日期的数据块 . 因此,列表视图行(日期)基于 groups
数组中包含的数组数量,每个日期下面的数据部分将从每个数组中的数据填充 .
2 回答
在
NativeScript
中无法获取Repeater
中所选item
的索引 . 但是,一个可能的决定可能是将id
设置为Label
并将tap
连接到它 . 当单击其中一个Label
时,您可以获得其独特的id
. 我附加了此选项的示例 .main-page.xml
main-page.js
由于NativeScript确实无法在嵌套转发器内部提供索引,并且基于 example of Nikolay Tsonev ,将jagged数组转换为具有JSON对象的数组会更容易 . 以下是this code用于处理数组所需的操作 .
但是你还有另一个解决方案 - 你可以通过$ parents [Page] .someDataModel为你的嵌套转发器提供完全不同的绑定源 . 例如:
这也需要您手动将二维数组转换为更合适的形式 . 更多关于 $parents 这个here