淘汰赛js删除不起作用
我正在尝试了解淘汰赛 js 中的可观察值! 但是,我在可观察数组上实现删除函数时遇到问题。
我的js如下:
$(function () {
var data = [{ name: "name1" }, { name: "name2" }, { name: "name3"}];
var viewModel = {
namesList: ko.observableArray(data),
nameToAdd: ko.observable("name4"),
myCustomAddItem: function () {
this.namesList.push({ name: this.nameToAdd() });
},
myCustomRemove: function () {
console.log("before + " + this.nameToAdd());
this.namesList.remove(this.nameToAdd());
console.log("after + " + this.nameToAdd());
}
};
ko.applyBindings(viewModel);
});
我的html是:
Name To add/remove <input type="text" data-bind="value: nameToAdd, valueUpdate: 'afterkeydown'"/>
<ul data-bind="template: {name: 'listTemp1', foreach :namesList}">
</ul>
<p>
<button data-bind="click: myCustomAddItem">Add Item</button>
<button data-bind="click: myCustomRemove">Remove Item</button>
<script id="listTemp1" type="text/html">
<li data-bind="text:name"> </li>
</script>
</p>
我的myCustomAddItem工作正常,但不是myCustomRemove。我还在 this.namesList.remove(this.nameToAdd());
之前和之后放置了一个 console.log 以查看那里是否有任何问题,但我看不到那里有任何错误。当我单击“删除项目”按钮时,Firebug 控制台会显示日志,但该项目并未从列表中删除。
任何帮助表示赞赏
I'm trying to get my head around observables in knockout js!
However, I'm facing a problem implementing the remove function on an observable array.
My js is as follow:
$(function () {
var data = [{ name: "name1" }, { name: "name2" }, { name: "name3"}];
var viewModel = {
namesList: ko.observableArray(data),
nameToAdd: ko.observable("name4"),
myCustomAddItem: function () {
this.namesList.push({ name: this.nameToAdd() });
},
myCustomRemove: function () {
console.log("before + " + this.nameToAdd());
this.namesList.remove(this.nameToAdd());
console.log("after + " + this.nameToAdd());
}
};
ko.applyBindings(viewModel);
});
and my html is:
Name To add/remove <input type="text" data-bind="value: nameToAdd, valueUpdate: 'afterkeydown'"/>
<ul data-bind="template: {name: 'listTemp1', foreach :namesList}">
</ul>
<p>
<button data-bind="click: myCustomAddItem">Add Item</button>
<button data-bind="click: myCustomRemove">Remove Item</button>
<script id="listTemp1" type="text/html">
<li data-bind="text:name"> </li>
</script>
</p>
my myCustomAddItem works fine, but not the myCustomRemove. I also have put a console.log before and after the this.namesList.remove(this.nameToAdd());
to see if anything's wrong there, but I cannot see any error in there. When I click the "Remove Item" button, firebug console shows the logs but the item's not removed from the list.
Any help appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
remove
的参数应该是一个函数,它在是否删除某些内容时返回 true 或 false。它的工作原理与
filter
函数非常相似。在你的情况下,这样的事情应该有效:
The parameter to
remove
should be a function which returns true or false on whether to remove something.It works quite similarly to the
filter
function.In your case, something like this should work:
[这应该是对 Jani 答案的评论,但我仍然无法对其他帖子发表评论,抱歉]
只是一个小小的澄清:从技术上讲,您可以调用remove()传递要删除的元素,请参阅http://knockoutjs.com/documentation/observableArrays.html。
您的代码的问题是“data”数组中的元素是对象(包含名为“name”的属性),并且您要求从数组中删除字符串“name4”(或“nameToAdd”包含的任何内容)。
您可能会想创建一个新对象来传递以删除,如下所示:
但这仍然失败,因为 javascript 对象相等的工作方式(例如,请参见: 如何确定两个 JavaScript 对象的相等性?)。
所以,最终你还是需要使用这个函数。
在这个简单的示例中,您还可以将“namesList”数组转换为简单的字符串数组,并在模板中绑定“$data”。请参阅http://jsfiddle.net/saurus/usKwA/。
在更复杂的场景中,也许你无法避免使用对象。
[this should be a comment on Jani answer, but I can't still comment on others post, sorry]
Just a small clarification: technically you can call remove() passing the element to remove, see section "remove and removeAll" on http://knockoutjs.com/documentation/observableArrays.html.
the problem with your code is that the elements in 'data' array are objects (containing a property called 'name'), and you are asking to remove from the array the string "name4" (or whatever 'nameToAdd' contains).
You can be tempted to create a new object to pass to remove, like this:
but this still fails, because the way javascript object equality works (see, for example: How to determine equality for two JavaScript objects?).
So, in the end you need to use the function anyway.
In this simple example, you can also convert the 'namesList' array to a simple array of strings, and bind "$data" in the template. see http://jsfiddle.net/saurus/usKwA/.
In a more complex scenario, maybe you can't avoid using objects.
[observableArray].remove(function(item) { return item.[whatever] == [somevalue] ; } );
[observableArray].remove(function(item) { return item.[whatever] == [somevalue] ; } );