Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a 'mat-select' readonly?

I am working on a angular 5 project. There are many mat-select elements which is supposed to be readonly like text boxes. I found out that there is a disabled feature which is:

  <mat-form-field>
    <mat-select placeholder="Choose an option" [disabled]="disableSelect.value">
      <mat-option value="option1">Option 1</mat-option>
      <mat-option value="option2" disabled>Option 2 (disabled)</mat-option>
      <mat-option value="option3">Option 3</mat-option>
    </mat-select>
  </mat-form-field>

which out put looks like:

enter image description here

It fades out the text and the lining below get changes, is it possible to make it readonly?

like image 535
Atul Stha Avatar asked Jun 26 '18 06:06

Atul Stha


People also ask

How do I make my mat option disabled?

Like individual <mat-option> elements, an entire <mat-optgroup> can be disabled or enabled by setting the disabled property on the group.

How do I make mat-select default?

Just for information, you need to make sure the datatype of the default value matches with that of the options, for.eg, <mat-select [(value)]="heroes[0]. id"> <mat-option *ngFor="let hero of heroes" [value]="hero.id">{{ hero.name }}</mat-option> </mat-select> will work.

What is Mat-Select trigger?

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


3 Answers

Add CSS to both select block and mat-form-field block, these can be applied automatically to all the select elements:

<mat-form-field class="readonly-wrapper">
  <mat-select class="readonly-block" placeholder="Choose an option" [disabled]="disableSelect.value">
    <mat-option value="option1">Option 1</mat-option>
    <mat-option value="option2" disabled>Option 2 (disabled)</mat-option>
    <mat-option value="option3">Option 3</mat-option>
  </mat-select>
</mat-form-field>  

CSS code:

.readonly-wrapper {
    cursor: not-allowed;
}

.readonly-wrapper .readonly-block {
    pointer-events: none;
}
like image 55
Prachi Avatar answered Oct 18 '22 22:10

Prachi


You can combine an editable select with a readonly textbox and ngIf between them:

  <mat-form-field>
    <mat-label>Choose an option</mat-label>
    <input *ngIf="!editing" mat-input formControlName="mySelect" [readonly]="true">
    <mat-select *ngIf="editing" formControlName="mySelect">
      <mat-option value="option1">Option 1</mat-option>
      <mat-option value="option2" disabled>Option 2 (disabled)</mat-option>
      <mat-option value="option3">Option 3</mat-option>
    </mat-select>
  </mat-form-field>
like image 30
Markb Avatar answered Oct 18 '22 21:10

Markb


You can replace the mat-select form field with a input box and bind input box data with mat-select data.

like image 41
Shaishav Kumar Avatar answered Oct 18 '22 20:10

Shaishav Kumar