[英]Get context of a tapped list item in Nativescript
Is it possible to get the context of a tapped item in a listview? 是否有可能在列表视图中获取点击项的上下文? For example if I have a list of three items how can I tell that it was the second item clicked and get the data from the object that relates to that item in the observable array? 例如,如果我有一个包含三个项目的列表,该如何判断这是单击的第二个项目,并从可观察数组中与该项目相关的对象获取数据?
EX: Here is my list template, if the user tapped the second connection in the list of connections, how could I get the data associated with that connection based off the clicked items index? 例:这是我的列表模板,如果用户点击了连接列表中的第二个连接,我如何基于单击的项目索引获取与该连接相关的数据?
<ListView items="{{ connections }}" loaded="" itemLoading="" itemTap="">
<ListView.itemTemplate>
<StackLayout class='connection-li'>
<GridLayout class="connection-info" rows="" columns="auto, *, *" tap="goToMeasurements">
<Image col="0" src="res://ic_person_black_36dp" stretch ="none" />
<Label col="1" class="connection-name" text="{{ PatientFirstName + ' ' + PatientLastName }}" textWrap="false" />
<Image col="2" verticalAlignment="middle" horizontalAlignment="right" src="res://ic_cancel_black_18dp" stretch ="none" />
</GridLayout>
</StackLayout>
</ListView.itemTemplate>
</ListView>
EDIT: Based off the docs I found the following event that I can hook into to get the index data, but where it references ListView how can I make that a specific reference to the one I'm interested in, can I give it a class and access it that way? 编辑:基于文档,我发现以下事件可以挂入索引数据,但是它引用ListView的地方如何使对我感兴趣的那个对象的特定引用成为现实,所以我可以给它一个类并以这种方式访问它?
listView.on(listViewModule.ListView.itemTapEvent, function (args: listViewModule.ItemEventData) {
var tappedItemIndex = args.index;
var tappedItemView = args.view;
//// Do someting
});
You don't need a separate class, just have a javascript file that corresponds to this page (ie main-page.xml and main-page.js). 您不需要单独的类,只需有一个与此页面相对应的javascript文件(即main-page.xml和main-page.js)。 Then, you can wire up the event in that itemTap
attribute: 然后,您可以在该itemTap
属性中itemTap
事件:
<ListView items="{{ connections }}" itemTap="onConnectionTapped">
Then export that event from your corresponding javascript file, and you can access the data object for the item a couple different ways: 然后从相应的javascript文件中导出该事件,然后可以通过两种不同的方式访问该项目的数据对象:
export function onConnectionTapped(args) {
var tappedView = args.view,
//the View will have a bindingContext
// set to the individual item from the list
tappedItem = tappedView.bindingContext;
//or, if you need the entire list as well,
// get it from the Page's bindingContext
// as each View has a ref to the Page it's on
var pageBindingContext = tappedView.page.bindingContext,
fullItemsList = pageBindingContext.connections,
itemForTap = fullItemsList[args.index];
}
In the goToMeasurements
handler args.object
will be GridLayout
. 在goToMeasurements
处理程序中, args.object
将为GridLayout
。 Its .bindingContext
property will the a data item from your connections
array. 它的.bindingContext
属性将是您的connections
数组中的一个数据项。 connections.indexOf(dataItem)
will return the index of this data item if you need its index. 如果需要索引, connections.indexOf(dataItem)
将返回此数据项的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.