简体   繁体   English

如何从NativeScript中的ListView点击事件获取特定的列表项

[英]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.

相关问题 Angular2 Nativescript:如何在点击时传递 ListView 项目 - Angular2 Nativescript: how to pass ListView item on tap 当我点击 ngfor 和 ngif 时,如何仅显示项目的信息? (本机脚本/角) - How can I show only the infos of the item when i tap with ngfor and ngif? (Nativescript/Angular) NativeScript Angular - 在点击事件中获取X和Y. - NativeScript Angular - Get X and Y on tap event 删除 ListView 项目突出显示点击 Nativescript Angular + LineSeparator 颜色? - Remove ListView item highlight on tap Nativescript Angular + LineSeparator Color? Nativescript:如何从画廊获取照片列表 - Nativescript: How to get photo list from gallery Nativescript和ListView,项目未定义 - Nativescript and ListView, item undefined Nativescript Angular:如何在点击事件中将按钮视图作为参数传递? - Nativescript Angular: How to pass a button view as a parameter in tap event ? Angular 5:我如何在项目列表中使用click事件? - Angular 5 : how can i use click event in item list? 如何从列表中的每个项目获取真实图像值并将其订阅到另一个列表? - How can I get the real image value from each item in my list and subscribe it to another list? 如何使用angular2本机脚本在列表视图中隐藏某些行项目 - How to hide some row item in listview with angular2 nativescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM