我有一个对象,其中有一个学校列表,其中包含嵌套类和每个类中的嵌套学生
我需要绑定optionsText以显示层次结构,以便下拉菜单显示选项,如:
A学校
学生1
学生2
B学校
学生4
学生5
学生6
我能够在下拉列表中获得学校的名称,但是我能够通过课程获得的学生部分显示为“未定义”。以下绑定。
<select data-bind="options: SchoolList, optionsText: function (item) { return item.School + '|' + item.Classes.forEach(function (item) { return item.Student})}, optionsValue: 'Id', value:SchoolListId, optionsCaption: 'Select'" id="school" name="school" class="school"></select>
任何帮助都会受到欢迎,
答案 0 :(得分:0)
对我而言,听起来你正试图通过一次下拉解决太多事情。也许这比任何事情都更像是用户体验问题。另外,据我所知,你不能在optionsText
绑定中调用那种逻辑(为什么你呢?)。
我认为你需要的是一个两步形式,或者,一种各种各样的向导。
我会在下面尝试&#34;伪&#34; - 代码选项1:
function Student(){
var self = this;
//the properties
};
function School(name, students){
var self = this;
self.name = ko.observable();
self.students = ko.observableArray();
if(name)
self.name(name);
if(students)
self.students(students);
};
function ViewModel(){
var self = this;
self.schools = ko.observableArray(
new School('A', [
new Student(),
new Student(),
]),
new School('A', [
new Student(),
new Student(),
new Student(),
]),
);
};
标记:
<select data-bind="options: schools, value: selectedSchool, valueAllowUnset: false"></select>
<!-- ko if: students().length > 0 -->
<select data-bind="options: students, value: selectedStudent, valueAllowUnset: false"></select>
<!-- /ko -->