[英]How can I get an specific list item from ListView tap event in nativescript
I'm trying to get data from a tap event using angular2 + typescript: 我正在尝试使用angular2 +打字稿从轻击事件中获取数据:
this is the component HTML: 这是组件HTML:
<RadListView row="1" [items]="groceryList" [class.visible]="listLoaded" (tap)="seeItem($event)"
swipeActions="true" (itemSwipeProgressStarted)="onSwipeCellStarted($event)">
<ng-template let-item="item">
<Label [text]="item.name" class="p-15 list-item"></Label>
</ng-template>
<GridLayout *tkListItemSwipeTemplate columns="*, auto">
<StackLayout id="delete-view" col="1" (tap)="delete($event)" class="delete-view">
<Image src="~/images/delete.png"></Image>
</StackLayout>
</GridLayout>
</RadListView>
This is the component code: 这是组件代码:
seeItem(args: ListViewEventData) {
console.log('item ', <Grocery>args.object.bindingContext);
console.log('in case');
}
When I tap an element from my ListView I get the console.log: 当我从ListView中点击一个元素时,我得到console.log:
[Eric Guzmán’s iPhone]: item undefined
[Eric Guzmán’s iPhone]: in case
The "delete" event (in this component) works using the same logic, when tap: 点击时,“删除”事件(在此组件中)使用相同的逻辑进行工作:
delete(args: ListViewEventData) {
let grocery = <Grocery>args.object.bindingContext;
this.groceryService.delete(grocery.id)
.subscribe(() => {
let index = this.groceryList.indexOf(grocery);
this.groceryList.splice(index, 1);
});
}
I'm a beginner in NativeScript and I would appreciate the help, 我是NativeScript的初学者,非常感谢您的帮助,
If you are expecting to get the item on tap, here are the changes you need to make 如果您希望轻按一下该项目,则需要进行以下更改
replace 更换
<ng-template let-item="item">
<Label [text]="item.name" class="p-15 list-item"></Label>
</ng-template>
with 与
<ng-template let-item="item">
<Label [text]="item.name" (tap)="seeItem(item)" class="p-15 list-item"></Label>
</ng-template>
And make the changes in the function 并在功能上进行更改
seeItem(item: Grocery) {
console.log('item ', item);
console.log('in case');
}
Try this it should work. 试试这个,它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.