简体   繁体   中英

Get source FormControl from mat-autocomplete

I have a table of dynamic matInput objects like so:

    <div formArrayName="salesItems">
        <table class="app-sales-items-table">
            <thead>
                <th>Qty</th>
                <th>Item #</th>
                <th>Description</th>
                <th>Weight</th>
                <th>Serial No</th>
                <th>Unit Price</th>
                <th>Item Discount %</th>
                <th>Line Total</th>
            </thead>
            <tbody>
                <tr *ngFor="let item of salesItemsForm.get('salesItems').controls; let index=index" [formGroupName]="index">
                    <td>{{item.qty}}</td>
                    <td>
                        <mat-form-field class="field-full-width">
                            <input type="text" matInput formControlName="itemNumber" [matAutocomplete]="autoItemNumber">
                        </mat-form-field>
                    </td>
                    <td>
                        <mat-form-field class="field-full-width">
                            <input type="text" matInput formControlName="description" [matAutocomplete]="autoDescription">
                        </mat-form-field>

                    </td>
                    <td>{{item.weight}}</td>
                    <td>{{item.serialNumber}}</td>
                    <td>{{item.unitPrice}}</td>
                    <td>{{item.itemDiscount}}</td>
                    <td>{{item.lineTotal}}</td>
                </tr>
            </tbody>
        </table>
    </div>

Each matInput has a corresponding mat-autocomplete, for example:

    <mat-autocomplete #autoDescription="matAutocomplete" (optionSelected)="setSelectedSalesItem($event.option.value, $event.source)">
        <mat-option *ngFor="let option of (filteredSalesItems )" [value]="option">{{option.description}}
        </mat-option>
    </mat-autocomplete>

As you can see, in the optionSelected method, I'm passing in $event.source. My real goal is to get the FormControl that triggered the event, so I can do things like set the other values in the same row. The issue is $event.source is of type MatAutoComplete, and I'm not sure how to get the (reactive) FormControl from that.

Edit:

stackblitz here: https://stackblitz.com/edit/angular-mzryga?file=src%2Fapp%2Fapp.component.ts

You can add another parameter to the function call. You already have the formControlName , you can pass the formControlName [itemNumber/description] and use it to get the formControl .

I think I got it. I moved the inside the ngFor. Siince my ngFor looks like this:

<tr *ngFor="let item of salesItemsForm.get('salesItems').controls; let index=index" [formGroupName]="index">

i can pass in item and it's gravy from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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