我有两个下拉列表,一个取决于其他(级联)。'MainCategories' 上的选择决定了 'SubCategories' 的项目。此外,“MainCategories”上的选择也决定了表格行的可见性。以下是我尝试过的:
<table>
<tbody data-bind="foreach: contacts">
<tr>
<td>MainCategories:
<select data-bind='options: MyCategories, value: mycategory, optionsText: "Type", optionsValue: "Type",optionsCaption: "Select..."'></select>
</td>
<td>
<!-- ko with: mycategory -->SubCategories:
<select data-bind='options: Categories, optionsText: "Category", optionsCaption: "Select...", value: $parent.product'></select>
<!-- /ko -->
</td>
</tr>
<tr data-bind="visible:mycategory()=='Type1'">
<td>I am visible</td>
</tr>
</tbody>
</table>
<script type = "text/javascript" >
var MyCategories = [{
"Categories": [{
"Category": "Category1"
}, {
"Category": "Category2"
}],
"Type": "Type1"
}, {
"Categories": [{
"Category": "Category3"
}, {
"Category": "Category4"
}],
"Type": "Type2"
}];
var initialData = [{
category: "Mobile",
product: "Nokia"
}];
var ContactsModel = function (contacts) {
var self = this;
self.contacts = ko.observableArray(ko.utils.arrayMap(contacts, function (contact) {
return {
mycategory: ko.observable(contact.category),
product: ko.observable(contact.product)
};
}));
};
ko.applyBindings(new ContactsModel(initialData));
</script>
如果我删除optionsValue: "Type",那么 'SubCategories' 会得到正确的项目。但是表格行的可见性没有按预期工作。如果我有optionsValue: "Type",则不会填充“子类别”。而且,当我将“MainCategories”的选项更改 1 或 2 次时,只有可见性可以正常工作。
请帮我找出我在这里做错了什么。谢谢,