如何使用angular2获取按钮单击下的primeng下拉列表的选定文本

时间:2017-06-03 07:45:40

标签: javascript jquery angular angular2-template angular2-forms

我无法在按钮点击时获得primeng下拉列表的选定文本。 我使用formgroup来div并向网格添加多个条目。 我尝试了很多,只获得选定的价值。我也需要下拉标签。 请帮我。 HTML标记:

<div class="row">
                <div class="form-group row" formGroupName="FarePaxDetails">
                    <label for="AirportName" class="col-sm-1 control-label">Pax Type</label>
                    <div class="col-sm-1">
                        <p-dropdown [options]="paxes" [(ngModel)]="PaxTypeId" (onChange)="ChangePaxType($event)" formControlName="PaxType"></p-dropdown>
                    </div>
                    <label for="AirportName" class="col-sm-2 control-label">Baggage Allow</label>
                    <div class="col-sm-1">
                        <input type="text" class="form-control" formControlName="BeggageAllow" [(ngModel)]="BeggageAllow" placeholder="Baggage Allow" />
                    </div>
                    <label for="AirportName" class="col-sm-2 control-label">Adult Fare(%)</label>
                    <div class="col-sm-1">
                        <input type="text" class="form-control" [(ngModel)]="Percentage" formControlName="Percentage" placeholder="Adult Fare(%)" />
                    </div>
                    <label for="AirportName" class="col-sm-1 control-label">Amount</label>
                    <div class="col-sm-1">
                        <input type="text" class="form-control" [(ngModel)]="Amount" formControlName="Amount" placeholder="Amount" />
                    </div>
                    <div class="col-sm-1">
                        <button type="button" (click)="AddFarePaxType(FarePaxDetails)" pButton class="btn btn-info" label="Add"></button>
                    </div>
                </div>
            </div>

以下是HTML组件中的代码:

import { Component, ViewEncapsulation } from '@angular/core';

import { TabViewModule, CheckboxModule, DropdownModule, RadioButtonModule, SelectItem, FieldsetModule } from 'primeng/primeng';

import { Response } from '@angular/http';

import { PayTypeService } from '../../Service/PaxTypeService';

import { FormGroup, FormControl, Validators, FormsModule, ReactiveFormsModule } from '@angular/forms';

import { DataTableModule, SharedModule, LazyLoadEvent, DataTable, ConfirmDialogModule, ConfirmationService } from 'primeng/primeng';
@Component({

    selector: 'main-fairtype',
    templateUrl: './mainfaretype.component.html',
    styleUrls: ['./mainfaretype.component.css'],
    encapsulation: ViewEncapsulation.None,
    providers: [PayTypeService]
})

export class MainFareTypeComponent {    

    paxes: SelectItem[];

    PaxFareTypeList: any[];

    public mainForm: FormGroup;

    constructor(private PayTypeService: PayTypeService) {
        this.mainForm = new FormGroup({
        FareType: new FormControl('', [Validators.required]),
        FareColor: new FormControl(''),
        TourCode: new FormControl('', [Validators.required]),
        FareBasis: new FormControl('', [Validators.required]),
        MinStay: new FormControl('', [Validators.required]),
        MaxStay: new FormControl('', [Validators.required]),
        TicketBefore: new FormControl('', [Validators.required]),
        ReservationBefore: new FormControl('', [Validators.required]),
        Restrictions: new FormControl(''),
        FareRule: new FormControl(''),
        FarePaxDetails: new FormGroup({
            PaxType: new FormControl('', [Validators.required]),
            BeggageAllow: new FormControl('', [Validators.required]),
            Percentage: new FormControl('', [Validators.required]),
            Amount: new FormControl('', [Validators.required])
        })
    });
    this.PayTypeService.GetAllPaxes().then((data: any) => {
        debugger
        this.paxes = [];
        for (var i = 0; i < data.length; i++)
            this.paxes.push({ label: data[i].paxTypeName, value: data[i].paxTypeId });
    });
    this.PaxFareTypeList = [];
}
AddFarePaxType(data: any) {
    this.PaxFareTypeList.push({
        PaxType: this.mainForm.controls.FarePaxDetails.get('PaxType').value,
        Baggage: this.mainForm.controls.FarePaxDetails.get('BeggageAllow').value,
        Percentage: this.mainForm.controls.FarePaxDetails.get('Percentage').value,
        Amount: this.mainForm.controls.FarePaxDetails.get('Amount').value
    })
}

2 个答案:

答案 0 :(得分:1)

我遇到了这个问题,经过数小时的集思广益,我使用了以下技巧进行了修复。

添加对下拉组件的引用,并将其传递给“ onChange”事件处理程序,如下所示。

<p-dropdown #dd [options]="paxes" [(ngModel)]="PaxTypeId" (onChange)="ChangePaxType($event, dd)"
                formControlName="PaxType"></p-dropdown>

ChangePaxType(event, dd: Dropdown){
   console.log(dd.selectedOption.label) // this is your selected item label
}

这是唯一可行的方法!

答案 1 :(得分:0)

  

试试这个:

<div formGroupName="FarePaxDetails">
<div class="col-sm-1">
    <p-dropdown [options]="paxes" [(ngModel)]="PaxTypeId" (onChange)="ChangePaxType($event)"
                formControlName="PaxType"></p-dropdown>
</div>
<label for="AirportName" class="col-sm-2 control-label">Baggage Allow</label>
<div class="col-sm-1">
    <input type="text" class="form-control" formControlName="BeggageAllow" [(ngModel)]="BeggageAllow"
           placeholder="Baggage Allow"/>
</div>
<label for="AirportName" class="col-sm-2 control-label">Adult Fare(%)</label>
<div class="col-sm-1">
    <input type="text" class="form-control" [(ngModel)]="Percentage" formControlName="Percentage"
           placeholder="Adult Fare(%)"/>
</div>
<label for="AirportName" class="col-sm-1 control-label">Amount</label>
<div class="col-sm-1">
    <input type="text" class="form-control" [(ngModel)]="Amount" formControlName="Amount" placeholder="Amount"/>
</div>
<div class="col-sm-1">
    <button type="button" (click)="AddFarePaxType(FarePaxDetails)" pButton class="btn btn-info"
            label="Add"></button>
</div>
</div>
相关问题