【发布时间】:2021-06-21 04:45:57
【问题描述】:
在 Knockout.js 中,将对象嵌套在数组中的最佳方法是什么?
我试图有一个按一组课程标题排序的可观察对象,并且在里面会有不同的课程提供给具有相同组属性(课程代码)的学生。
这是我的sn-p
function course(_id, _code, _title, _campus) {
var self = this;
this.id = ko.observable(_id);
this.courseCode = ko.observable(_code);
this.courseTitle = ko.observable(_title);
this.coursecampus = ko.observable(_campus);
}
function gpCourseProperties(_code, _isHidden) {
var self = this;
this.gpCode = ko.observable(_code);
this.hide = ko.observable(_isHidden);
this.courses = ko.observableArray();
this.addCourse = function (_id, _courseCode, _courseTitle, _courseCampus) {
self.courses.push(new course(_id, _courseCode, _courseTitle, _courseCampus));
}
this.switchMutated = function (code) {
self.hide(!self.hide());
};
this.switchText = ko.computed(function () {
if (self.hide() == true) {
return '+'
}
return '-';
}, this);
}
function ViewModel() {
var self = this;
this.gpCourseProp = ko.observableArray();
self.gpCourseProp.push(new gpCourseProperties("MATH1030", true));
self.gpCourseProp.push(new gpCourseProperties("MATH1006", true));
self.gpCourseProp.push(new gpCourseProperties("MATH1046", true));
for (i = 0; i < self.gpCourseProp().length; i++) {
if (self.gpCourseProp()[i].gpCode == "MATH1030") {
self.gpCourseProp()[i].addCourse(new course("1", "MATH1030", "Calculus", "City1"));
self.gpCourseProp()[i].addCourse(new course("2", "MATH1030", "Calculus", "City2"));
self.gpCourseProp()[i].addCourse(new course("3", "MATH1030", "Calculus", "City3"));
}
if (self.gpCourseProp()[i].gpCode == "MATH1006") {
self.gpCourseProp()[i].addCourse(new course("4", "MATH1006", "Linear algebra", "City1"));
self.gpCourseProp()[i].addCourse(new course("6", "MATH1006", "Linear algebra", "City2"));
}
if (self.gpCourseProp()[i].gpCode == "MATH1046") {
self.gpCourseProp()[i].addCourse(new course("5", "MATH1046", "Discrete Math", "City1"));
self.gpCourseProp()[i].addCourse(new course("7", "MATH1046", "Discrete Math", "City2"));
}
}
}
var vm = new ViewModel();
ko.applyBindings(vm);
tr.mutated {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Course Code</th>
<th>Course Title</th>
<th>Course Campus</th>
</tr>
</thead>
<tbody data-bind="foreach: gpCourseProp">
<tr class="table-dark">
<td></td>
<td><span data-bind="text: $data.gpCode"></span></td>
<td><span></span></td>
<td></td>
</tr>
<!-- ko foreach: $data.courses -->
<tr data-bind="css: { mutated: $parent.mutated.hide() == true }">
<td><span data-bind="text: $data.id"></span></td>
<td><span data-bind="text: $data.courseCode"></span></td>
<td><span data-bind="text: $data.courseTitle"></span></td>
<td><span data-bind="text: $data.coursecampus"></span></td>
</tr>
<!-- /ko -->
</tbody>
</table>
我遇到的问题是尝试将课程添加到 gpCourseProperties。
【问题讨论】:
-
'我有 jsfiddle,但我有一些问题。这里是 jsfiddle。” 请使用 Stack Snippets(
[<>]工具栏按钮)在这里,现场执行您的可运行示例;here's how to do one。
标签: javascript knockout.js knockout-2.0