【发布时间】:2014-08-26 13:21:19
【问题描述】:
我有一个包含另一个数组的淘汰赛可观察数组。我正在尝试让两个数组(CountyCollections、specialCollections)的(添加/删除)方法工作。我只需要能够添加或删除特定的 CountyCollection 或 specialCollection。我需要做哪些改变?这是我的视图模型和代码。
var initialData = {
FirstName: "George",
LastName: "Jettison",
CountyCollections: [{
applicationIdentifier: "",
countyId: 0,
countyCode: "01",
countyName: "",
specialtyCollections: [{
applicationIdentifier: "99",
countyCode: "03",
specialtyId: 0,
specialtyName: "",
patentRegistrationNumber: ""
}]
}]
};
var ResultModel = function (data) {
var self = this;
self.FirstName = ko.observable(data.FirstName);
self.LastName = ko.observable(data.LastName);
self.CountyCollections = ko.observableArray(ko.utils.arrayMap(data.CountyCollections,
function (item) {
return item;
}));
self.CountyCollections.specialty = ko.observableArray([]) ;
var getById = function (items, id) {
return ko.utils.arrayFirst(items, function (item) {
return item.countyCode === id;
});
};
self.addSpecialty = function() {
alert(self.CountyCollections()[0].specialtyCollections[0].applicationIdentifier);
self.CountyCollections()[0].specialtyCollections.push(new specialtyCollections);};
self.addCounty = function () {
alert(self.CountyCollections.length);
self.CountyCollections.push(data);
};
self.removeCounty = function (data) {
self.CountyCollections.remove(data);
};
};
ko.applyBindings(new ResultModel(initialData));
这是我的部分工作的 jsfiddle:http://jsfiddle.net/CurtRabon/ntoezwuk/12/
【问题讨论】:
-
什么不起作用?我无法理解你的小提琴想要做什么......