如何在Nativescript / JS中从ListView点击事件传递/接收项目详细信息?

时间:2019-04-12 00:05:53

标签: nativescript

我想做的(我认为)是从 itemTap 事件获取 store.id 并将其与 .navigate( ),因此我可以使用它来获取详细信息页面的正确数据,但是我不知道如何获取被点击的项目。

我有一个list-page.xml:

 <Page loaded="loaded" xmlns:lv="nativescript-ui-listview">
<ActionBar title="Bars"></ActionBar>
<GridLayout>
    <ListView items="{{ storeList }}" row="1" itemTap="showDetail">
        <ListView.itemTemplate >
            <GridLayout class="grocery-list-item" >
                <Label class="p-15" text="{{ name }}" />
            </GridLayout>
        </ListView.itemTemplate>
    </ListView>
</GridLayout>

和list-page.js:

var dialogsModule = require("tns-core-modules/ui/dialogs");
var observableModule = require("tns-core-modules/data/observable");
var ObservableArray = require("tns-core-modules/data/observable-array").ObservableArray;
var page;
var StoreListViewModel = require("../shared/view-models/store-list-view-model");
var frameModule = require("tns-core-modules/ui/frame");
var storeList = new StoreListViewModel([]);
var pageData = new observableModule.fromObject({
    storeList:storeList
});
exports.loaded = function (args) {
    page = args.object;
    page.bindingContext = pageData;
    sindex = args.object.bindingContext;

    storeList.empty();
    storeList.load();
};
exports.showDetail = function() {
    frameModule.topmost().navigate("views/detail/detail-page");
};

storeList来自我的api,并成功发送回由listview呈现的商店列表。

我看过其他十几个教程/问题,似乎有几个提到从args.index或args.object.bindingContext获取它,但是当我从showDetail函数进行console.log(args.index)时,它是未定义的。 console.log(args.object.bindingContext)给了我很多数据,但是无论我单击哪一行都是相同的。...

CONSOLE LOG file:///app/bundle.js:359:12: {
"_observers": {
"propertyChange": [
{}
]
},
"_map": {
"storeList": {
"_observers": {
"change": [
{}
]
},
"_array": [
{
"name": "Hipster's Coffee",
"address": "2200 Broadway, Oakland, CA 94612",
"id": 2
},
{
"name": "Suzy's Stationary",
"address": "630 Divisadero St., San Francisco, CA 94117",
"id": 3
}
],
"_addArgs": {
"eventName": "change",
"object": "[Circular]",
"action": "add",
"index": 1,
"removed": [],
"addedCount": 1
},
"_deleteArgs": {
"eventName": "change",
"object": "[Circular]",
"action": "delete",
"index": null,
"removed": null,
"addedCount": 0
}
}
},
"storeList": "[Circular]"
}

我是NativeScript的新手,所以显然我在这里缺少一些简单的东西。

3 个答案:

答案 0 :(得分:2)

aggregate(counterparty.a + counterparty.b + counterparty.c ~ asset.class, assets + rating, sum)

看看示例游乐场here

答案 1 :(得分:1)

函数showDetail需要访问接收到的参数。只需在代码隐藏文件中更改以下内容即可:

exports.showDetail = function(args) {

    console.log(args); // Now there are arguments received from the itemTap event

    frameModule.topmost().navigate("views/detail/detail-page");
};

答案 2 :(得分:0)

最终找到了从args中提取项目详细信息的正确方法:

var item = args.view.bindingContext;

然后我可以访问item.name,item.id等,并像这样传递它:

var navigationOptions={
    moduleName:'views/detail/detail-page',
    context:{
        store_id: item.id
    }
}
frameModule.topmost().navigate(navigationOptions);