简体   繁体   English

敲除选择下拉禁用项目

[英]Knockout select dropdown disable item

Currently I'm only able to enable/disable the entire drop down using the enable binding in knockout. 目前我只能使用敲除中的启用绑定启用/禁用整个下拉列表。 When Enable = false, the whole drop down is no longer clickable, and user cannot see the other possible values in the drop down. 当Enable = false时,整个下拉列表不再可点击,用户无法在下拉列表中看到其他可能的值。

<select data-bind="options: OptionsList, optionsText: 'Key', optionsValue: 'Value', value: FieldValue, enable: Enable"></select>

what got rendered is this: 得到的是这样的:

<select disabled=""></select>

What I'm hoping to do is render something like this 我希望做的是渲染这样的东西

<select> 
<option disabled="disabled" value='1'>One </option> 
<option selected="select"   value='2'>Two </option> 
<option disabled="disabled" value='3'>Three </option>   
</select>

This way I can still see my options but they are all disabled so the user can't change them. 这样我仍然可以看到我的选项,但它们都被禁用,因此用户无法更改它们。

I have looked at the optionsAfterRender in knockout, but I no longer have access to the selected value. 我在淘汰赛中查看了optionsAfterRender,但我无法再访问所选值。 the item passed in is just the key and value of the select item, not the observable. 传入的项只是选择项的键和值,而不是可观察项。

Any help would be much appreciated. 任何帮助将非常感激。 Thanks 谢谢

You could choose to build your option elements using foreach like: 您可以选择使用foreach构建option元素,如:

<select data-bind="value: selected, foreach: options">
    <option data-bind="attr: { disabled: !enabled(), value: value }, text: name"></option>
</select>

Sample: http://jsfiddle.net/rniemeyer/4PuxQ/ 示例: http//jsfiddle.net/rniemeyer/4PuxQ/

Otherwise, here is a sample of an optionsBind binding from Michael Best that would let you bind the option without using a foreach (uses optionsAfterRender functionality): 否则,这里是Michael Best的optionsBind绑定示例,它允许您在不使用foreach的情况下绑定选项(使用optionsAfterRender功能):

<select data-bind="value: selected, options: options, optionsText: 'name', optionsValue: 'value', optionsBind: 'attr: { disabled: !enabled() }`"></select>'

Sample: http://jsfiddle.net/rniemeyer/KxY44/ 示例: http//jsfiddle.net/rniemeyer/KxY44/

You need to use the optionsAfterRender to apply disabled to the options. 您需要使用optionsAfterRender将禁用应用于选项。 It's discussed in the documentation: http://knockoutjs.com/documentation/options-binding.html 它在文档中讨论: http//knockoutjs.com/documentation/options-binding.html

<select data-bind="options: OptionsList, optionsText: 'Key', optionsValue: 'Value', value: FieldValue, optionsAfterRender: setOptionDisable"></select>

self.setOptionDisable = function(option, item) {
    ko.applyBindingsToNode(option, {disable: item.disable}, item);
}

Here is a demo of it working: http://jsfiddle.net/vkFUC/ 以下是它的工作演示: http//jsfiddle.net/vkFUC/

You can use optionsAfterRender for disabling options. 您可以使用optionsAfterRender来禁用选项。 You need to provide one more observable property to object for enabling or disabling the option. 您需要为object提供一个可观察的属性,以启用或禁用该选项。

Code:- 码:-

  //Optionlist will look like this 
  OptionsList: ko.observableArray([{
    Key: 1,
    Value: "Jack",
    disable: ko.observable(false)
  }, {
    Key: 2,
    Value: "Jhon",
    disable: ko.observable(false)
   }]),

    //Function will be used in optionsAfterRender 
    setOptionDisable: function (option, item) {
      ko.applyBindingsToNode(option, {
        disable: item.disable
      }, item);
    }

Fiddle Demo 小提琴演示

For version 3.5.0 , if you want to continue to use mbest solution (which I personally find very useful and elegant), just replace the ko.contextFor with the call to this proposed workaround from chrisknoll: 对于3.5.0版,如果你想继续使用mbest解决方案(我个人觉得非常有用和优雅),只需用ko.contextFor调用这个建议的解决方法来替换ko.contextFor:

function contextForNodeOrNearestParent(node) {
    // Proposed fix for 3.5.0 ko.contextFor   https://github.com/knockout/knockout/pull/2447/files
    var context = ko.contextFor(node);
    if (!context && node.parentNode) {
        return contextForNodeOrNearestParent(node.parentNode);
    }
    return context;
}

The original custom bindingHandler from mbest would be then modified like this: 然后将修改mbest的原始自定义bindingHandler,如下所示:

ko.bindingHandlers.optionsBind = {
    preprocess: function (value, key, addBinding) {
        addBinding('optionsAfterRender', 'function(option, item) { ko.bindingHandlers.optionsBind.applyBindings(option, item, ' + value + ') }');
    },
    applyBindings: function (option, item, bindings) {
        if (item !== undefined) {
            option.setAttribute('data-bind', bindings);
            //ko.applyBindings(ko.contextFor(option).createChildContext(item), option);
            ko.applyBindings(contextForNodeOrNearestParent(option).createChildContext(item), option);
        }
    }
};

This is working for my project like a charm. 这对我的项目很有吸引力。 And I'm posting this here, in case it can save someone's time, and for future personal reference. 我在这里张贴这个,以防它可以节省一些人的时间,以及将来的个人参考。

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

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