简体   繁体   English

在 Angular 中选择下拉菜单

[英]Select drop-down in Angular

I want to create a drop-down that shows options as "code-description" but when the option is selected display only "code" in the selected field. I want to create a drop-down that shows options as "code-description" but when the option is selected display only "code" in the selected field.

Eg : "A-Apple" in the drop-down, but display only "A" when selected.例如:下拉列表中的“A-Apple”,但选择时仅显示“A”。 I can pass the value as only code but unable to display it as code alone in the selected field.我可以将值作为仅代码传递,但无法在所选字段中单独显示为代码。

As said from AhmerMH, it would be better if you can be more precise and share some code and some other information.正如 AhmerMH 所说,如果您可以更精确并共享一些代码和其他一些信息,那就更好了。

It depends a lot from which type of drop down you want and how your data are made.这在很大程度上取决于您想要哪种类型的下拉菜单以及您的数据是如何制作的。 Anyway, i'll show you some examples that maybe can help.无论如何,我会向您展示一些可能会有所帮助的示例。

  1. If you need a drop down menu, like mat-menu, and your data reside in some lists, you can use something like:如果您需要一个下拉菜单,例如 mat-menu,并且您的数据位于某些列表中,您可以使用以下内容:
Drop Down Codes and Descriptions option 2 option 3 option 4 下拉代码和描述选项 2 选项 3 选项 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>
  1. Otherwise if you mean a drop down like a form-field you can have different cases, it always depends on your data:否则,如果您的意思是像表单字段这样的下拉菜单,您可以有不同的情况,它始终取决于您的数据:

2.1 - if you have a small bunch of fixed data you can try with this: 2.1 - 如果你有一小部分固定数据,你可以试试这个:

form = this.fb.group({
    data: [''],
})

and correspondingly in your .TS file, you can define it in the form as并相应地在您的 .TS 文件中,您可以将其定义为

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'},
]

and the list of data as:和数据列表为:

<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>

2.2 - if your data are not fixed and maybe you have to get them from a DB: 2.2 - 如果您的数据不固定,也许您必须从​​数据库中获取它们:

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);
} 

and in your .TS file:并在您的 .TS 文件中:

 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); }

Hope it helps :)希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM