Show extra values in mat option when panel is open - angular-material

<mat-select >
<mat-option *ngFor="let i of List" [value]="i.value">
{{i.viewValue}}<span *ngIf="panelOpen">{{i.viewValue2}}</span>
</mat-option>
</mat-select>
I would like to show additional values in mat option row and hide that additional value after selection. Is There any possibility using panelOpen, Thanks in advance.

If you declare the mat-select as a variable using the # notation, you can then change your ngIf to mySelect.panelOpen where mySelect is the ID you gave the mat-select:
<mat-select #mySelect>
<mat-option *ngFor="let i of List" [value]="i.value">
{{i.viewValue}}
<span *ngIf="mySelect.panelOpen">{{i.viewValue2}}</span>
</mat-option>
</mat-select>
Here's a StackBlitz.

Related

Change event not being invoked in material ui select component

I have to run some code when country is selected in the dropdown but the method is not called at all. Here is the material code:
<mat-form-field>
<mat-label>Select country</mat-label>
<mat-select (change)="countrySelected(value)">
<mat-option>None</mat-option>
<mat-option value="option1">France</mat-option>
<mat-option value="option2">Germany</mat-option>
<mat-option value="option3">America</mat-option>
</mat-select>
</mat-form-field>
Here is the method that is not being called:
countrySelected(value): void {
console.log(value);
}
You should be using the event selectionChange instead. So change your code as follows:
<mat-form-field>
<mat-label>Select country</mat-label>
<mat-select (change)="selectionChange(value)">
<mat-option>None</mat-option>
<mat-option value="option1">France</mat-option>
<mat-option value="option2">Germany</mat-option>
<mat-option value="option3">America</mat-option>
</mat-select>
</mat-form-field>
Checkout https://material.angular.io/components/select/api for all supported events.

How to change the direction of a dropdown in a mat select?

I'm using a mat select to display a list of items.
<mat-label>{{ 'CLAIMS.PLACEHOLDERS.STATUS' | translate }}</mat-label>
<mat-select (valueChange)="onStatusChange($event)">
<mat-option *ngFor="let option of statusses" [value]="option">
{{ 'STATUS.' + option | translate }}
</mat-option>
</mat-select>
When selecting certain options my dropdown is "stuck" in my navbar:
Is there any way to change the "direction" of the dropdown? So it does down instead of up?
I was able to force my dropdown to open downwards by adding the disableOptionCentering option.
<mat-label>{{ 'CLAIMS.PLACEHOLDERS.CLAIMTYPE' | translate }}</mat-label>
<mat-select (valueChange)="onClaimTypeChange($event)" disableOptionCentering>
<mat-option *ngFor="let option of claimTypes" [value]="option">
{{ option }}
</mat-option>
</mat-select>

Angular Material Orphaned form label on mat-select

I am running into an issue when testing for accessibility problems on an Angular 8 Material app. I am using the Chrome Plugin WAVE Evaluation Tool to check for accessibility https://chrome.google.com/webstore/detail/wave-evaluation-tool/jbbplnpkjmmeebjpijfedlgcdilocofh.
The problem looks to be only with a Mat Select list.
-- Error
image of Mat list with Orphaned Form Lable
WAVE tool Errors
-- Code ( this is what I started with )
<mat-form-field>
<mat-select>
<mat-option *ngFor="let tag of tags" [value]="tags.value">
{{ tag.viewValue }}
</mat-option>
</mat-select>
<mat-label>Resource Tag</mat-label>
</mat-form-field>
-- Code (then after reading that it was missing the form label I changed it to this)
<mat-form-field>
<mat-select id="tags">
<mat-option *ngFor="let tag of tags" [value]="tags.value">
{{ tag.viewValue }}
</mat-option>
</mat-select>
<mat-label for="tags">Resource Tag</mat-label>
</mat-form-field>
Even after that change, the Orphaned Form Lable is still there.
I also removed all Material elements and just created a default select list using HTML elements that worked and did NOT have a missing form label. From this my guess its something going on in the Material elements.

Hide values from the Angular Material multiple drop down list

How can I hide the values in the drop down box. What I want to achieve is just a placeholder without showing the values selected from multiple. I should keep this ngmodel and selectionChange for other purposes.
Is there any way?
<mat-form-field>
<mat-select
placeholder="Select Columns"
(selectionChange)="columnSelect()"
multiple
[(ngModel)]="selectedColumns"
>
<mat-option
*ngFor="let c of fixedColumns"
[value]="c"
>
{{ c }}
</mat-option>
</mat-select>
</mat-form-field>
I solved the problem by adding mat-select-trigger;
<mat-select
(selectionChange)="columnSelect()"
multiple
[(ngModel)]="selectedColumns"
>
<mat-select-trigger class="trigger">
Select Columns
</mat-select-trigger>
<mat-option
*ngFor="let c of fixedColumns"
[value]="c"
>
{{ c }}
</mat-option>
</mat-select>
``

After upgrading to Angular 6, material 6, select option event fired continuously and browser hangs. How to stop unwanted emit events?

In below code, my country select options are fired so many times that browser stopped responding.
<div [formGroup]="countryForm">
<mat-form-field>
<mat-select formControlName="selectedCountry" class="my-item-text" placeholder="Country">
<mat-option (onSelectionChange)="onCountrySelectionChanged($event)" *ngFor="let myCountry of countries" [value]="myCountry.short" class="my-item-text">{{ myCountry.name }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
and my component code as below
onCountrySelectionChanged(changeEvent) {
if (changeEvent && changeEvent.isUserInput && this.selectedCountry != changeEvent.source.value) {
this.countrySelected.emit( changeEvent.source.value);
}
}
I tried to restrict by checking if its user change event [isUserInput] and also checking if the value really changed! Now am able to reduce the fire-events and the application works normal.
Is there a better way to use select-option as am now including above logic everywhere am using mat-select component.
The mat-select has an Output property you can bind to called selectionChange, which should fire whenever the option is changed by the user. Try switching up your code to this:
<div [formGroup]="countryForm">
<mat-form-field>
<mat-select (selectionChange)="onCountrySelectionChanged($event)" formControlName="selectedCountry" class="my-item-text" placeholder="Country">
<mat-option *ngFor="let myCountry of countries" [value]="myCountry.short" class="my-item-text">{{ myCountry.name }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
Thinking about it - what's probably happened is that you've put your onSelectionChange binding on every single mat-option, so when you change the option, you're likely firing that once for each option in your select. Moving it out to the mat-select means it will only fire once.

Resources