简体   繁体   English

选择元素无法使用淘汰赛正确更新

[英]Select element not updating correctly with Knockout

Background 背景

I have a situation where I want to have a few dropdown menus which change options based on what is available. 我遇到一种情况,我希望有一些下拉菜单,这些菜单会根据可用选项来更改选项。 I've managed to simplify this code and replicate the problem with the code below. 我设法简化了这段代码,并使用下面的代码复制了该问题。

In this example I have 5 available colors and I want to choose four of them. 在此示例中,我有5种可用颜色,我想选择其中四种。 If I select one, then I want it to not be available in the other menus. 如果我选择一个,那么我希望它在其他菜单中不可用。

Problem 问题

The dropdown menus only sort of work. 下拉菜单仅能正常工作。 The options that are shown do seem to be valid based on what's available, however sometimes when selecting an entry it will not allow it until I choose a second time. 根据可用的内容,显示的选项似乎确实有效,但是有时在选择一个条目时,直到我第二次选择时,它才允许它。 Also, as seen in commented code below, a lodash _.sortBy seems to break functionality altogether. 另外,如下面的注释代码所示,破折号_.sortBy似乎完全破坏了功能。

HTML HTML

<div data-bind="foreach:colorChoices">
  <select data-bind="options: localAvailableOptions, optionsCaption: 'Select...', optionsText: function(currentValue) {return 'Color ID ' + currentValue;}, value: id"></select>
</div>

Javascript 使用Javascript

function appModel() {
    var self = this;
    self.colorIds = [1, 2, 3, 4, 5];
    self.colorChoices = ko.observableArray();

    self.selectedColorIds = ko.computed(function() {
        return _(self.colorChoices())
            .filter(function(item) {
                return item.id()
            })
            .map(function(item) {
                return item.id()
            })
            .value();
    });

    self.availableColorIds = ko.computed(function() {
        return _.difference(self.colorIds, self.selectedColorIds());
    });

    self.colorChoices.push(new colorChoice(self));
    self.colorChoices.push(new colorChoice(self));
    self.colorChoices.push(new colorChoice(self));
    self.colorChoices.push(new colorChoice(self));
}

function colorChoice(parent) {
    var self = this;
    self.id = ko.observable();
    self.localAvailableOptions = ko.computed(function() {
        //clone as to not modify original array
        var availableIds = _.clone(parent.availableColorIds());
        //add own ID so dropdown menu contains matching entry
        if (self.id()) {
            availableIds.push(self.id());
        }
        //seems to break with _.sortBy
        //return _.sortBy(availableIds);
        return availableIds;
    });
}

ko.applyBindings(new appModel());

CodePen (same code) CodePen(相同代码)

https://codepen.io/anon/pen/KEKPKV https://codepen.io/anon/pen/KEKPKV

I found the issue. 我发现了问题。

if (self.id()) {
    availableIds.push(self.id());
}

This was missing a check to see if it already existed, and meant that the available options included duplicate values, which was presumably producing the undefined behavior. 缺少检查以查看是否已经存在的检查,这意味着可用选项包括重复的值,这大概会产生未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM