0
votes

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

1
Can you please create a stackblitz to reproduce the issue.Sachin Gupta

1 Answers

0
votes

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.