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

Support template content for selected value in md-select #2275

Closed
wouterbeukers opened this issue Dec 19, 2016 · 26 comments · Fixed by #3341
Closed

Support template content for selected value in md-select #2275

wouterbeukers opened this issue Dec 19, 2016 · 26 comments · Fixed by #3341
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix needs: discussion Further discussion with the team is needed before proceeding P4 A relatively minor issue that is not relevant to core functions

Comments

@wouterbeukers
Copy link

wouterbeukers commented Dec 19, 2016

Bug, feature request, or proposal:

bug

What is the expected behavior?

Selected option of a md-select supports html-tags

What is the current behavior?

Selected option of a md-select doesn't supports html-tags

What are the steps to reproduce?

See http://plnkr.co/edit/QJUCobw9yFcu1l1WtpOR?p=preview
Open the md-select.
In the dropdown the option supports html tags
Select the option
All html tags are stripped of the shown value in the md-select.

@dougmbarcellos
Copy link

Use this to display the formatted html:
<div [innerHtml]="selectedValue"></div>

@wouterbeukers
Copy link
Author

That isn't the issue.
It is about the value what the md-selects shows after you select an option.
This value is stripped of al it's html-tags.

@negberts
Copy link

The html is stripped here:

get viewValue(): string {
// TODO(kara): Add input property alternative for node envs.
return this._getHostElement().textContent.trim();
}

I would love to change it, but I don't know what the impact will be. There's probably a reason why textContent is taken and not just value

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

fxck commented Feb 28, 2017

@kara do you have something in mind regarding this? I would probably like to take a look at it.. also @crisbeto do you expect any problems with this because of multiselect?

@crisbeto
Copy link
Member

crisbeto commented Feb 28, 2017

I don't expect any issues with multi select since it'll use the same property to get the text. I was actually considering giving this a shot in the next few days.

crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 28, 2017
Adds the `md-select-label` directive which allows users to customize the selected value. E.g. it is now possible to do something like this, if the user wanted to reverse the selected label for some reason:

```ts
<md-select placeholder="Food" [formControl]="control" #select="mdSelect">
  <md-select-label>
    {{ select.selected?.viewValue.split('').reverse().join('') }}
  </md-select-label>
  <md-option *ngFor="let food of foods" [value]="food.value">
    {{ food.viewValue }}
  </md-option>
</md-select>
```

Fixes angular#2275.
@andreasrueedlinger
Copy link

@crisbeto thanks for your PR. So I guess you intended that with this change you can add the "plain HTML" option as label as follows:
<md-select-label [innerHtml]="select.selected?._getHostElement().innerHTML"></md-select-label>

@crisbeto
Copy link
Member

crisbeto commented Mar 7, 2017

This is the intended usage @andreasrueedlinger:

<md-select #select="mdSelect">
  <md-select-label>
    {{ select.selected?.viewValue }}
  </md-select-label>
  <!-- options go here -->
</md-select>

@andreasrueedlinger
Copy link

@crisbeto but that does not fix the problem adressed in this issue, namely that the label does not support Html!

@crisbeto
Copy link
Member

crisbeto commented Mar 7, 2017

You can put any HTML you want inside the md-select-label element.

@fxck
Copy link
Contributor

fxck commented Mar 7, 2017

That's not what he meant I think.

When you have..

<md-option>
  <img src="whatever.jpg" /> Whatever
</md-option>

..and you select it, the selected content won't be <img src="whatever.jpg" /> Whatever, it will be stripped down to Whatever. He wants the original unstripped HTML to be there instead. I'm not sure if {{ select.selected?.viewValue }} does that?

@andreasrueedlinger
Copy link

But in your example you use normal binding and viewValue which returns the trimmed textContent, so no Html anymore.
My example:

<md-select placeholder="Feedback" [formControl]="control" #select="mdSelect">
  <md-select-label [innerHtml]="select.selected?._getHostElement().innerHTML"></md-select-label>
  <md-option value="good">
    <md-icon>thumb_up</md-icon> Nice!
  </md-option>
  <md-option value="bad">
    <md-icon>thumb_down</md-icon> Bad!
  </md-option>
</md-select>

@crisbeto
Copy link
Member

crisbeto commented Mar 7, 2017

That's true, but if we just dumped the innerHTML instead, we'd be opening the user up to XSS, in addition to the potential performance implications.

@fxck
Copy link
Contributor

fxck commented Mar 7, 2017

There should be a straightforward way to do it nevertheless. It's pretty limiting when you know the values are safe.

@fxck
Copy link
Contributor

fxck commented Mar 7, 2017

Also the way it works currently is that if I have..

<md-option>
  <span class="in-a-circle-with-background-and-uppercase">un</span>User Name
</md-option>

..the selected value will be unUser Name, which is pretty annoying..

@crisbeto
Copy link
Member

crisbeto commented Mar 7, 2017

You can still use _getHostElement().innerHTML as @andreasrueedlinger mentioned, but there are better ways to do the same (e.g. you could map images or icons to values in your component).

@andreasrueedlinger
Copy link

XSS should not be a problem since Angular sanitizes the input to innerHtml unless you explicitely allow (https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizer-class.html)

@ermarkar
Copy link

ermarkar commented Mar 9, 2017

support for multiple selection ?

@fxck
Copy link
Contributor

fxck commented Mar 12, 2017

Good thing is that I think this could be used for #2722 (comment)

@Toub
Copy link

Toub commented Apr 24, 2017

Is there any workaround with 2.0.0-beta.3 ?

@fxck
Copy link
Contributor

fxck commented Apr 24, 2017

@Toub still waiting for #3341

@matte00
Copy link

matte00 commented May 15, 2017

Any news about this?

@jelbourn jelbourn removed the has pr label Jun 6, 2017
@jelbourn jelbourn changed the title md-select - Selected option of a md-select doesn't support html Support template content for selected value in md-select Jun 6, 2017
@jelbourn jelbourn added the P4 A relatively minor issue that is not relevant to core functions label Jun 6, 2017
@negberts
Copy link

An updated plunker for the problem can be found here: http://plnkr.co/edit/vuF3Pz7UHDXERXZbtiIK?p=preview

@mladilav
Copy link

mladilav commented Jul 6, 2017

Any news about this?

@sdargaud
Copy link

As a dirty workaround I am currently using somehting along those lines :

<md-select placeholder="Feedback" [formControl]="control" #select="mdSelect">
  <md-option value="good">
    <md-icon *ngIf="select.panelOpen">thumb_up</md-icon> Nice!</md-icon>
    <div *ngIf="!select.panelOpen">Nice!</div>
  </md-option>
</md-select>

At least I don't have "thumb_up Nice!" displayed and can work with it until it's fixed (be aware, it does seem to take some time to evaluate the template when opening the select, and the raw html is shown for a short period of time).

crisbeto added a commit to crisbeto/material2 that referenced this issue Aug 8, 2017
Adds the `md-select-trigger` directive which allows users to customize the selected value. E.g. it is now possible to do something like this, if the user wanted to reverse the selected label for some reason:

```ts
<md-select placeholder="Food" [formControl]="control" #select="mdSelect">
  <md-select-trigger>
    {{ select.selected?.viewValue.split('').reverse().join('') }}
  </md-select-trigger>
  <md-option *ngFor="let food of foods" [value]="food.value">
    {{ food.viewValue }}
  </md-option>
</md-select>
```

Fixes angular#2275.
mmalerba pushed a commit that referenced this issue Aug 8, 2017
Adds the `md-select-trigger` directive which allows users to customize the selected value. E.g. it is now possible to do something like this, if the user wanted to reverse the selected label for some reason:

```ts
<md-select placeholder="Food" [formControl]="control" #select="mdSelect">
  <md-select-trigger>
    {{ select.selected?.viewValue.split('').reverse().join('') }}
  </md-select-trigger>
  <md-option *ngFor="let food of foods" [value]="food.value">
    {{ food.viewValue }}
  </md-option>
</md-select>
```

Fixes #2275.
mmalerba pushed a commit that referenced this issue Aug 9, 2017
Adds the `md-select-trigger` directive which allows users to customize the selected value. E.g. it is now possible to do something like this, if the user wanted to reverse the selected label for some reason:

```ts
<md-select placeholder="Food" [formControl]="control" #select="mdSelect">
  <md-select-trigger>
    {{ select.selected?.viewValue.split('').reverse().join('') }}
  </md-select-trigger>
  <md-option *ngFor="let food of foods" [value]="food.value">
    {{ food.viewValue }}
  </md-option>
</md-select>
```

Fixes #2275.
@LayZeeDK
Copy link
Contributor

For future reference, you can use the <mat-select-trigger> to change the display of the selected value.

It was added by @crisbeto in Angular Material version 2 beta 10.

@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 10, 2019
@mmalerba mmalerba added the needs: discussion Further discussion with the team is needed before proceeding label Mar 3, 2020
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 needs: discussion Further discussion with the team is needed before proceeding P4 A relatively minor issue that is not relevant to core functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.