如何刷新 ko multi select 组件中的 'options' 绑定?
How to refresh the 'options' binding in an ko multi select component?
我有一个 multi select 组件,我用 'options' 绑定绑定,'options' 根据我 select 在另一个 multi select组件。
下面是第一个 multi select component
<select data-bind="multiple: true, required: true, options:repositoriesForSelect, value: selectedRepository"></select>
根据此组件中 select 的值,正在刷新第二个组件的选项
<select data-bind=" multiple: true,required: true,options: branchesForSelect,value: selectedBranch"></select>
使用计算变量刷新第二个选项:
branchesForSelect = ko.computed(function(){
//selectedRepository is an observable array here
//some logic
});
效果很好,但除上述内容外,我还想根据同一组件中编辑的值 select 刷新 'branchesForSelect'。意思是,如果 'branchesForSelect' 包含值 'A'、'B'、'C',那么在 'A' 的 select 上,我想刷新 'branchesForSelect' 在选项列表中仅显示 'C'。
有人可以指导我吗?如果问题不清楚,请在评论中告诉我。
谢谢
通过计算第二个选项列表,您走在了正确的轨道上。这是您仍然需要做的:在计算内部,使用 selectedRepository
的值到 return 一组选项,这些选项 linked 到 selection。通过使用此值,knockout 将确保在第一个 select 的 value
变量更改后,重新评估第二个选项列表。
在评论中澄清问题后:
在用户输入后更改 select 本身的值从用户体验的角度来看是个坏主意(如果你问我),但肯定可以做到。下面的代码将向您展示如何操作。当 multi-select 的第一个选项处于活动状态时,其他选项将被隐藏。
这是一个例子:
var repositoryBranches = {
a: ["All", 1, 2, 3, 4, 5, 6],
b: ["All", 0, 7, 8],
c: ["All", 9, 10]
};
var VM = function() {
var self = this;
this.repositoryKeys = ["a", "b", "c"];
this.selectedRepository = ko.observable("a");
this.selectedBranches = ko.observableArray([]);
this.branchesForSelectedRepository = ko.computed(function() {
var allBranchesForRepo = repositoryBranches[self.selectedRepository()];
// We're making an exception if the first one is selected:
// don't show any other options if the selected one is the first one
if (self.selectedBranches()[0] === allBranchesForRepo[0]) {
return ["All"];
}
return allBranchesForRepo;
});
// Clear selection when changing repository
this.selectedRepository.subscribe(function clearSelection() {
self.selectedBranches([]);
});
};
ko.applyBindings(new VM());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<select data-bind="options:repositoryKeys, value: selectedRepository"> </select>
<select multiple="true" data-bind="options:branchesForSelectedRepository, selectedOptions: selectedBranches"></select>
(fiddle)
如果您需要帮助找到从 link "branches" 到 "repositories" 的好方法,请告诉我。理想情况下,"repositories" 有自己的模型,其中包含一个 "branches" 数组。然后你就可以定义你的计算数组,比如 return self.firstSelection().branches;
我有一个 multi select 组件,我用 'options' 绑定绑定,'options' 根据我 select 在另一个 multi select组件。 下面是第一个 multi select component
<select data-bind="multiple: true, required: true, options:repositoriesForSelect, value: selectedRepository"></select>
根据此组件中 select 的值,正在刷新第二个组件的选项
<select data-bind=" multiple: true,required: true,options: branchesForSelect,value: selectedBranch"></select>
使用计算变量刷新第二个选项:
branchesForSelect = ko.computed(function(){
//selectedRepository is an observable array here
//some logic
});
效果很好,但除上述内容外,我还想根据同一组件中编辑的值 select 刷新 'branchesForSelect'。意思是,如果 'branchesForSelect' 包含值 'A'、'B'、'C',那么在 'A' 的 select 上,我想刷新 'branchesForSelect' 在选项列表中仅显示 'C'。
有人可以指导我吗?如果问题不清楚,请在评论中告诉我。
谢谢
通过计算第二个选项列表,您走在了正确的轨道上。这是您仍然需要做的:在计算内部,使用 selectedRepository
的值到 return 一组选项,这些选项 linked 到 selection。通过使用此值,knockout 将确保在第一个 select 的 value
变量更改后,重新评估第二个选项列表。
在评论中澄清问题后:
在用户输入后更改 select 本身的值从用户体验的角度来看是个坏主意(如果你问我),但肯定可以做到。下面的代码将向您展示如何操作。当 multi-select 的第一个选项处于活动状态时,其他选项将被隐藏。
这是一个例子:
var repositoryBranches = {
a: ["All", 1, 2, 3, 4, 5, 6],
b: ["All", 0, 7, 8],
c: ["All", 9, 10]
};
var VM = function() {
var self = this;
this.repositoryKeys = ["a", "b", "c"];
this.selectedRepository = ko.observable("a");
this.selectedBranches = ko.observableArray([]);
this.branchesForSelectedRepository = ko.computed(function() {
var allBranchesForRepo = repositoryBranches[self.selectedRepository()];
// We're making an exception if the first one is selected:
// don't show any other options if the selected one is the first one
if (self.selectedBranches()[0] === allBranchesForRepo[0]) {
return ["All"];
}
return allBranchesForRepo;
});
// Clear selection when changing repository
this.selectedRepository.subscribe(function clearSelection() {
self.selectedBranches([]);
});
};
ko.applyBindings(new VM());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<select data-bind="options:repositoryKeys, value: selectedRepository"> </select>
<select multiple="true" data-bind="options:branchesForSelectedRepository, selectedOptions: selectedBranches"></select>
(fiddle)
如果您需要帮助找到从 link "branches" 到 "repositories" 的好方法,请告诉我。理想情况下,"repositories" 有自己的模型,其中包含一个 "branches" 数组。然后你就可以定义你的计算数组,比如 return self.firstSelection().branches;