Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(md-select) - enable to clear the value (unselect) #3110

Closed
jefersonestevo opened this issue Feb 15, 2017 · 5 comments · Fixed by #3141
Closed

feat(md-select) - enable to clear the value (unselect) #3110

jefersonestevo opened this issue Feb 15, 2017 · 5 comments · Fixed by #3141
Assignees
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix

Comments

@jefersonestevo
Copy link
Contributor

Bug, feature request, or proposal:

feat

What is the expected behavior?

In a md-select, let the user create a option to "unselect" the item value.

What is the current behavior?

When we add a empty option with value="" (ng-value="undefined" or ng-value="null"), the placeholder act as if we've selected an option

What are the steps to reproduce?

After selecting one option on md-select, try to "unselect" and revert it to the "empty" state

Providing a Plunker (or similar) is the best way to get the team to see your issue.
Plunker template: http://plnkr.co/edit/o077B6uEiiIgkC0S06dd
http://plnkr.co/edit/e8Qoaq7M1ubiWHpVKctu?p=preview

What is the use-case or motivation for changing an existing behavior?

Some non-required select on my applications need to let the user "unselect" the options

Which versions of Angular, Material, OS, browsers are affected?

@angular/material: 2.0.0-beta.1

Is there anything else we should know?

@kara kara added the feature This issue represents a new feature or feature request rather than a bug or bug fix label Feb 15, 2017
@kara
Copy link
Contributor

kara commented Feb 15, 2017

We do want to add easy de-select functionality; it's on the list! In the meantime, you can get it to work with form reset:

<md-select placeholder="My Option" [(ngModel)]="opt2" name="opt2" #model="ngModel">
      <md-option (click)="model.reset()"></md-option>
      <md-option value="item1">Item 1</md-option>
      <md-option value="item2">Item 2</md-option>
</md-select>

I've updated your plunker with an example.

@jefersonestevo
Copy link
Contributor Author

@kara thanks for the workaround. It worked like a charm. The only problem with this approach is that, when I have an undefined field in my ts file (not null), it assumes that the "empty" option is the one selected and the page is opened as if I had already selected an option.

I've updated my plnkr with the above description.

But for now, I can work it out by setting an out of domain value to the "empty" option. Thanks

@crisbeto crisbeto assigned crisbeto and unassigned crisbeto Feb 16, 2017
crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 16, 2017
…tion

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes angular#3110.
Fixes angular#2634.
crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 24, 2017
…tion

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes angular#3110.
Fixes angular#2634.
crisbeto added a commit to crisbeto/material2 that referenced this issue May 9, 2017
…tion

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes angular#3110.
Fixes angular#2634.
crisbeto added a commit to crisbeto/material2 that referenced this issue May 22, 2017
…tion

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes angular#3110.
Fixes angular#2634.
tinayuangao pushed a commit that referenced this issue May 23, 2017
…tion (#3141)

* fix(select): allow option with undefined or null value to clear selection

Allows for options, with a value of `null` or `undefined`, to clear the select. This is similar to the way the native select works.

Fixes #3110.
Fixes #2634.

* fix: address feedback
@niveo
Copy link

niveo commented Jun 8, 2017

Compare two objects don't work.

food = {value: 'steak-0', viewValue: 'Steak'};

foods = [
    {value: null, viewValue: 'None'},
    {value: 'steak-0', viewValue: 'Steak'},
    {value: 'pizza-1', viewValue: 'Pizza'},
    {value: 'tacos-2', viewValue: 'Tacos'}
  ];

        {{food}}
        <md-select placeholder="Food" [(ngModel)]="food">
            <md-option *ngFor="let item of foods" [value]="item">
                {{food===item}}
            </md-option>
        </md-select>

@casni14
Copy link

casni14 commented Dec 22, 2017

I found that you can use (change)="model.reset" then you avoid creating an empty option field. Like this:

<md-select placeholder="My Option" [(ngModel)]="opt2" name="opt2" #model="ngModel" (change)="model.reset()">
      <md-option value="item1">Item 1</md-option>
      <md-option value="item2">Item 2</md-option>
</md-select>

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants