我刚刚完成阅读和测试 nativescript helloworld app,我不明白如何通过单击删除项目。我理解它就像我需要在单击后获取数组索引以在其上制作 .splice 一样,但是在 args 中我现在有这样的数据?请解释我如何做到这一点。谢谢!
任务.js
var observableModule = require("data/observable");
var observableArray = require("data/observable-array");
var viewModule = require("ui/core/view");
var tasks = new observableArray.ObservableArray([]);
var pageData = new observableModule.Observable();
var page;
exports.onPageLoaded = function(args) {
page = args.object;
pageData.set("task", "");
pageData.set("tasks", tasks);
page.bindingContext = pageData;
};
exports.add = function() {
tasks.push({ name: pageData.get("task") });
pageData.set("task", "");
viewModule.getViewById( page, "task" ).dismissSoftInput();
};
exports.del_first = function() {
tasks.splice(0,1);
viewModule.getViewById( page, "task" ).dismissSoftInput();
console.log('DEL');
};
exports.remove = function(args) {
console.log('REM');
};
任务.xml
<Page loaded="onPageLoaded">
<GridLayout rows="auto, *">
<StackLayout orientation="horizontal" row="0">
<TextField width="200" text="{{ task }}" hint="Enter a task" id="task" />
<Button text="Add" tap="add"></Button>
<Button text="Delete 1st" tap="del_first"></Button>
</StackLayout>
<ListView items="{{ tasks }}" row="1">
<ListView.itemTemplate>
<StackLayout orientation="horizontal" row="0">
<Label text="{{ name }}" />
<Button text="x" tap="remove"></Button>
</StackLayout>
</ListView.itemTemplate>
</ListView>
</GridLayout>
</Page>