Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not enough space in <mat-select>. How to add multiple line <mat-option>?

I want to have under the option text additional lines which describe the option. By default mat-select limits number of characters and add "..." at the end of the line. I want to have multiple line option of needed. stakckblitz demo: https://stackblitz.com/edit/angular-wj9svw My code:

export class SelectOverviewExample {
  foods: Food[] = [
    {value: 'steak-0', viewValue: 'Steak longDescription longDescription longDescription longDescription longDescription'},
    {value: 'pizza-1', viewValue: 'Pizza longDescription longDescription longDescription longDescription longDescription longDescription v v longDescription'},
    {value: 'tacos-2', viewValue: 'Tacos'}
  ];
}

html:

<mat-form-field>
  <mat-select placeholder="Favorite food">
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{food.viewValue}}
      <b> some additional text text text text texttext </b>
    </mat-option>
  </mat-select>
</mat-form-field>
like image 322
Paweł Meller Avatar asked Oct 16 '18 10:10

Paweł Meller


People also ask

What is Mat Optgroup?

The <mat-optgroup> element can be used to group common options under a subheading. The name of the group can be set using the label property of <mat-optgroup> . Like individual <mat-option> elements, an entire <mat-optgroup> can be disabled or enabled by setting the disabled property on the group.

How do you readonly mat select?

Create a project. Add Angular material to the project. Create a form with fieldset in it and mat-select in it (form -> fieldset -> mat-select) Add [disabled] to fieldset inside form tag to be true on submit.

What is Mat Select trigger?

MatSelectTrigger. Allows the user to customize the trigger that is displayed when the select has a value.


2 Answers

Use following css :

    .mat-select-panel mat-option.mat-option {
      height: unset;
    }

   .mat-option-text.mat-option-text {
      white-space: normal;
    }

OR

/deep/ .mat-select-panel mat-option.mat-option {
  height: unset;
}

/deep/ .mat-option-text.mat-option-text {
  white-space: normal;
}

Sample demo is here - https://stackblitz.com/edit/angular-material2-issue-ndtstg

like image 74
Sunil Singh Avatar answered Sep 21 '22 14:09

Sunil Singh


.mat-option {
  margin: 1rem 0;
  overflow: visible;
  line-height: initial;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.mat-option i {
  display: block;
  font-size: 1.5rem;
  opacity: 0.6;
  margin-left: 0.5rem;

}

This was also helpful

like image 37
Paweł Meller Avatar answered Sep 22 '22 14:09

Paweł Meller