我在主 viewModel - QuoteSimpleSearchVM中有一个名为simpleSearchResultsArray的数组。
数组simpleSearchResultsArray包含SimpleSearchResults对象。
在每个SimpleSearchResults中定义了一个名为quoteDetailsObj的对象。数据被添加到quoteDetailsObj中。并且对象quoteDetailsObj绑定到视图。我无法将此嵌套数组视图模型绑定到视图。这是 jsfiddle 的链接:http: //jsfiddle.net/FENuV/29/
查看代码:
<div id="QdetailsDiv">
<div>
<div>
<div><span>Sold to Account</span></div>
<div>
<input type="text" id="txtSoldToAccount" data-bind="value:SoldToAccount"/>
</div>
</div>
<div>
<div><span>Job Name</span></div>
<div>
<input type="text" id="txtQDJobName" data-bind="value:jobName"/></div>
</div>
</div>
<input type="button" value="unbind" id="unbindbutton"></input>
</div>
视图模型代码:
QuoteDetails = function () {
var self = this;
self.SoldToAccount = ko.observable();
self.jobName = ko.observable();
};
function SimpleSearchResults() {
var self = this;
self.index = ko.observable();
self.quoteDetailsObj= null;
self.BindDataToQuotesDetailVM = function (SimpleSearchResultsObj) {
SimpleSearchResultsObj.quoteDetailsObj = ko.observable(new QuoteDetails());
SimpleSearchResultsObj.quoteDetailsObj.SoldToAccount = ko.observable("SoldToAccount test");
SimpleSearchResultsObj.quoteDetailsObj.jobName = ko.observable("jobName test");
ko.applyBindings(SimpleSearchResultsObj.quoteDetailsObj, document.getElementById("QdetailsDiv"));
};
};
function QuoteSimpleSearchVM() {
var self = this;
self.simpleSearchResultsArray = ko.observableArray([]);
var SimpleSearchResultsObj= new SimpleSearchResults();
SimpleSearchResultsObj.index=ko.observable("1");
self.simpleSearchResultsArray.push(SimpleSearchResultsObj);
var SimpleSearchResultsObj2= new SimpleSearchResults();
SimpleSearchResultsObj2.index=ko.observable("2");
self.simpleSearchResultsArray.push(SimpleSearchResultsObj2);
};
var obj= new QuoteSimpleSearchVM();
$(document).ready(function () {
$("#unbindbutton").click(function(){
var element = $('#QdetailsDiv')[0];
ko.cleanNode(element);
});
ko.utils.arrayForEach(obj.simpleSearchResultsArray(), function (SimpleSearchResultsObj) {
if (SimpleSearchResultsObj.index() === "2") {
SimpleSearchResultsObj.BindDataToQuotesDetailVM(this);
}
});
});
谁能帮我解决这个问题?
提前致谢。