gpt4 book ai didi

javascript - 在knockout.js中绑定(bind)多个viewmodel

转载 作者:行者123 更新时间:2023-11-28 00:20:28 25 4
gpt4 key购买 nike

我需要提供员工的教育详细信息和工作经历。所以我决定使用 Knockout JS 来实现动态表单。

这是我的代码:

    //Data
var History = function () {
var self = this;
self.CompanyName = ko.observable();
self.Designation = ko.observable();
self.StartDate = ko.observable();
self.EndDate = ko.observable()
};

var viewModelEmploymentHistory = function () {
// Operations
var self = this;
self.historyList = ko.observableArray([new History()]); // Put one line in by default
self.addHistory = function () { self.historyList.unshift(new History()) }
self.removeHistory = function (history) {
self.historyList.remove(history)
}
};

//Data
var Education = function () {
var self = this;
self.Degree = ko.observable();
self.YearOfPassing = ko.observable();
self.Percentage = ko.observable();
self.Institute = ko.observable()

};

var viewModelEducationalDetails = function () {
// Operations
var self = this;
self.educationList = ko.observableArray([new Education()]); // Put one line in by default
self.addEducation = function () { self.educationList.unshift(new Education()) };
self.removeEducation = function (education) {
self.educationList.remove(education)
}
};

//var masterVM = (function () {
// this.viewModelEducationalDetails = new viewModelEducationalDetails(),
// this.viewModelEmploymentHistory = new viewModelEmploymentHistory();
//})();

//ko.applyBindings(masterVM)
//ko.applyBindings(new viewModelEmploymentHistory());

ko.applyBindings(new viewModelEducationalDetails(), document.getElementById("containerEducation"));
ko.applyBindings(new viewModelEmploymentHistory(), document.getElementById("containerHistory"));

HTML:

    <div  id="containerHistory" >    
<div data-bind="foreach: historyList">
<div class=" col-md-9 col-lg-10 border ">
<div ><a href="#" class="hrefRemove" data-bind='click: $parent.removeHistory'>Remove</a></div>
<div class="form-group">
@Html.LabelFor(model => model.CompanyName, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
<div class="col-sm-4 col-md-5 col-lg-5">
@Html.TextBoxFor(model => model.CompanyName, new { @Class = "form-control", placeholder = "Company Name", data_bind = "value: CompanyName, namePath: true" })
</div>
<div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.CompanyName) </div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Designation, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
<div class="col-sm-4 col-md-5 col-lg-5">
@Html.TextBoxFor(model => model.Designation, new { @Class = "form-control", placeholder = "Designation", data_bind = "value: Designation, namePath: true" })
</div>
<div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Designation) </div>
</div>
<div class="form-group">
@*@Html.HiddenFor(m=>m.DateOfBirth)*@
@Html.LabelFor(model => model.StartDate, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
<div class="col-sm-4 col-md-5 col-lg-5">

@Html.EditorFor(model => model.StartDate, new { placeholder = "MM/DD/YYYY", @Class = "hasDatepicker dateClass", data_bind = "value: StartDate , namePath: true" })
</div>
<div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.StartDate) </div>
</div>
<div class="form-group">
@*@Html.HiddenFor(m=>m.DateOfBirth)*@
@Html.LabelFor(model => model.EndDate, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
<div class="col-sm-4 col-md-5 col-lg-5">
@Html.EditorFor(model => model.EndDate, new { placeholder = "mm/dd/yyyy", @Class = "hasDatepicker dateClass", data_bind = "value: EndDate, namePath:true" })

</div>
<div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.EndDate) </div>
</div>
</div>
<br />
<br />

</div>

</div>




div id="containerEducation">

<div data-bind="foreach: educationList">
<div class=" col-md-9 col-lg-10 border ">
<div><a href="#" class="hrefRemoveEducation" data-bind='click: $parent.removeEducation'>Remove</a></div>

<div class="form-group">
@Html.LabelFor(model => model.Degree, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
<div class="col-sm-4 col-md-5 col-lg-5">
@Html.TextBoxFor(model => model.Degree, new { @Class = "form-control", placeholder = "Degree Name", data_bind = "value: Degree, namePath: true" })
</div>
<div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Degree) </div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Institute, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
<div class="col-sm-4 col-md-5 col-lg-5">
@Html.TextBoxFor(model => model.Institute, new { @Class = "form-control", placeholder = "Institute Name", data_bind = "value: Institute, namePath: true" })
</div>
<div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Institute) </div>
</div>
<div class="form-group">
@*@Html.HiddenFor(m=>m.DateOfBirth)*@
@Html.LabelFor(model => model.Percentage, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
<div class="col-sm-4 col-md-5 col-lg-5">

@Html.TextBoxFor(model => model.Percentage, new { @Class = "form-control", placeholder = "Percentage", data_bind = "value: Percentage, namePath: true" })

</div>
<div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Percentage) </div>
</div>
<div class="form-group">
@*@Html.HiddenFor(m=>m.DateOfBirth)*@
@Html.LabelFor(model => model.YearOfPassing, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
<div class="col-sm-4 col-md-5 col-lg-5">
@Html.TextBoxFor(model => model.YearOfPassing, new { @Class = "form-control", placeholder = "YearOfPassing", data_bind = "value: YearOfPassing, namePath: true" })

</div>
<div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.YearOfPassing) </div>
</div>
</div>
<br />
<br />

</div>

</div>

当我尝试添加更多 div 时,出现如下控制台错误。

Uncaught Error: Unable to parse bindings. Message: ReferenceError:
historyList is not defined; Bindings value: foreach: historyList

请注意:当我绑定(bind)单个 View 模型时,这工作正常。

最佳答案

这就是窍门。

 var viewModelEmploymentHistory = function () {
// Operations
var self = this;
self.historyList = ko.observableArray([new History()]); // Put one line in by default
self.addHistory = function () { self.historyList.unshift(new History()) }
self.removeHistory = function (history) {
self.historyList.remove(history)
}
self.educationList = ko.observableArray([new Education()]); // Put one line in by default
self.addEducation = function () { self.educationList.unshift(new Education()) };
self.removeEducation = function (education) {
self.educationList.remove(education)}
};

关于javascript - 在knockout.js中绑定(bind)多个viewmodel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078416/

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