挖空和选择器的数组过滤问题
An array filtering issue with knockout and selectors
我有一个文本数组,我显示在<select>
中。
文本可能有不同的版本号,我想仅根据最新版本过滤<select>
。
我想有更优雅的方法可以做到这一点(欢迎建议(,但我选择使用 2 <select>
s 设置为根据复选框的替代可见性。
代码是一个黑客,但结果看起来相当不错。不幸的是,有一个错误。
我有两个可观察量指示它们各自数组中的选定选项:
self.SelectedText = ko.observable();
self.SelectedUnique = ko.observable();
两者都有订阅,但我无法在两个订阅中将它们链接在一起,所以我选择了一个独立在另一个上独立,如下所示:
self.SelectedUnique.subscribe(function (text) {
if (text) {
self.SelectedText(text);
}
});
但是,不同步。
场景 1:选择文本 1、2、3。[确定]场景 2:选择文本 2;选中"仅限最新版本"
这会导致不显示任何选项("选择..."(。不是我想要的。
情况变得更糟。
场景 3:取消选中; 选择文本 3;然后再次选中"仅最新版本"。
现在,选择的选项设置为选择未过滤的选项2。
可能有一个简单的问题。我只是无法让它工作。这是小提琴: 小提琴: http://jsfiddle.net/h5mt51gv/6/
感谢所有的帮助和建议!
我已经简化了你的方法:
<select>
绑定到计算的选项列表 (visibleTextBatches
(- 此计算列表取决于复选框的状态 (
latestOnly
(,有效地在完整列表和过滤列表之间切换 - 过滤列表(
latestTextBatches
(是另一个计算,它保存每个组的最新版本 <select>
将实际选定的TextBatch
对象存储在可观察量(selectedTextBatch
(- 有一个对
visibleTextBatches
的订阅,当筛选列表时,该订阅会导致最新的可选TextBatch
成为当前。当列表未筛选时,它不执行任何操作。
function TextBatch(data) {
this.textbatchId = data.textbatchId;
this.parentId = data.parentId;
this.version = data.version;
this.title = ko.observable(data.title);
}
function ViewModel() {
var self = this;
// read up on the mapping plugin, too
self.textBatches = ko.observableArray([
new TextBatch({textbatchId: 1, parentId: 1, version: 1, title: "TB1.1"}),
new TextBatch({textbatchId: 2, parentId: 1, version: 2, title: "TB1.2"}),
new TextBatch({textbatchId: 3, parentId: 3, version: 1, title: "TB2.1"})
]);
self.selectedTextBatch = ko.observable();
self.latestOnly = ko.observable(false);
self.latestTextBatchGroups = ko.computed(function () {
var latest = {};
ko.utils.arrayForEach(self.textBatches(), function (batch) {
if (!latest.hasOwnProperty(batch.parentId) ||
batch.version > latest[batch.parentId].version
) latest[batch.parentId] = batch;
});
return latest;
});
self.latestTextBatches = ko.computed(function () {
return ko.utils.arrayFilter(self.textBatches(), function (batch) {
return batch === self.latestTextBatchGroups()[batch.parentId];
});
});
self.visibleTextBatches = ko.computed(function () {
return self.latestOnly() ? self.latestTextBatches() : self.textBatches();
});
self.visibleTextBatches.subscribe(function () {
var selectedBatch = self.selectedTextBatch();
if (selectedBatch && self.latestOnly()) {
self.selectedTextBatch(
self.latestTextBatchGroups()[selectedBatch.parentId]
);
}
});
}
ko.applyBindings(new ViewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div>
<select data-bind="
options: visibleTextBatches,
optionsText: 'title',
optionsCaption: 'Select...',
value: selectedTextBatch
" />
</div>
<div>
<input type="checkbox" id="chkLatestOnly" data-bind="checked: latestOnly" />
<label for="chkLatestOnly">Latest only</label>
</div>
<hr />
<pre data-bind="text: ko.toJSON($root, null,2)"></pre>
相关文章:
- 使用一个键的值数组过滤多个javascript对象
- 根据另一个包含角度 js 中对象的数组过滤包含对象的数组
- Javascript:按字符串数组过滤对象数组
- 在 javascript 中使用数组过滤 for AngularJS
- 挖空和选择器的数组过滤问题
- Lodash 按数组的属性数组过滤
- 如何用另一个字符串数组过滤ng重复中的字符串数组
- 如何根据javascript数组过滤rss数据
- 基于NodeJS中的其他数组过滤/搜索对象的JavaScript数组
- 使用不带嵌套循环的数组过滤对象数组js
- 使用数组过滤数组Javascript
- 用一维数组过滤多维数组
- AngularJS:如何给值数组过滤
- Angularjs,逐个数组过滤
- 用另一个对象数组过滤Javascript对象数组
- 创建搜索栏以将数组过滤到表中
- 通过id数组过滤角度数据
- coffeescript -数组过滤不工作
- Backbone.js:如何通过模型ID数组过滤对象集合
- 使用基于嵌套值的数组过滤对象数组