angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Support disabling options in select with ng:options

Open esprehn opened this issue 15 years ago • 96 comments

Need a way to construct

<select>
  <option disabled>A</option>
  <option>B</option>
  <optionC</option>
</select>

There's no way to express the disabled state of a specific option with ng:options

esprehn avatar Nov 05 '11 22:11 esprehn

+1

georgesebastian avatar Sep 23 '14 06:09 georgesebastian

+1, the syntax could be

select as label with options for value in array

With options being an hash like: {disabled: product.available}

See actual documentation.

this syntax make sence

loddit avatar Sep 24 '14 02:09 loddit

Hello everybody, I'm working on a PR to make ng-options support disable. So far we have two syntax proposals:

select as label with options for value in array

by @Dorian, and

select as label disable if cond group by group for value in array

by @cades.

Which one do you prefer and why, or there is another syntax proposal?

cades avatar Sep 24 '14 09:09 cades

I found a problem in syntax

select as label with options for value in array

If there's code like this:

<select ng-options="product.id as product.name with options for product in products"></select>
function Ctrl($scope){
  $scope.options = {
    disabled: product.available
  };
  $scope.products = [
    {id:1, name: 'P1', available: true},
    {id:2, name: 'P2', available: false}
  ];
}

$scope.options cannot be an hash like: {disabled: product.available}, because product does not live in the scope of where $scope.options is declared. However, it can be a string like: 'product.available', or a callback like

$scope.options = {
  disabled: function(product){
    return product.available;
  }
};

cades avatar Sep 24 '14 09:09 cades

@cades awesome!

I think select as label disable if cond group by group for value in array is the way to go as it's more obvious from the statement what is happening. No having to jump through other files just to see why something is being disabled, you can see it right there

aronduby avatar Sep 24 '14 14:09 aronduby

Have you thought about using an additional attribute, like ng-options-disabled?

aronduby avatar Sep 24 '14 15:09 aronduby

@aronduby @vikasgulati has provided one.

cades avatar Sep 24 '14 15:09 cades

@cades yes, but it doesn't work with group-by

aronduby avatar Sep 24 '14 15:09 aronduby

@aronduby Yes it doesn't :( An separate directive need modification to ng-options, too.

cades avatar Sep 24 '14 15:09 cades

@cades I vote for the select as label disable if cond group by group for value in array option, as I cannot personally think of anything else you would need to do to an option that isn't already covered by existing functionality. Conditional disabling is the last major functionality gap for ng-options, and I am excited by the idea of your PR, especially if it got accepted into core.

greglockwood avatar Oct 09 '14 01:10 greglockwood

Now the PR is ready (disable if version), however I found another issue: what if the default option (initial value set on $scope, which binds to select's ng-model) is disabled? Should we prevent user from doing this, or respect user's intention?

The possible behavior may be:

  1. select it (as user wish)
  2. use null option, and (optionally) print warning message to console
  3. throw an exception, forcing user to resolve it

cades avatar Oct 29 '14 06:10 cades

@cades i think it should be the same behavior as plain HTML: select it ( http://jsfiddle.net/006vyh0b/ )

mathroc avatar Oct 29 '14 07:10 mathroc

@mathroc++ And this is the default behavior (without extra work)

cades avatar Oct 29 '14 09:10 cades

great job!

Mickey- avatar Nov 07 '14 08:11 Mickey-

+1

runcom avatar Dec 02 '14 16:12 runcom

+1

Currently using @vikasgulati solution, but notice that it doesn't support dynamic options. There needs to be a watch on the first argument so that if that changes, the disabled attribute will be updated.

chadreed-intl avatar Dec 05 '14 23:12 chadreed-intl

+1

ankit-saxena avatar Dec 30 '14 07:12 ankit-saxena

:+1: any good news on this ?

lekhnath avatar Dec 30 '14 09:12 lekhnath

+1

lebster avatar Jan 07 '15 21:01 lebster

+1

gcardosostep avatar Jan 08 '15 21:01 gcardosostep