当我添加新教师时,它会对教师和包含的助手进行排序,但UI不会刷新。请参阅textarea控件,显示在添加新教师后,教师可观察数组对其中的asstCols进行排序和排序,但UI不会刷新。我尝试在self.teachers()可观察数组和包含的asstCols()上添加valueHasMutated(),但无济于事。我不知道我所做的事情是否过于复杂,但较大的应用程序也能够添加新的列/助手,并且需要能够按字母顺序对它们进行排序。添加。因此,我故意以错误的顺序添加助理列以演示UI列刷新的问题。有趣的是,当你将Doug添加为教师时,它确实将他添加到教师可观察数组的顶部,并且UI将他置于顶部,以便工作,但列需要一些注意。如果您单击Doug第二列中的复选框,则可以看到它正确更新了百分比,但是没有更新原始教师的权限,因为UI不同步。
查看:
<table>
<tr style="color:white; background-color:grey">
<td></td>
<!-- ko foreach: columns -->
<td colspan="2" data-bind="text: $data"></td>
<!-- /ko -->
</tr>
<tbody data-bind="foreach: teachers">
<tr>
<td data-bind="text: name"></td>
<!-- ko foreach: asstCols -->
<td class="cellLeft"><input data-bind="checked: isChecked, click: $root.updatePercentage" type="checkbox" /></td>
<td class="cellRight" data-bind="text: accounting.toFixed(percentage(), 0) + '%'"></td>
<!-- /ko -->
</tr>
</tbody>
</table>
<br />
<table class="reset">
<tr>
<td>Teacher:</td>
<td style="padding-left:5px;">
<select data-bind="
options: availableTeachers,
optionsText: 'name',
value: selectedTeacher">
</select>
</td>
<td style="padding-left:5px;"><button data-bind="click: addTeacher">Add Teacher</button></td>
</tr>
</table>
<textarea style="width:620px; height:300px;" data-bind="text: ko.toJSON($data)"></textarea>
查看型号:
// methods
var Teacher = function (id, name, asstCols) {
this.id = id;
this.name = name;
this.asstCols = ko.observableArray(asstCols).extend({ rateLimit: 0 }); // trigger just one re-evaluation of computed observable
};
var AsstCol = function (id, asstName, isChecked, percentage) {
this.id = id;
this.asstName = ko.observable(asstName);
this.isChecked = ko.observable(isChecked);
this.percentage = ko.observable(percentage);
};
var Assistant = function (id, name) {
this.id = id;
this.name = name;
};
var viewModel = function (teachers, assistants, columnList) {
var self = this;
self.teachers = ko.observableArray(teachers).extend({ rateLimit: 0 });
self.columns = ko.observableArray();
// set initial columns
for (var index in columnList) {
self.columns.push(columnList[index].asstName());
};
self.assistants = ko.observableArray(assistants).extend({ rateLimit: 0 });
self.selectedTeacher = ko.observable("0");
self.availableTeachers = ko.observableArray([
new Teacher(5, "Doug", [{}]),
new Teacher(6, "Kevin", [{}])
]);
// methods
// passes current item as the first parameter
self.updatePercentage = function (asstCol) {
var totalChecked = 0, percentage = 0;
ko.utils.arrayForEach(self.teachers(), function (teacher) {
for (var i = 0; i < self.columns().length; i++) {
if (teacher.asstCols()[i].id == asstCol.id) {
if (teacher.asstCols()[i].isChecked()) {
totalChecked++;
break;
}
}
}
});
percentage = 100 / totalChecked;
ko.utils.arrayForEach(self.teachers(), function (teacher) {
for (var i = 0; i < self.columns().length; i++) {
if (teacher.asstCols()[i].id == asstCol.id) {
if (teacher.asstCols()[i].isChecked())
teacher.asstCols()[i].percentage(percentage);
else
teacher.asstCols()[i].percentage(0);
}
}
});
return true; // return default browser behavior to allow check/uncheck
};
// operations
self.addTeacher = function () {
self.teachers.push(new Teacher(ko.unwrap(self.selectedTeacher().id), ko.unwrap(self.selectedTeacher().name), undefined));
// add each of the asst columns
ko.utils.arrayForEach(self.assistants(), function (assistant) {
self.teachers()[self.teachers().length - 1].asstCols.push(new AsstCol(assistant.id, assistant.name, false, 0));
});
self.teachers.sort(function(a, b) {
return a.name.toLowerCase() == b.name.toLowerCase() ? 0 : (a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1);
});
self.columns.sort();
ko.utils.arrayForEach(self.teachers(), function (teacher) {
teacher.asstCols().sort(function(a, b) {
return a.asstName().toLowerCase() == b.asstName().toLowerCase() ? 0 :
(a.asstName().toLowerCase() < b.asstName().toLowerCase() ? -1 : 1);
});
});
// remove teacher from dropdown and reset the selection
self.availableTeachers.remove(function (item) { return item.id == ko.unwrap(self.selectedTeacher().id); });
self.selectedTeacher("0");
};
};
var initialTeachers = [
new Teacher(1, "Jeff", [
new AsstCol(20, "Susie", true, 50),
new AsstCol(21, "Bobby", true, 33)
]),
new Teacher(2, "Joe", [
new AsstCol(20, "Susie", false, 0),
new AsstCol(21, "Bobby", true, 33)
]),
new Teacher(3, "Josie", [
new AsstCol(20, "Susie", true, 50),
new AsstCol(21, "Bobby", true, 33)
])
];
var initialAssistants = [
new Assistant(20, "Bobby"),
new Assistant(21, "Susie")
];
var vm = new viewModel(initialTeachers, initialAssistants, initialTeachers[0].asstCols());
ko.applyBindings(vm);
的CSS:
table {
border-spacing: 0px;
border-collapse: collapse;
}
td, th {
border: solid 1px black;
padding: 2px;
}
.reset td, th {
border: 0;
padding: 0;
}
.cellLeft {
border: 0;
border-left:solid 1px black;
border-bottom:solid 1px black;
padding:1px 1px 1px 4px;
}
.cellRight {
border: 0;
border-right:solid 1px black;
border-bottom:solid 1px black;
padding:1px; padding:1px 4px 1px 1px;
}
答案 0 :(得分:1)
两个问题:
teacher.asstCols()
:您在执行排序之前阅读了observable。您的数据不匹配。您已对id
属性值进行了拼写错误。
var initialAssistants = [
new Assistant(20, "Bobby"),
new Assistant(21, "Susie")
];
new Teacher(1, "Jeff", [
new AsstCol(20, "Susie", true, 50),
new AsstCol(21, "Bobby", true, 33)
])