[英]How to remove an item from an observable array which is inside an observable array
如果在ko.observableArray内部有一个ko.observableArray,我如何从中删除项目,甚至选择数组
Usually, you wrap the array with something that makes it easier to recognize. 通常,您使用易于识别的东西包装数组。 Like:
喜欢:
this.boxes = ko.observableArray([
{ id: 1, items: ko.observableArray([1, 2, 3]) },
{ id: 2, items: ko.observableArray([4, 5]) }
]);
If you don't want to, it's probably best to either save a reference to the array before wrapping it: 如果您不想这么做,最好在包装数组之前保存对数组的引用:
const firstBox = ko.observableArray([1, 2, 3]);
const secondBox = ko.observableArray([4, 5]);
this.boxes = ko.observableArray([firstBox, secondBox]);
firstBox.remove(2);
Note that this removal will not trigger an update on boxes
. 请注意,此删除不会触发
boxes
的更新。
You can also look for an array containing the item you want to remove. 您还可以查找包含要删除的项目的数组。 Once there are multiple matches, you'll have to decide what to do...
一旦有多个匹配项,您就必须决定要做什么...
this.boxes = ko.observableArray([
ko.observableArray([1, 2, 3]),
ko.observableArray([4, 5])
]);
const remove = x => {
const inBoxes = this.boxes().filter(box => box().includes(x));
if (inBoxes.length !== 1) // What to do here?
else inBoxes[0].remove(x);
};
remove(2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.