我使用受此问题中已接受答案启发的代码(jsFiddle - 帖子底部的代码),使用 Knockout 创建了多个级联选择。
它工作得很好,但是我想在子选择不可用时禁用它们,而不是完全隐藏它们。
有一个简单的选择吗?
隐藏行为似乎是由 'with' 绑定驱动的,但这似乎是数组链接方式不可或缺的一部分。
var mainViewModel = null;
var mainViewModelData = {
"RequestName": "test Name",
"BusinessLines": [{
"Id": 1,
"Title": "Retail",
"Clusters": [{
"Id": 1,
"Title": "Corporate si Trezorerie"},
{
"Id": 2,
"Title": "Good Bee"},
{
"Id": 3,
"Title": "Leasing"}]},
{
"Id": 2,
"Title": "Corporate",
"Clusters": [{
"Id": 1,
"Title": "REM"},
{
"Id": 2,
"Title": "BCR Procesare"},
{
"Id": 3,
"Title": "Erste Asset Management"}]}],
"SelectedBusinessLine": null,
"SelectedCluster": null,
"RequirementHighLevelDescription": null,
"Revenues": 0,
"Savings": 0,
"PreventedLosses": 0
};
mainViewModel = ko.mapping.fromJS(mainViewModelData);
ko.applyBindings(mainViewModel);
<div>
<table>
<tr>
<td>
Business Line
</td>
<td>
<select data-bind="options: BusinessLines, optionsText: 'Title', value: SelectedBusinessLine,
optionsCaption: 'Select Business Line..'">
</select>
</td>
</tr>
<tr data-bind="with: SelectedBusinessLine">
<td>
Cluster
</td>
<td> <select data-bind="options: Clusters, optionsText: 'Title', value: $root.SelectedCluster, optionsCaption: 'Select Cluster..'">
</select>
</td>
</tr>
</table>
</div>