- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚完成阅读和测试 nativescript helloworld app ,我不明白如何通过单击删除项目。我理解它就像我需要在单击以对其进行 .splice 后获取数组索引,但是在 args 中我现在有这样的数据?请向我解释如何执行此操作。谢谢!
tasks.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');
};
tasks.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>
最佳答案
问题是按钮不知道要删除哪个任务。除了您传递给它的值,它没有传递任何值,即 text="x"和 tap="remove"。
因此,诀窍是将您自己的附加值分配给包含任务 ID/索引的按钮,以便您可以将其匹配回任务。您可以为按钮属性值和对象键使用任何未分配的名称(我选择使用 index="{{ index }}"。所以 deleteId ="{{ id }}"也同样有效。
因此,目前最好的方法是对您的代码进行一些小的更改:
tasks.js
var observableModule = require("data/observable");
var observableArray = require("data/observable-array");
var viewModule = require("ui/core/view");
// Pretend we loaded some records, notice the new "index" field?
// This is what name we use in the xml, so it is: "{{ index }}"
// Again this name can be anything you want; id, index, guid, uuid, etc...
var _tasks = [{name: 'aaa', index: 0}, {name: 'bbb', index: 1},
{name: 'ccc', index: 2},{name: 'ddd', index: 3},{name: 'eee', index: 4}];
// Simple _index count since I cheated and created a fixed array;
// the better way would be to enumerate the array
// and set _index to the highest index found in the array.
var _index = _tasks.length;;
var tasks = new observableArray.ObservableArray(_tasks);
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() {
// Note we are creating a new index, always incrementing so that the
// _index will always be unique. UUID's would also work.
var newIndex = ++_index;
// Push the name and new created index again using the same "index" field
tasks.push({ name: pageData.get("task"), index: newIndex });
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) {
// The args is a event packet, with
// eventName = what event occurred
// object = the object this event occurred against
// So we need to use the target object,
// then we are getting the "index" PROPERTY we assigned on it in the xml
var target = args.object;
var index = target.index;
// Now we search all the tasks, to find the index that matches our
// index that the button was pressed on. Then we delete it and break out of the loop.
for (var i=0;i<tasks.length;i++) {
if (tasks.getItem(i).index === index) {
tasks.splice(i,1);
break;
}
}
console.log('REM');
};
tasks.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 }}" />
<!------------ Notice the new "index" property ----------->
<Button text="x" index="{{ index }}" tap="remove"/>
</StackLayout>
</ListView.itemTemplate>
</ListView>
</GridLayout>
</Page>
关于javascript - 从 helloworld 应用程序中删除 nativescript listview 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383879/
这个问题已经有答案了: What does "Could not find or load main class" mean? (63 个回答) 已关闭 7 年前。 我用java语言编写了一个简单的程
在使用 Expo 的 React Native 项目中,我尝试使用 export 部署以下云函数: 注意:我在 index.js 中使用 Javascript。 export const helloW
解决了“无法找到或加载主类Hello”错误的问题后...运行程序后我再次遇到这个问题(我使用文本板)代码是: class HelloWorld { public static void m
我最初尝试像这样推送我的(有史以来第一次!)git repo: $ git push helloworld 但我得到了这个: To git-smichaels@free5.projectlocker.
全新安装,通过 vue cli 安装 Vue。运行 vue create app并使用基本配置进行设置。 从 Home.vue 中删除 HelloWorld.vue 组件和附带的导入代码。 运行 np
我第一次编程有点问题。我收到消息 可以获取“c:\users\amittler\source\repos\HelloWorld...”吗? 谢谢你的帮助 最佳答案 是的,得到这个就可以了。 您的 ID
好的,我只是无法让 java 运行我的 .class 文件:我按照 Oracle tutorial 中的步骤操作并尝试运行这个程序: class HelloWorldApp { public
我是 Scala 和 SBT 的新手,所以我可能会遗漏一些明显的东西。 我试图在 http://www.scalafx.org/docs/quickstart/ 上编译 HelloWorld 示例 我
这是我的设置: docker pull riot/riotbuild wget https://github.com/RIOT-OS/RIOT/archive/2019.04.zip unzip 20
我一直在尝试创建我的第一个 Jenkins 插件。一切都很好,只是全局配置在 jenkins 服务重新启动后不会保留。 只要服务不重新启动,配置就可以很好地保存。 全局配置果冻文件... Jenki
我尝试从终端运行基本的 HelloWorld.class 文件。 我使用以下输入: Java HelloWorld.class 但它说: Error: Could not find or load "
以下代码取自 http://doc.akka.io/docs/akka/2.2.3/AkkaScala.pdf import akka.actor.Actor object Greeter { c
我带着这样一个微不足道的问题来到 Stack Exchange 感到内疚,但我整个上午都在为它苦苦思索,似乎无处可去。我正在尝试运行 Grails 教程中的简单 HelloWorld 应用程序:htt
好吧,这真的让我很生气。我在我的模拟器和安卓设备上都运行了它。该代码不显示“Helloworld, Android -mykong.com”。我启动应用程序,在模拟器上找到它,单击它,它会转到应用程序
我回到了 c++(我已经好几年没用过它了)来学习 box2d API。 我确切地说我在 Linux 系统 (Ubuntu) 上并且我已经从 this guide 之后的源代码安装了 box2d(2.3
社区成员推荐我分析以下内容,以了解 ARM 架构中发出的系统调用。我怀疑如何在这种结构中获取程序。我已经尝试过 gdb disas 命令,但部分不可见。请帮助。 .data HelloWorldStr
在编译我的 wxWidget HelloWorld 应用程序时,出现以下错误: Warning 1 warning LNK4098: defaultlib 'LIBCMTD' conflicts
我刚刚为我的 eclipse 安装了 c/c++ 开发工具,一切正常,除了当我运行 hello world 程序时控制台中没有打印任何文本,但我没有收到任何错误。我真的很困惑,有人知道这是为什么吗?
我在以下文件夹 C:\Program Files\Java 中安装了 Java 1.7.0。我的操作系统是带有 Service Pack 3 的 Windows XP(2002 版)。 我设置的环境变
前言 讲解Spring之前,我们首先梳理下Spring有哪些知识点可以进行入手源码分析,比如: Spring IOC依赖注入 Spring AOP切面编程 Spri
我是一名优秀的程序员,十分优秀!