According to AhmerMH, providing more precise details and sharing some code along with other information would be beneficial.
The type of drop-down menu you require and the structure of your data play a significant role in implementation. Here are some example snippets that might assist you:
- If you need a drop-down menu similar to mat-menu and your data is organized in lists, you can utilize the following approach:
Drop Down
Codes and Descriptions
option 2
option 3
option 4
<mat-menu #moreOptions="matMenu">
//it depends on how the objects you want to display are made
//if code and description are in the same object:
<div *ngFor="let c of codesList; index as i">
<button mat-menu-item>{{c.code}} {{c.description}}</button>
</div>
//otherwise, if code and description are separated: (if codes are in "codesList" and descriptions are in "descsList" and if descsList is ordered in the same way of codesList)
<div *ngFor="let c of codesList; index as i">
<button mat-menu-item>{{c}} {{descsList[i]}}</button>
</div>
</mat-menu>
- In case you are looking for a form-field type drop-down, there are various scenarios depending on your data:
2.1 - If you have a set amount of fixed data points, consider using this method:
<div class="row">
<div class="col">
<mat-form-field class="" appearance="outline" floatLabel="always" hideRequiredMarker>
<mat-label>Codes and Descriptions</mat-label>
<mat-select formControlName="data" (selectionChange)="doSomething()">
<mat-option *ngFor="let d of referenceData" [value]="d.value">
{{d.code}} {{d.description}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</div>
To correspondingly define it in your .TS file, use:
form = this.fb.group({
data: [''],
})
And include the list of data like so:
referenceData = [
{code: 'Code One', description: 'Desc One', value: 'first_value'},
{code: 'Code Two', description: 'Desc Two', value: 'second_value'},
{code: 'Code Three', description: 'Desc Three', value: 'third_value'},
]
2.2 - If your data varies and needs to be fetched from a database:
<div class="row">
<div class="col">
<mat-form-field class="" appearance="outline" hideRequiredMarker formGroupName="data" *ngIf="data$ | async as datas">
<mat-label class="font-18-semi-b">Codes and Descriptions</mat-label>
<input matInput formControlName="id" placeholder="Data">
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayData(datas)" (optionSelected)="dataChanged($event.option.value)">
<mat-option *ngFor="let d of datas" [value]="d.id">
{{d.code}} {{d.description}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
</div>
In your .TS file:
form = this.fb.group({
data: this.fb.group({
id: ['',],
}),
})
data$: Observable<Data>;
displayData(datas: Data[]) {
return (dataId?: number): string | undefined => {
let data = datas.find(option => option.id === dataId);
return data ? data.code + " " + data.description : undefined
};
}
dataChanged(event){
this.dataService.get(event).pipe( //your service that you use to get the data from the server
tap((value:Data) => {
//do what you want, like:
var selectedData = value;
})
).subscribe();
this.form.get("data.id").setValue(event);
}
I hope this provides some assistance :)