1

I'm trying to add dynamic rows in my ng2 app. Bellow sample working fine. But if I move this HTML template table inside a HTML form tag, it will not display textbox values. but textboxes values hold in ngmodel. How does display the textbox values?

HTML Template:

             <table>
                <tr>
                    <td>Item</td>
                    <td>Qty</td>
                    <td>UP</td>
                    <td>Total</td>
                </tr>
                <tr *ngFor="let row of invoiceItemsData; let i = index">
                    <td><input type="text" class="form-control" required [(ngModel)]="row.item_id" value="{{row.item_id}}" name="item_id"></td>
                    <td><input type="text" class="form-control" required [(ngModel)]="row.qty" value="{{row.qty}}" name="qty"></td>
                    <td><input type="text" class="form-control" required [(ngModel)]="row.unit_price" value="{{row.unit_price}}" name="unit_price"></td>
                    <td><input type="text" class="form-control" required [(ngModel)]="row.total" value="{{row.total}}" name="total"></td>
                    <a class="btn btn-primary" (click)="addNewRow()">New</a>
                </tr>
                {{invoiceItemsData|json}}
            </table>

NG2 Code sample:

export class InvoiceFormComponent implements OnInit {

       public myForm: FormGroup;
       public invoiceItemsData = [{'item_id':'','qty':'','unit_price':'','total':''}] ;

       constructor(private formBuilder: FormBuilder) {}

       ngOnInit() {}

        addNewRow(){
          this.invoiceItemsData.push({'item_id':'','qty':'','unit_price':'','total':''})
        }
}

Output: enter image description here

Added HTML Table inside to HTML from

<form  name="inv" novalidate (ngSubmit)="save(myForm)">
            <table class="table">
                <tr>
                    <td>Item</td>
                    <td>Qty</td>
                    <td>UP</td>
                    <td>Total</td>
                </tr>
                <tr *ngFor="let row of invoiceItemsData; let i = index">
                    <td><input type="text" class="form-control" required [(ngModel)]="row.item_id" value="{{row.item_id}}" name="item_id"></td>
                    <td><input type="text" class="form-control" required [(ngModel)]="row.qty" value="{{row.qty}}" name="qty"></td>
                    <td><input type="text" class="form-control" required [(ngModel)]="row.unit_price" value="{{row.unit_price}}" name="unit_price"></td>
                    <td><input type="text" class="form-control" required [(ngModel)]="row.total" value="{{row.total}}" name="total"></td>

                    <a class="btn btn-primary" (click)="addNewRow()">New</a>
                </tr>
                {{invoiceItemsData|json}}
            </table>
</form>

Output when HTML table move inside to a HTML form: enter image description here

4
  • Can you post the HTML code with the form element? Why do you use both ngModel and value atributes of the inputs? What do you have the myForm property for? Commented Mar 31, 2017 at 9:01
  • Hi @JánHalaša I just add the HTML code with the form element. I just try to display values that's why I put value attributes. But It didn't work. Ignore the myForm part
    – rdanusha
    Commented Mar 31, 2017 at 9:06
  • Please check how forms work. I'm not even sure if you are trying to use dynamic or template driven forms, you are missing quite many bits in your form ;) angular.io/docs/ts/latest/cookbook/dynamic-form.html angular.io/docs/ts/latest/cookbook/… There's a couple of links for you to check out. There are also tons of articles/tutorials out there, just google :)
    – AVJT82
    Commented Mar 31, 2017 at 9:34
  • It work after form fields name attribute change dynamically. Ex: name="item_id_{{ i }}"
    – rdanusha
    Commented Apr 17, 2017 at 7:25

0

Browse other questions tagged or ask your own question.