Kon*_*nev 4 javascript knockout.js
我有一个ViewModel带有可观察数组的knockout.js :
function ItemsViewModel() {
this.data = ko.observableArray([
new Item(1, "One description"),
new Item(2, "Two description"),
new Item(3, "Three description"),
// ... etc
]);
}
Run Code Online (Sandbox Code Playgroud)
该项看起来像这样:
function Item(id, name) {
this.id = ko.observable(id);
this.name = ko.observable(name);
};
Run Code Online (Sandbox Code Playgroud)
根据我在我创建的可观察数组,我ViewModel想创建一个第二个计算数组,如下所示:
function ItemsViewModel() {
this.data = ko.observableArray([
new Item(1, "One description"),
new Item(2, "Two description"),
new Item(3, "Three description"),
// ... etc
]);
this.computedData = // here I want to create a computed array based on the values
// of this.data
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到如何在knockout.js文档中的任何位置创建此计算数组的示例.能否请您举例说明如何将第一个数组转换为表单的计算数组:
this.computedData = [
{ dataItem: data[0].name + ' ' + data[0].id },
{ dataItem: data[1].name + ' ' + data[1].id },
// ... etc.
]
Run Code Online (Sandbox Code Playgroud)
Art*_*kov 11
您可以使用computedobservable:
self.computedData = ko.computed(function() {
return ko.utils.arrayMap(self.data(), function(item) {
return { dataItem: item.name() + ' ' + item.id() };
});
});
Run Code Online (Sandbox Code Playgroud)
这是工作示例:http://jsfiddle.net/vyshniakov/3fesA/1/
阅读更多有关计算文档的信息:http://knockoutjs.com/documentation/computedObservables.html
| 归档时间: |
|
| 查看次数: |
7522 次 |
| 最近记录: |