Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added new options parameter 'optionsDisabled' #398

Closed
wants to merge 1 commit into from

2 participants

@maverix

Allows option binding to include unselectable options (disabled)

Unfortunately this is not supported by IE 8 and below.

  • UPDATE - This is supported by IE 8
Andrew Harry Added new select option 'optionDisabled'
This is not supported for IE 8 and below
c81465d
@maverix

Opps I didn't see the virtual duplicate pull request #108

I wrote a test case at least :)

@mbest
Collaborator

Closing in favor of #382.

@mbest mbest closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2012
  1. Added new select option 'optionDisabled'

    Andrew Harry authored
    This is not supported for IE 8 and below
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 2 deletions.
  1. +24 −0 spec/defaultBindingsBehaviors.js
  2. +9 −2 src/binding/defaultBindings.js
View
24 spec/defaultBindingsBehaviors.js
@@ -453,6 +453,30 @@ describe('Binding: Options', {
ko.applyBindings({ myValues: modelValues }, testNode);
var displayedText = ko.utils.arrayMap(testNode.childNodes[0].childNodes, function (node) { return node.innerText || node.textContent; });
value_of(displayedText).should_be(["bob (manager)", "frank (coder & tester)"]);
+ },
+
+ 'Should accept optionsDisabled param to set the disabled subproperty of the model values': function () {
+ var modelValues = new ko.observableArray([
+ { name: 'bob', id: ko.observable(6), disabled: ko.observable(true) },
+ { name: ko.observable('frank'), id: 13, disabled: true },
+ { name: ko.observable('jill'), id: 24, disabled: false }
+ ]);
+ testNode.innerHTML = "<select data-bind='options:myValues, optionsText: \"name\", optionsValue: \"id\", optionsDisabled: \"disabled\"'><option>should be deleted</option></select>";
+ ko.applyBindings({ myValues: modelValues }, testNode);
+ var disabledValues = ko.utils.arrayMap(testNode.childNodes[0].childNodes,
+ function (node) {
+ var _disabled = node.getAttribute('disabled');
+ if (_disabled === 'disabled')
+ return node.value;
+ });
+ var enabledValues = ko.utils.arrayMap(testNode.childNodes[0].childNodes,
+ function (node) {
+ var _disabled = node.getAttribute('disabled');
+ if (_disabled === undefined || _disabled === null)
+ return node.value;
+ });
+ value_of(disabledValues).should_be([6, 13, undefined]);
+ value_of(enabledValues).should_be([undefined, undefined, 24]);
},
'Should update the SELECT node\'s options if the model changes': function () {
View
11 src/binding/defaultBindings.js
@@ -230,8 +230,15 @@ ko.bindingHandlers['options'] = {
// Apply a value to the option element
var optionValue = typeof allBindings['optionsValue'] == "string" ? value[i][allBindings['optionsValue']] : value[i];
optionValue = ko.utils.unwrapObservable(optionValue);
- ko.selectExtensions.writeValue(option, optionValue);
-
+ ko.selectExtensions.writeValue(option, optionValue);
+
+ if (allBindings['optionsDisabled']) {
+ var disabledValue = typeof allBindings['optionsDisabled'] == "string" ? value[i][allBindings['optionsDisabled']] : allBindings['optionsDisabled'];
+ disabledValue = ko.utils.unwrapObservable(disabledValue);
+ if (disabledValue)
+ option.setAttribute('disabled', 'disabled');
+ }
+
// Apply some text to the option element
var optionsTextValue = allBindings['optionsText'];
var optionText;
Something went wrong with that request. Please try again.