gpt4 book ai didi

knockout.js - 在 ObservableArray 中观察\更新数据

转载 作者:行者123 更新时间:2023-12-04 05:23:18 25 4
gpt4 key购买 nike

我正在学习 Knockout.js 并且我有一个关于更新嵌套数据的问题,我在做什么可能还是我需要重新考虑我的模型?

这是 View 模型中的数据:

this.menu = ko.observableArray([
{text: "one",
children: [{text: "child 1.1", checked: true},{text: "child 1.2", checked: false}]},
{text: "two",
children: [{text: "child 2.1", checked: false},{text: "child 2.2", checked: true}]},
{text: "three",
children: [{text: "child 3.1", checked: false}]}
]);

这是 fiddle :

http://jsfiddle.net/4aqrg/1/

数据在最后写出来,所以我们可以看到它的状态。

我希望在 HTML View 中更改复选框时更新选中的值,但这并没有发生。在状态变化的背后,我想采取一些行动。在这种情况下,操作将是在图表中显示数据系列(仍然要做那部分)。

提前致谢,
马特

最佳答案

您示例中的问题是您使数组可观察,但单个属性不可观察。

您需要使每个“已检查”属性 ko.observable 才能更新。

像这样:

this.menu = ko.observableArray([
{text: "one",
children: [{text: "child 1.1", checked: ko.observable(true)},{text: "child 1.2", checked: ko.observable(false)}]},
{text: "two",
children: [{text: "child 2.1", checked: ko.observable(false)},{text: "child 2.2", checked: ko.observable(true)}]},
{text: "three",
children: [{text: "child 3.1", checked: ko.observable(false)}]}
]);

如果您希望文本也是可观察的,您还必须将其包装起来: ko.observable("child 1.1")

关于knockout.js - 在 ObservableArray 中观察\更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13478870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com